[PATCH] Potential memory leak in argz_replace.c

David Stacey drstacey@tiscali.co.uk
Mon May 11 07:23:00 GMT 2015


I'm new to this list, so allow me to introduce myself. I am Dave Stacey, 
and I maintain a handful of packages for Cygwin. I also run a regular 
scan of the Cygwin source code using the Coverity Scan static analysis 
tool. I'd like to report a few warnings that Coverity reports in 
Cygwin's use of newlib. I'll start with one that's fairly simple; once 
I've learnt how you like these reporting then I'll try to send more.

The first is a theoretical (albeit unlikely) memory leak in 
argz_replace.c that occurs in the event that a realloc(3) fails. The 
attached patch should address this, and a ChangeLog entry is below. The 
change is rather trivial.

I'm aware that you need to support a great many compilers and platforms, 
and so I will completely understand if you would prefer to keep the code 
as it is. Hopefully the patch is in keeping with the style of the 
surrounding code, but if you would like anything altered then let me 
know and I will do my best to oblige.

Regards,

Dave Stacey.

newlib/ChangeLog
2015-05-10  David Stacey  <drstacey@tiscali.co.uk>

     * libc/argz/argz_replace.c: Fix potential memory leak.


-------------- next part --------------
--- a/newlib/libc/argz/argz_replace.c	2015-03-10 10:40:06.000000000 +0000
+++ b/newlib/libc/argz/argz_replace.c	2015-05-10 20:19:28.353985800 +0100
@@ -71,7 +71,10 @@
 
       /* reallocate argz, and copy over the new value. */
       if(!(*argz = (char *)realloc(*argz, new_argz_len)))
-        return ENOMEM;
+        {
+          free(new_argz);
+          return ENOMEM;
+        }
 
       memcpy(*argz, new_argz, new_argz_len);
       *argz_len = new_argz_len;


More information about the Newlib mailing list