[Python-checkins] bpo-37931: Fix crash on OSX re-initializing os.environ (GH-15428)

Miss Islington (bot) webhook-mailer at python.org
Fri Dec 6 14:32:40 EST 2019


https://github.com/python/cpython/commit/836cf31a3cf468ed9598a220b8e194b366287bfe
commit: 836cf31a3cf468ed9598a220b8e194b366287bfe
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-12-06T11:32:33-08:00
summary:

bpo-37931: Fix crash on OSX re-initializing os.environ (GH-15428)


On most platforms, the `environ` symbol is accessible everywhere.

In a dylib on OSX, it's not easily accessible, you need to find it with
_NSGetEnviron.

The code was caching the *value* of environ. But a setenv() can change the value,
leaving garbage at the old value. Fix: don't cache the value of environ, just
read it every time.
(cherry picked from commit 723f71abf7ab0a7be394f9f7b2daa9ecdf6fb1eb)

Co-authored-by: Benoit Hudson <benoit at imgspc.com>

files:
A Misc/NEWS.d/next/macOS/2019-08-23-12-14-34.bpo-37931.goYgQj.rst
M Misc/ACKS
M Modules/posixmodule.c

diff --git a/Misc/ACKS b/Misc/ACKS
index e52ae984f1571..62c5928c508ff 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -725,6 +725,7 @@ Miro Hrončok
 Chiu-Hsiang Hsu
 Chih-Hao Huang
 Christian Hudon
+Benoît Hudson
 Lawrence Hudson
 Michael Hudson
 Jim Hugunin
diff --git a/Misc/NEWS.d/next/macOS/2019-08-23-12-14-34.bpo-37931.goYgQj.rst b/Misc/NEWS.d/next/macOS/2019-08-23-12-14-34.bpo-37931.goYgQj.rst
new file mode 100644
index 0000000000000..45b54e89cb89e
--- /dev/null
+++ b/Misc/NEWS.d/next/macOS/2019-08-23-12-14-34.bpo-37931.goYgQj.rst
@@ -0,0 +1,3 @@
+Fixed a crash on OSX dynamic builds that occurred when re-initializing the
+posix module after a Py_Finalize if the environment had changed since the
+previous `import posix`. Patch by Benoît Hudson.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 7c823434e67fe..850769fd95eef 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1372,7 +1372,6 @@ win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
 ** man environ(7).
 */
 #include <crt_externs.h>
-static char **environ;
 #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
 extern char **environ;
 #endif /* !_MSC_VER */
@@ -1390,15 +1389,16 @@ convertenviron(void)
     d = PyDict_New();
     if (d == NULL)
         return NULL;
-#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
-    if (environ == NULL)
-        environ = *_NSGetEnviron();
-#endif
 #ifdef MS_WINDOWS
     /* _wenviron must be initialized in this way if the program is started
        through main() instead of wmain(). */
     _wgetenv(L"");
     e = _wenviron;
+#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
+    /* environ is not accessible as an extern in a shared object on OSX; use
+       _NSGetEnviron to resolve it. The value changes if you add environment
+       variables between calls to Py_Initialize, so don't cache the value. */
+    e = *_NSGetEnviron();
 #else
     e = environ;
 #endif



More information about the Python-checkins mailing list