Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_os/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,17 @@ def test_clearenv(self):

self.assertRaises(TypeError, os.environ.clear, None)

def test_clearenv_environ_not_null(self):
Comment thread
search1ofall-maker marked this conversation as resolved.
from test.support import import_helper
Comment thread
aisk marked this conversation as resolved.
Outdated
ctypes = import_helper.import_module('ctypes')

os.environ.clear()
c_environ = ctypes.c_void_p.in_dll(ctypes.CDLL(None), "environ")
self.assertIsNotNone(
c_environ.value,
"os.environ.clear() set the C 'environ' pointer to NULL"
)


class WalkTests(unittest.TestCase):
"""Tests for os.walk()."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in C libraries after os.environ.clear() by ensuring environ
pointer is not left as NULL.
Comment thread
search1ofall-maker marked this conversation as resolved.
Outdated
8 changes: 8 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -13895,6 +13895,8 @@ os_unsetenv_impl(PyObject *module, PyObject *name)


#ifdef HAVE_CLEARENV
static char *empty_environ[] = { NULL };

/*[clinic input]
os._clearenv
[clinic start generated code]*/
Expand All @@ -13912,6 +13914,12 @@ os__clearenv_impl(PyObject *module)
if (err) {
return posix_error();
}

/* glibc's clearenv() sets 'environ' to NULL. Point it to a static empty
array to prevent crashes in third-party C libraries (e.g. Tcl/Tk)
that access 'environ' without a NULL check. */
environ = empty_environ;

Py_RETURN_NONE;
}
#endif
Expand Down
Loading