diff options
author | Dmitry Bogatov <KAction@gnu.org> | 2014-04-10 09:23:28 +0400 |
---|---|---|
committer | Andy Wingo <wingo@pobox.com> | 2014-04-22 22:24:22 +0200 |
commit | 7eaa92ffa9594025d989d5584750b72fe5797c20 (patch) | |
tree | e0937b85103f211af7a49624a7a8ff58290807c3 /libguile/script.c | |
parent | d2fcbb193b67106d0c0b57cd1e988acf2d30ace7 (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.c | 21 |
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); |