public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Martin Sebor <msebor@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] stdlib: Avoid -Wuse-after-free in __add_to_environ [BZ #26779]
Date: Wed, 26 Jan 2022 02:58:30 +0000 (GMT)	[thread overview]
Message-ID: <20220126025830.2F27C385DC30@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=4f20a1dc5242fb4bb8763e0451df898fa48e740c

commit 4f20a1dc5242fb4bb8763e0451df898fa48e740c
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue Jan 25 17:39:36 2022 -0700

    stdlib: Avoid -Wuse-after-free in __add_to_environ [BZ #26779]
    
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>

Diff:
---
 stdlib/setenv.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/stdlib/setenv.c b/stdlib/setenv.c
index c3d2cee7b6..2176cbac31 100644
--- a/stdlib/setenv.c
+++ b/stdlib/setenv.c
@@ -150,7 +150,9 @@ __add_to_environ (const char *name, const char *value, const char *combined,
     {
       char **new_environ;
 
-      /* We allocated this space; we can extend it.  */
+      /* We allocated this space; we can extend it.  Avoid using the raw
+	 reallocated pointer to avoid GCC -Wuse-after-free.  */
+      uintptr_t ip_last_environ = (uintptr_t)last_environ;
       new_environ = (char **) realloc (last_environ,
 				       (size + 2) * sizeof (char *));
       if (new_environ == NULL)
@@ -159,7 +161,7 @@ __add_to_environ (const char *name, const char *value, const char *combined,
 	  return -1;
 	}
 
-      if (__environ != last_environ)
+      if ((uintptr_t)__environ != ip_last_environ)
 	memcpy ((char *) new_environ, (char *) __environ,
 		size * sizeof (char *));


                 reply	other threads:[~2022-01-26  2:58 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=20220126025830.2F27C385DC30@sourceware.org \
    --to=msebor@sourceware.org \
    --cc=glibc-cvs@sourceware.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).