From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Dmitry Bogatov Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH 1/2] Fix memory leak on `realloc' failure Date: Sat, 12 Apr 2014 10:59:51 +0400 Organization: Church of GNU Message-ID: <20140412065951.GA2532@localhost.kaction.name> References: <1397108266-18581-1-git-send-email-KAction@gnu.org> <20140410163648.MIXO2.198633.root@cdptpa-web07> <20140410165243.GA16599@localhost.kaction.name> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="/04w6evG8XlLl3ft" X-Trace: ger.gmane.org 1397286026 2098 80.91.229.3 (12 Apr 2014 07:00:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 12 Apr 2014 07:00:26 +0000 (UTC) Cc: guile-devel@gnu.org To: dsmich@roadrunner.com Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Apr 12 09:00:20 2014 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WYrvF-0002VD-8k for guile-devel@m.gmane.org; Sat, 12 Apr 2014 09:00:17 +0200 Original-Received: from localhost ([::1]:33100 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WYrvF-0003uK-0Y for guile-devel@m.gmane.org; Sat, 12 Apr 2014 03:00:17 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35896) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WYrv6-0003mm-Nk for guile-devel@gnu.org; Sat, 12 Apr 2014 03:00:13 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WYrv2-00035e-D3 for guile-devel@gnu.org; Sat, 12 Apr 2014 03:00:08 -0400 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:48783) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WYrv2-00035V-As for guile-devel@gnu.org; Sat, 12 Apr 2014 03:00:04 -0400 Original-Received: from [190.122.102.42] (port=41955 helo=localhost) by fencepost.gnu.org with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1WYruz-0004MM-W3; Sat, 12 Apr 2014 03:00:04 -0400 Content-Disposition: inline In-Reply-To: <20140410165243.GA16599@localhost.kaction.name> X-Haskell-Quote: IO String contains a String in the same way that /bin/ls contains a list of files X-Perfect-World: There should be one -- and preferably only one -- obvious way to do it. User-Agent: Mutt/1.5.23 (2014-03-12) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:4830:134:3::e X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:17058 Archived-At: --/04w6evG8XlLl3ft Content-Type: text/plain; charset=us-ascii Content-Disposition: inline * Dmitry Bogatov [2014-04-10 20:52:43+0400] > > > + { > > > + free (nargv); > > > return 0L; > > > + } > > > else > > > nargv[nargi++] = narg; > > I don't understand this. In both cases, free() will be called with an > > argument of 0, which does nothing. > My bad. It does not fix problem, that if realloc fails, it does not free > memory, but we blindly assign, losing pointer to previous memory > chunk. Attached new version. Please, take a look. -- Best regards, Dmitry Bogatov , Free Software supporter, esperantisto and netiquette guardian. git://kaction.name/rc-files.git GPG: 54B7F00D --/04w6evG8XlLl3ft Content-Type: text/x-diff; charset=us-ascii Content-Disposition: inline; filename="0001-Fix-memory-leak-on-realloc-failure.patch" >From d3ce0b807aa624a9316e2b7da024f030233dae74 Mon Sep 17 00:00:00 2001 From: Dmitry Bogatov Date: Thu, 10 Apr 2014 09:23:28 +0400 Subject: [PATCH] Fix memory leak on `realloc' failure Signed-off-by: Dmitry Bogatov --- libguile/script.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/libguile/script.c b/libguile/script.c index 6a8cc43..07a8040 100644 --- a/libguile/script.c +++ b/libguile/script.c @@ -220,6 +220,21 @@ script_get_backslash (FILE *f) } #undef FUNC_NAME +/* + * Like `realloc', but free memory on failure; + * unlike `scm_realloc', return NULL, not aborts. +*/ +static void* +realloc0 (void *ptr, size_t size) +{ + void *new_ptr = realloc (ptr, size); + if (!new_ptr) + { + free (ptr); + } + return new_ptr; +} + static char * script_read_arg (FILE *f) @@ -245,7 +260,7 @@ script_read_arg (FILE *f) if (len >= size) { size = (size + 1) * 2; - buf = realloc (buf, size); + buf = realloc0 (buf, size); if (! buf) return 0; } @@ -328,9 +343,9 @@ scm_get_meta_args (int argc, char **argv) found_args: /* FIXME: we leak the result of calling script_read_arg. */ while ((narg = script_read_arg (f))) - if (!(nargv = (char **) realloc (nargv, + if (!(nargv = (char **) realloc0 (nargv, (1 + ++nargc) * sizeof (char *)))) - return 0L; + return 0L; else nargv[nargi++] = narg; fclose (f); -- I may be not subscribed. Please, keep me in carbon copy. --/04w6evG8XlLl3ft--