]> granicus.if.org Git - php/commitdiff
Fixed bug #64544 (Valgrind warnings after using putenv)
authorXinchen Hui <laruence@php.net>
Fri, 29 Mar 2013 15:42:50 +0000 (23:42 +0800)
committerXinchen Hui <laruence@php.net>
Fri, 29 Mar 2013 15:42:50 +0000 (23:42 +0800)
The frozen_envion is needed, since if an item in environ is updated
(like the test script HOME one), invalid free still shows up

NEWS
sapi/cli/ps_title.c
sapi/cli/tests/bug64544.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index c1cd3e1ab5d4495242dea1887442cc3e7b785512..c8b71167221de60f9b68ee7a421ed5f5ee2f39b5 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,7 @@ PHP                                                                        NEWS
 28 Mar 2013, PHP 5.5.0 Beta 2
 
 - Core:
+  . Fixed bug #64544 (Valgrind warnings after using putenv). (Laruence)
   . Fixed bug #64515 (Memoryleak when using the same variablename 2times in
     function declaration). (Laruence)
   . Fixed bug #64503 (Compilation fails with error: conflicting types for
index 730a31b11e784a629f300b27ffa2c3cba587ccb1..28830c9082a9d44f93e5cdff2166e9c7f68c0c50 100644 (file)
@@ -128,7 +128,7 @@ static char** save_argv;
  * This holds the 'locally' allocated environ from the save_ps_args method.
  * This is subsequently free'd at exit.
  */
-static char** new_environ;
+static char** frozen_environ, **new_environ;
 
 /*
  * Call this method early, before any code has used the original argv passed in
@@ -182,7 +182,8 @@ char** save_ps_args(int argc, char** argv)
          * move the environment out of the way
          */
         new_environ = (char **) malloc((i + 1) * sizeof(char *));
-        if (!new_environ)
+        frozen_environ = (char **) malloc((i + 1) * sizeof(char *));
+        if (!new_environ || !frozen_environ)
             goto clobber_error;
         for (i = 0; environ[i] != NULL; i++)
         {
@@ -192,6 +193,7 @@ char** save_ps_args(int argc, char** argv)
         }
         new_environ[i] = NULL;
         environ = new_environ;
+        memcpy((char *)frozen_environ, (char *)new_environ, sizeof(char *) * (i + 1));
 
     }
 #endif /* PS_USE_CLOBBER_ARGV */
@@ -409,9 +411,9 @@ void cleanup_ps_args(char **argv)
 #ifdef PS_USE_CLOBBER_ARGV
         {
             int i;
-            for (i = 0; new_environ[i] != NULL; i++)
-                free(new_environ[i]);
-            free(new_environ);
+            for (i = 0; frozen_environ[i] != NULL; i++)
+                free(frozen_environ[i]);
+            free(frozen_environ);
         }
 #endif /* PS_USE_CLOBBER_ARGV */
 
diff --git a/sapi/cli/tests/bug64544.phpt b/sapi/cli/tests/bug64544.phpt
new file mode 100644 (file)
index 0000000..cc49545
--- /dev/null
@@ -0,0 +1,20 @@
+--TEST--
+Bug #64544 (Valgrind warnings after using putenv)
+--SKIPIF--
+<?php
+if (substr(PHP_OS, 0, 3) == "WIN") {
+       die("skip non windows test");
+}
+?>
+--FILE--
+<?php
+
+putenv("HOME=/tmp");
+var_dump(getenv("HOME"));
+
+putenv("FOO=BAR");
+var_dump(getenv("FOO"));
+?>
+--EXPECTF--
+string(4) "/tmp"
+string(3) "BAR"