]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
driver: fix environ corruption after putenv() [PR106624]
authorSergei Trofimovich <siarheit@google.com>
Tue, 16 Aug 2022 11:35:07 +0000 (12:35 +0100)
committerSergei Trofimovich <siarheit@google.com>
Tue, 16 Aug 2022 12:15:46 +0000 (13:15 +0100)
The bug appeared afte r13-2010-g1270ccda70ca09 "Factor out
jobserver_active_p" slightly changed `putenv()` use from allocating
to non-allocating:

    -xputenv (concat ("MAKEFLAGS=", dup, NULL));
    +xputenv (jinfo.skipped_makeflags.c_str ());

`xputenv()` (and `putenv()`) don't copy strings and only store the
pointer in the `environ` global table. As a result `environ` got
corrupted as soon as `jinfo.skipped_makeflags` store got deallocated.

This started causing bootstrap crashes in `execv()` calls:

    xgcc: fatal error: cannot execute '/build/build/./prev-gcc/collect2': execv: Bad address

The change restores memory allocation for `xputenv()` argument.

gcc/

PR driver/106624
* gcc.cc (driver::detect_jobserver): Allocate storage xputenv()
argument using xstrdup().

gcc/gcc.cc

index cac11c1a117241000d0c4316e4db5e3e4f5aa155..75ca0ece1a46ca2d3898571cf7ea48f47934e014 100644 (file)
@@ -9182,7 +9182,7 @@ driver::detect_jobserver () const
 {
   jobserver_info jinfo;
   if (!jinfo.is_active && !jinfo.skipped_makeflags.empty ())
-    xputenv (jinfo.skipped_makeflags.c_str ());
+    xputenv (xstrdup (jinfo.skipped_makeflags.c_str ()));
 }
 
 /* Determine what the exit code of the driver should be.  */