gh-157741: Prevent NULL 'environ' pointer after os.environ.clear() - #157745
search1ofall-maker wants to merge 14 commits into
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
There are multiple news entry files now, please remove the invalid one. |
Cleaned up the duplicate news entry and regenerated clinic files. Thanks for catching that! |
Co-authored-by: An Long <aisk@users.noreply.github.com>
…bt18o.rst Co-authored-by: An Long <aisk@users.noreply.github.com>
|
Hi @aisk, All CI checks are now passing! To summarize the fix: os._clearenv() now points environ to a static empty environment array (empty_environ) after clearenv() runs to prevent crashes in C libraries accessing environ. empty_environ is declared as const with ignored.tsv updated to satisfy check-c-globals while avoiding NULL pointer dereferences. Ready for your review whenever you have time. Thanks! |
|
Thanks @aisk! I've simplified empty_environ back to static char *empty_environ[] = {NULL};, removed the explicit cast, and sorted its entry alphabetically in ignored.tsv. |
|
@aisk The failure in test_tkinter on macos-26-intel appears to be an unrelated flaky CI test (GUI/display issue on macOS runner). All core OS/environment tests passed cleanly. I think it is not because of my changes |
Is it possible to merge it |
Let's wait for @ericsnowcurrently or another core dev to review it. And please be patient before it gets merged, as there are a lot of pending PRs and issues in the backlog these days. |
|
All CI checks are now passing! |
What issue does this PR fix?
Fixes #157741
Description of changes
In
glibc, callingclearenv()sets the Cenvironpointer toNULL. Third-party C extensions and libraries (such as Tcl/Tk) accessenvirondirectly without checking forNULL, leading to a segmentation fault whenos.environ.clear()is called.This PR sets
environto a static empty array (empty_environ = { NULL }) immediately after callingclearenv()inos._clearenv, matching the safe fallback behavior used whenHAVE_CLEARENVis not defined.os.environ.clear()now leaves the Cenvironpointer NULL, which crashes Tcl/Tk (tkinter) on the nextTcl_CreateInterp#157741