Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
serhiy-storchaka opened this issue Nov 8, 2023 · 1 comment
Closed

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

serhiy-storchaka opened this issue Nov 8, 2023 · 1 comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes OS-windows type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Nov 8, 2023

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

@serhiy-storchaka serhiy-storchaka added OS-windows 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels Nov 8, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Nov 8, 2023
@serhiy-storchaka serhiy-storchaka added the type-bug An unexpected behavior, bug, or error label Nov 8, 2023
@vstinner vstinner changed the title os.putenv() on Windows truncates on an embedded NUL os.putenv() on Windows truncates value on an embedded NUL Nov 9, 2023
@vstinner
Copy link
Member

vstinner commented Nov 9, 2023

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.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 10, 2023
…on Windows (pythonGH-111842)

(cherry picked from commit 0b06d24)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 10, 2023
…on Windows (pythonGH-111842)

(cherry picked from commit 0b06d24)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Nov 11, 2023
… on Windows (GH-111842) (GH-111966)

(cherry picked from commit 0b06d24)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Nov 11, 2023
… on Windows (GH-111842) (GH-111967)

(cherry picked from commit 0b06d24)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes OS-windows type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants