[glibc] string: Fix memory leak in argz-addsep.c

Adhemerval Zanella via Glibc-cvs <[email protected]> Fri, 3 Jul 2026 23:34:30 +0000 (GMT)
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=70dd422b6ffd02639f39063e59a8f9fb5620ed2c

commit 70dd422b6ffd02639f39063e59a8f9fb5620ed2c
Author: Samuel Balazi <[email protected]>
Date:   Fri Jun 19 15:11:39 2026 +0200

    string: Fix memory leak in argz-addsep.c
    
    Assign the realloc result to a temporary variable, so the original
    memory block is not lost if the allocation fails.
    
    Reviewed-by: Adhemerval Zanella  <[email protected]>

Diff:
---
 string/argz-addsep.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/string/argz-addsep.c b/string/argz-addsep.c
index 509559dee4..a077d9c7c0 100644
--- a/string/argz-addsep.c
+++ b/string/argz-addsep.c
@@ -30,10 +30,12 @@ __argz_add_sep (char **argz, size_t *argz_len, const char *string, int delim)
     {
       const char *rp;
       char *wp;
+      char *tmp_argz;
 
-      *argz = (char *) realloc (*argz, *argz_len + nlen);
-      if (*argz == NULL)
+      tmp_argz = (char *) realloc (*argz, *argz_len + nlen);
+      if (tmp_argz == NULL)
 	return ENOMEM;
+      *argz = tmp_argz;
 
       wp = *argz + *argz_len;
       rp = string;