public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] driver: fix environ corruption after putenv() [PR106624]
@ 2022-08-16 11:48 Sergei Trofimovich
  2022-08-16 11:59 ` Martin Liška
  0 siblings, 1 reply; 2+ messages in thread
From: Sergei Trofimovich @ 2022-08-16 11:48 UTC (permalink / raw)
  To: gcc-patches; +Cc: Martin Liska, Sergei Trofimovich

From: Sergei Trofimovich <siarheit@google.com>

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 (driver::detect_jobserver): Allocate storage xputenv()
	argument using xstrdup().
---
 gcc/gcc.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index cac11c1a117..75ca0ece1a4 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.  */
-- 
2.37.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] driver: fix environ corruption after putenv() [PR106624]
  2022-08-16 11:48 [PATCH] driver: fix environ corruption after putenv() [PR106624] Sergei Trofimovich
@ 2022-08-16 11:59 ` Martin Liška
  0 siblings, 0 replies; 2+ messages in thread
From: Martin Liška @ 2022-08-16 11:59 UTC (permalink / raw)
  To: Sergei Trofimovich, gcc-patches; +Cc: Sergei Trofimovich

On 8/16/22 13:48, Sergei Trofimovich wrote:
> From: Sergei Trofimovich <siarheit@google.com>
> 
> 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.

Thanks for the patch.

I think it's an obvious fix, please install it.

Martin

> 
> gcc/
> 
> 	PR driver/106624
> 	* gcc (driver::detect_jobserver): Allocate storage xputenv()
> 	argument using xstrdup().
> ---
>  gcc/gcc.cc | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/gcc/gcc.cc b/gcc/gcc.cc
> index cac11c1a117..75ca0ece1a4 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.  */


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-16 11:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-16 11:48 [PATCH] driver: fix environ corruption after putenv() [PR106624] Sergei Trofimovich
2022-08-16 11:59 ` Martin Liška

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).