Skip to content

os.putenv() on Windows truncates value on an embedded NUL #111841

Closed

Description

@serhiy-storchaka

os.putenv() on Windows truncates a value containing an embedded null character.

>>> import os
>>> os.putenv('xyz', 'abc\0def')
>>> os.system('''python.bat -c "import os; print(repr(os.environ['xyz']))"''')
Running Debug|x64 interpreter...
'abc'
0

os.putenv() and os.unsetenv() also truncate a name. It leads to OSError because it truncates before "=".

>>> os.putenv('abc\0def', 'xyz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    os.putenv('abc\0def', 'xyz')
OSError: [Errno 22] Invalid argument
>>> os.unsetenv('abc\0def')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    os.unsetenv('abc\0def')
OSError: [Errno 22] Invalid argument

Linked PRs

Activity

  1. added a commit that references this issue on Nov 8, 2023
  2. changed the title [-]os.putenv() on Windows truncates on an embedded NUL[/-] [+]os.putenv() on Windows truncates value on an embedded NUL[/+] on Nov 9, 2023
  3. vstinner commented on Nov 9, 2023

    @vstinner
    Member

    Only the value is truncated if it contains embedded null character.

    Python doesn't check for embedded null character in the variable name, but as you write, OSError is raised if the name is truncated. And Python rejects name which contains "=", so os.putenv('a=1\0', 'ignore') raises ValueError.

  4. added 2 commits that reference this issue on Nov 10, 2023
  5. added 3 commits that reference this issue on Nov 10, 2023
  6. added a commit that references this issue on Feb 11, 2024
  7. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13only security fixesOS-windowstype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions