Skip to content

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

Closed
@serhiy-storchaka

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13bugs and 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