summaryrefslogtreecommitdiff
path: root/libguile/script.c
diff options
context:
space:
mode:
authorDmitry Bogatov <KAction@gnu.org>2014-04-10 09:23:28 +0400
committerAndy Wingo <wingo@pobox.com>2014-04-22 22:24:22 +0200
commit7eaa92ffa9594025d989d5584750b72fe5797c20 (patch)
treee0937b85103f211af7a49624a7a8ff58290807c3 /libguile/script.c
parentd2fcbb193b67106d0c0b57cd1e988acf2d30ace7 (diff)
Fix memory leak on `realloc' failure
* libguile/script.c (realloc0): New helper. (script_read_arg, scm_get_meta_args): Use realloc0, not realloc. Signed-off-by: Dmitry Bogatov <KAction@gnu.org>
Diffstat (limited to 'libguile/script.c')
-rw-r--r--libguile/script.c21
1 files changed, 18 insertions, 3 deletions
diff --git a/libguile/script.c b/libguile/script.c
index 0d7b28fa8..b6910bad3 100644
--- a/libguile/script.c
+++ b/libguile/script.c
@@ -219,6 +219,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)
@@ -244,7 +259,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;
}
@@ -327,9 +342,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);