From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1851) id E67963858D1E; Tue, 24 Jan 2023 10:52:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E67963858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674557545; bh=k/8fywqf23iabU4daGASVBUKrt8blMHSVy0zsssJy0E=; h=From:To:Subject:Date:From; b=AlC3yVhuVogu3It0u2aro1FV/GGsZcGDRSUVPd1vvZkD9wifHyMtkilccIC36kPTg 0KqsxTgR3tJ+tOdrz53cIm7kX6rCFdzLGHz7CxKtyZ48oYRg7Brs4QFKFIMjNsbd8B JNOVMFr0x72rG0vLUd3zxFHEWvG9dMjFIVszdrso= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Martin Liska To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9061] driver: fix environ corruption after putenv() [PR106624] X-Act-Checkin: gcc X-Git-Author: Sergei Trofimovich X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 7ec291e86b2fce0e0d9f2f495af449f3c844bf6f X-Git-Newrev: 193f7e62815b4089dfaed4c2bd34fd4f10209e27 Message-Id: <20230124105225.E67963858D1E@sourceware.org> Date: Tue, 24 Jan 2023 10:52:25 +0000 (GMT) List-Id: https://gcc.gnu.org/g:193f7e62815b4089dfaed4c2bd34fd4f10209e27 commit r12-9061-g193f7e62815b4089dfaed4c2bd34fd4f10209e27 Author: Sergei Trofimovich 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.cc (driver::detect_jobserver): Allocate storage xputenv() argument using xstrdup(). (cherry picked from commit 2b403297b111c990c331b5bbb6165b061ad2259b) Diff: --- gcc/gcc.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gcc/gcc.cc b/gcc/gcc.cc index b4c8d659014..fbcc9d03314 100644 --- a/gcc/gcc.cc +++ b/gcc/gcc.cc @@ -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. */