From: Dmitry Bogatov <KAction@gnu.org>
To: dsmich@roadrunner.com
Cc: guile-devel@gnu.org
Subject: Re: [PATCH 1/2] Fix memory leak on `realloc' failure
Date: Sat, 12 Apr 2014 10:59:51 +0400 [thread overview]
Message-ID: <20140412065951.GA2532@localhost.kaction.name> (raw)
In-Reply-To: <20140410165243.GA16599@localhost.kaction.name>
[-- Attachment #1: Type: text/plain, Size: 670 bytes --]
* Dmitry Bogatov <KAction@gnu.org> [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 <KAction@gnu.org>,
Free Software supporter, esperantisto and netiquette guardian.
git://kaction.name/rc-files.git
GPG: 54B7F00D
[-- Attachment #2: 0001-Fix-memory-leak-on-realloc-failure.patch --]
[-- Type: text/x-diff, Size: 1578 bytes --]
From d3ce0b807aa624a9316e2b7da024f030233dae74 Mon Sep 17 00:00:00 2001
From: Dmitry Bogatov <KAction@gnu.org>
Date: Thu, 10 Apr 2014 09:23:28 +0400
Subject: [PATCH] Fix memory leak on `realloc' failure
Signed-off-by: Dmitry Bogatov <KAction@gnu.org>
---
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.
next prev parent reply other threads:[~2014-04-12 6:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-10 5:37 [PATCH 1/2] Fix memory leak on `realloc' failure KAction
2014-04-10 5:37 ` [PATCH 2/2] Reduce scope of variables KAction
2014-04-10 16:36 ` [PATCH 1/2] Fix memory leak on `realloc' failure dsmich
2014-04-10 16:52 ` Dmitry Bogatov
2014-04-12 6:59 ` Dmitry Bogatov [this message]
2014-04-22 20:25 ` Andy Wingo
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
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140412065951.GA2532@localhost.kaction.name \
--to=kaction@gnu.org \
--cc=dsmich@roadrunner.com \
--cc=guile-devel@gnu.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).