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

Not guarding env value in subprocess.py cause python crash when certain env variable is set to None #99238

Closed
tianrui-wei opened this issue Nov 8, 2022 · 4 comments · Fixed by #99253
Labels
type-bug An unexpected behavior, bug, or error

Comments

@tianrui-wei
Copy link

tianrui-wei commented Nov 8, 2022

Bug report

A minimal bug can be found using the following file

from subprocess import Popen, PIPE

env = {}
env["a"] = None
process = Popen(['echo', '"hello"'], stdout=PIPE, stderr=PIPE, env = env)
stdout, stderr = process.communicate()

Your environment

M1 MacOS Monterey, python 3.10
x86_64 Ubuntu 20.04 LTS, python 3.11 (installed via pyenv)

@gpshead
Copy link
Member

gpshead commented Nov 8, 2022

Environment dictionaries are string to string maps. - https://docs.python.org/3/library/os.html#os.environ None is not a valid value. If code wants to define an environment variable with no value it should set it to an empty string. env["a"] = "".

While the subprocess module docs do not explicitly say this, the intent was always that this dict matched the type you'd find in os.environ. We could improve the subprocess docs to make this clear.

@tianrui-wei
Copy link
Author

Do you think there's a stricter type checking approach we could take rather than the PR I proposed? I ran into a library that does this and unfortunately took quite a while to debug. I'd love your feedback on this matter, thanks.

@gpshead
Copy link
Member

gpshead commented Nov 8, 2022

We shouldn't be adding assertions into the standard library because of uncommon bugs in calling code. However if you use a static analysis tool like pytype or mypy it should catch this one as the subprocess API definition in https://github.com/python/typeshed/blob/main/stdlib/subprocess.pyi#L73 that python type checkers is correct for env. None is not allowed.

gpshead added a commit to gpshead/cpython that referenced this issue Nov 8, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 8, 2022
(cherry picked from commit 2eee9d9)

Co-authored-by: Gregory P. Smith <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 8, 2022
(cherry picked from commit 2eee9d9)

Co-authored-by: Gregory P. Smith <[email protected]>
miss-islington added a commit that referenced this issue Nov 8, 2022
(cherry picked from commit 2eee9d9)

Co-authored-by: Gregory P. Smith <[email protected]>
miss-islington added a commit that referenced this issue Nov 8, 2022
(cherry picked from commit 2eee9d9)

Co-authored-by: Gregory P. Smith <[email protected]>
@tianrui-wei
Copy link
Author

Thanks Gregory, this looks really good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants