Skip to content

Commit

Permalink
Fix crash during perl's shutdown when env variables were modified by …
Browse files Browse the repository at this point in the history
…Python

When perl is configured without -Accflags='-DPERL_USE_SAFE_PUTENV' it modifies
environment variables directly without going through the C library. During
shutdown of the interpreter it will then free() the environment variable strings.

Python uses its own allocator for strings so a free() on one of those will
explode with for example:
*** glibc detected *** /build/toolchain/lin32/perl-5.10.0/bin/perl:
free(): invalid pointer: 0x0da50904 ***

Perl can be forced to use putenv() to set environment variables by compiling
with -DPERL_USE_SAFE_PUTENV or at runtime by setting PL_use_safe_putenv to a
non-zero value. It will then not try to free() the environment strings either.

So we set PL_use_safe_putenv to avoid the free(). The downside is that
environment variables may be leaked on systems without a safe putenv(). But
that's probably still better than the crash.

Fixes GH #7 and #9
  • Loading branch information
niner committed Apr 20, 2015
1 parent f439af5 commit 35db0d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions Python.xs
Expand Up @@ -66,6 +66,7 @@ void do_pyinit() {
MODULE = Inline::Python PACKAGE = Inline::Python

BOOT:
PL_use_safe_putenv = 1;
py_true = perl_get_sv("Inline::Python::Boolean::true", FALSE);
py_false = perl_get_sv("Inline::Python::Boolean::false", FALSE);
#ifdef CREATE_PYTHON
Expand Down
8 changes: 8 additions & 0 deletions t/34env.t
@@ -0,0 +1,8 @@
use Test::More;
use Inline Python => <<PYTHON;
import os
os.environ["TEST_VARIABLE"] = "BOB"
PYTHON
ok(1, 'survived Python setting an env variable');
done_testing;
# might explode during cleanup but the test harness will catch that

0 comments on commit 35db0d3

Please sign in to comment.