From 8e8aabed9af3a9e0aaa08237069f30c9d8f9b1b5 Mon Sep 17 00:00:00 2001 From: Pierre Blanchard Date: Tue, 15 Oct 2024 15:47:06 +0000 Subject: [PATCH] Fix errors reported by cppcheck in src/common/addSuffix.c Error: memleakOnRealloc Fix: Free is realloc fails (and at end of main). --- src/common/addSuffix.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/common/addSuffix.c b/src/common/addSuffix.c index cbfaeb19..3543c16f 100644 --- a/src/common/addSuffix.c +++ b/src/common/addSuffix.c @@ -207,7 +207,18 @@ int main(int argc, char **argv) { nkeywords++; if (nkeywords >= nalloc) { nalloc *= 2; - keywords = realloc(keywords, sizeof(char *) * nalloc); + char ** tmp = realloc(keywords, sizeof(char *) * nalloc); + if (tmp == NULL) { + // free keywords if realloc fails + // otherwise address is lost. + free(keywords); + fclose(fp); + fprintf(stderr, "Failed realloc!\n"); + exit(-1); + } + else { + keywords = tmp; + } } } @@ -228,6 +239,8 @@ int main(int argc, char **argv) { fclose(fp); + free(keywords); + exit(0); }