diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-08-13 14:39:42 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-08-13 14:39:42 -0700 |
commit | 23fa33d7061a2bba7f0732ea0c8c324a560a65d3 (patch) | |
tree | f291bcf77d07b56aeb766a77c675d01d0f9d7d53 | |
parent | aac963bb2d95db0ec7aa27f0e12159ee8edbb168 (diff) |
Improve handling of realloc()
Fixes cppcheck warning:
[fslsfonts.c:268]: (error) Common realloc mistake: 'font_list' nulled but not freed upon failure
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | fslsfonts.c | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/fslsfonts.c b/fslsfonts.c index 3138a81..3608e5a 100644 --- a/fslsfonts.c +++ b/fslsfonts.c @@ -264,17 +264,19 @@ get_list(const char *pattern) program_name, pattern); return; } - if (font_list) - font_list = realloc(font_list, (font_cnt + (unsigned) available) - * sizeof(FontList)); - else - font_list = malloc((font_cnt + (unsigned) available) - * sizeof(FontList)); - if (font_list == NULL) { - fprintf(stderr, "%s: unable to allocate %zu bytes for font list\n", - program_name, - (font_cnt + (unsigned) available) * sizeof(FontList)); - exit(-1); + else { + FontList *old_list = font_list; + + font_list = realloc(old_list, (font_cnt + (unsigned) available) + * sizeof(FontList)); + + if (font_list == NULL) { + free(old_list); + fprintf(stderr, "%s: unable to allocate %zu bytes for font list\n", + program_name, + (font_cnt + (unsigned) available) * sizeof(FontList)); + exit(-1); + } } for (i = 0; i < available; i++) { font_list[font_cnt].name = fonts[i]; |