Skip to content

gh-157741: Prevent NULL 'environ' pointer after os.environ.clear() - #157745

Open
search1ofall-maker wants to merge 14 commits into
python:mainfrom
search1ofall-maker:gh-157741-fix-clearenv-null
Open

search1ofall-maker wants to merge 14 commits into
python:mainfrom
search1ofall-maker:gh-157741-fix-clearenv-null

Conversation

@search1ofall-maker

@search1ofall-maker search1ofall-maker commented Sep 18, 2026

Copy link
Copy Markdown

What issue does this PR fix?

Fixes #157741

Description of changes

In glibc, calling clearenv() sets the C environ pointer to NULL. Third-party C extensions and libraries (such as Tcl/Tk) access environ directly without checking for NULL, leading to a segmentation fault when os.environ.clear() is called.

This PR sets environ to a static empty array (empty_environ = { NULL }) immediately after calling clearenv() in os._clearenv, matching the safe fallback behavior used when HAVE_CLEARENV is not defined.

@python-cla-bot

python-cla-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Sep 18, 2026

Copy link
Copy Markdown

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 skip news label instead.

@bedevere-app

bedevere-app Bot commented Sep 18, 2026

Copy link
Copy Markdown

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 skip news label instead.

@bedevere-app

bedevere-app Bot commented Sep 18, 2026

Copy link
Copy Markdown

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 skip news label instead.

Comment thread Modules/posixmodule.c Outdated
Comment thread Modules/posixmodule.c Outdated
Comment thread Lib/test/test_os/test_os.py Outdated
Comment thread Lib/test/test_os/test_os.py Outdated
@bedevere-app

bedevere-app Bot commented Sep 18, 2026

Copy link
Copy Markdown

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 skip news label instead.

@aisk

aisk commented Sep 18, 2026

Copy link
Copy Markdown
Member

There are multiple news entry files now, please remove the invalid one.

@search1ofall-maker

Copy link
Copy Markdown
Author

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!

Comment thread Lib/test/test_os/test_os.py
Comment thread Lib/test/test_os/test_os.py Outdated
Comment thread Misc/NEWS.d/next/Library/2026-09-18-18-07-38.gh-issue-157741.Bbt18o.rst Outdated
@search1ofall-maker

Copy link
Copy Markdown
Author

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!

Comment thread Modules/posixmodule.c Outdated
Comment thread Tools/c-analyzer/cpython/ignored.tsv Outdated
@search1ofall-maker

Copy link
Copy Markdown
Author

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 aisk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@search1ofall-maker

search1ofall-maker commented Sep 19, 2026

Copy link
Copy Markdown
Author

@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

@search1ofall-maker

Copy link
Copy Markdown
Author

LGTM

Is it possible to merge it

@aisk

aisk commented Sep 20, 2026

Copy link
Copy Markdown
Member

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.

@search1ofall-maker

Copy link
Copy Markdown
Author

All CI checks are now passing!
@ericsnowcurrently , @aisk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

os.environ.clear() now leaves the C environ pointer NULL, which crashes Tcl/Tk (tkinter) on the next Tcl_CreateInterp

2 participants