From: Michael Schroeder Date: Thu, 11 Nov 2004 15:54:43 +0000 (+0100) Subject: SuSE's perl safe_putenf diff X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=50acdf951a03f778010b5632532ec99bfa7c4f6a;p=p5sagit%2Fp5-mst-13.2.git SuSE's perl safe_putenf diff Message-ID: <20041111145443.GA1854@immd4.informatik.uni-erlangen.de> slightly reworked to make the PL_use_safe_putenv variable fit in the current framework. This patch turns on the use of safe putenv for any application that embeds a perl interpreter. p4raw-id: //depot/perl@23507 --- diff --git a/embedvar.h b/embedvar.h index 47056d2..3e7d7f6 100644 --- a/embedvar.h +++ b/embedvar.h @@ -900,6 +900,7 @@ #define PL_sigfpe_saved (PL_Vars.Gsigfpe_saved) #define PL_sv_placeholder (PL_Vars.Gsv_placeholder) #define PL_thr_key (PL_Vars.Gthr_key) +#define PL_use_safe_putenv (PL_Vars.Guse_safe_putenv) #else /* !PERL_GLOBAL_STRUCT */ @@ -917,6 +918,7 @@ #define PL_Gsigfpe_saved PL_sigfpe_saved #define PL_Gsv_placeholder PL_sv_placeholder #define PL_Gthr_key PL_thr_key +#define PL_Guse_safe_putenv PL_use_safe_putenv #endif /* PERL_GLOBAL_STRUCT */ diff --git a/mg.c b/mg.c index d8e17dd..b5a9290 100644 --- a/mg.c +++ b/mg.c @@ -1082,6 +1082,7 @@ Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg) # endif { # ifndef PERL_USE_SAFE_PUTENV + if (!PL_use_safe_putenv) { I32 i; if (environ == PL_origenviron) @@ -1089,6 +1090,7 @@ Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg) else for (i = 0; environ[i]; i++) safesysfree(environ[i]); + } # endif /* PERL_USE_SAFE_PUTENV */ environ[0] = Nullch; diff --git a/miniperlmain.c b/miniperlmain.c index 286ce91..94a6d5b 100644 --- a/miniperlmain.c +++ b/miniperlmain.c @@ -48,6 +48,9 @@ int main(int argc, char **argv, char **env) { int exitstatus; +#ifndef PERL_USE_SAFE_PUTENV + PL_use_safe_putenv = 0; +#endif /* PERL_USE_SAFE_PUTENV */ #ifdef PERL_GLOBAL_STRUCT #define PERLVAR(var,type) /**/ diff --git a/perl.c b/perl.c index 1525df2..03187a3 100644 --- a/perl.c +++ b/perl.c @@ -471,7 +471,7 @@ perl_destruct(pTHXx) */ #ifndef PERL_MICRO #if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV) - if (environ != PL_origenviron + if (environ != PL_origenviron && !PL_use_safe_putenv #ifdef USE_ITHREADS /* only main thread can free environ[0] contents */ && PL_curinterp == aTHX diff --git a/perlapi.h b/perlapi.h index b74d1db..dddb24f 100644 --- a/perlapi.h +++ b/perlapi.h @@ -946,6 +946,8 @@ END_EXTERN_C #define PL_sv_placeholder (*Perl_Gsv_placeholder_ptr(NULL)) #undef PL_thr_key #define PL_thr_key (*Perl_Gthr_key_ptr(NULL)) +#undef PL_use_safe_putenv +#define PL_use_safe_putenv (*Perl_Guse_safe_putenv_ptr(NULL)) #endif /* !PERL_CORE */ #endif /* MULTIPLICITY */ diff --git a/perlvars.h b/perlvars.h index 9cc8a2f..d57cb28 100644 --- a/perlvars.h +++ b/perlvars.h @@ -69,3 +69,6 @@ PERLVAR(Gsv_placeholder, SV) PERLVARI(Gcsighandlerp, Sighandler_t, &Perl_csighandler) /* Pointer to C-level sighandler */ #endif +#ifndef PERL_USE_SAFE_PUTENV +PERLVARI(Guse_safe_putenv, int, 1) +#endif diff --git a/util.c b/util.c index 52319d3..9a90549 100644 --- a/util.c +++ b/util.c @@ -1372,6 +1372,7 @@ Perl_my_setenv(pTHX_ char *nam, char *val) #endif { #ifndef PERL_USE_SAFE_PUTENV + if (!PL_use_safe_putenv) { /* most putenv()s leak, so we manipulate environ directly */ register I32 i=setenv_getix(nam); /* where does it go? */ int nlen, vlen; @@ -1412,8 +1413,8 @@ Perl_my_setenv(pTHX_ char *nam, char *val) environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char)); /* all that work just for this */ my_setenv_format(environ[i], nam, nlen, val, vlen); - -#else /* PERL_USE_SAFE_PUTENV */ + } else { +# endif # if defined(__CYGWIN__) || defined( EPOC) setenv(nam, val, 1); # else @@ -1428,7 +1429,9 @@ Perl_my_setenv(pTHX_ char *nam, char *val) my_setenv_format(new_env, nam, nlen, val, vlen); (void)putenv(new_env); # endif /* __CYGWIN__ */ -#endif /* PERL_USE_SAFE_PUTENV */ +#ifndef PERL_USE_SAFE_PUTENV + } +#endif } }