Skip to content

Commit

Permalink
Fix errors reported by cppcheck in src/common/addSuffix.c
Browse files Browse the repository at this point in the history
Error: memleakOnRealloc
Fix: Free is realloc fails (and at end of main).
  • Loading branch information
blapie committed Oct 18, 2024
1 parent d112ebc commit 8e8aabe
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/common/addSuffix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand All @@ -228,6 +239,8 @@ int main(int argc, char **argv) {

fclose(fp);

free(keywords);

exit(0);
}

Expand Down

0 comments on commit 8e8aabe

Please sign in to comment.