Skip to content

Commit 723f71a

Browse files
benoithudsonvstinner
authored andcommitted
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.
1 parent e76ee1a commit 723f71a

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ Miro Hrončok
733733
Chiu-Hsiang Hsu
734734
Chih-Hao Huang
735735
Christian Hudon
736+
Benoît Hudson
736737
Lawrence Hudson
737738
Michael Hudson
738739
Jim Hugunin
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a crash on OSX dynamic builds that occurred when re-initializing the
2+
posix module after a Py_Finalize if the environment had changed since the
3+
previous `import posix`. Patch by Benoît Hudson.

Modules/posixmodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,6 @@ win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
14021402
** man environ(7).
14031403
*/
14041404
#include <crt_externs.h>
1405-
static char **environ;
14061405
#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
14071406
extern char **environ;
14081407
#endif /* !_MSC_VER */
@@ -1420,15 +1419,16 @@ convertenviron(void)
14201419
d = PyDict_New();
14211420
if (d == NULL)
14221421
return NULL;
1423-
#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1424-
if (environ == NULL)
1425-
environ = *_NSGetEnviron();
1426-
#endif
14271422
#ifdef MS_WINDOWS
14281423
/* _wenviron must be initialized in this way if the program is started
14291424
through main() instead of wmain(). */
14301425
_wgetenv(L"");
14311426
e = _wenviron;
1427+
#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1428+
/* environ is not accessible as an extern in a shared object on OSX; use
1429+
_NSGetEnviron to resolve it. The value changes if you add environment
1430+
variables between calls to Py_Initialize, so don't cache the value. */
1431+
e = *_NSGetEnviron();
14321432
#else
14331433
e = environ;
14341434
#endif

0 commit comments

Comments
 (0)