public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Martin Liska <marxin@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-10479] driver: fix environ corruption after putenv() [PR106624]
Date: Tue, 24 Jan 2023 10:53:42 +0000 (GMT)	[thread overview]
Message-ID: <20230124105342.0DA883858D1E@sourceware.org> (raw)

https://gcc.gnu.org/g:9d21cc4edd94f8f2b1a3241fab5cf75649003226

commit r11-10479-g9d21cc4edd94f8f2b1a3241fab5cf75649003226
Author: Sergei Trofimovich <siarheit@google.com>
Date:   Tue Aug 16 12:35:07 2022 +0100

    driver: fix environ corruption after putenv() [PR106624]
    
    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.c (driver::detect_jobserver): Allocate storage xputenv()
            argument using xstrdup().
    
    (cherry picked from commit 2b403297b111c990c331b5bbb6165b061ad2259b)

Diff:
---
 gcc/gcc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/gcc.c b/gcc/gcc.c
index 795cf9eb01d..20a649ea08e 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -9077,7 +9077,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.  */

                 reply	other threads:[~2023-01-24 10:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230124105342.0DA883858D1E@sourceware.org \
    --to=marxin@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).