Skip to content

Commit

Permalink
Avoid the realloc() warning from VS Code Analyzer.
Browse files Browse the repository at this point in the history
It's not just worrying about the lack of a check for a null return, it's
worried about the leak.  Assign the result to a different variable and,
if the result is null, free the old data before exiting, and if it's not
null, assign the new variable to the one we're using as a pointer to the
array.

Change-Id: Ia1d5d271293e13708c35a7562a1f40671304c417
Reviewed-on: https://code.wireshark.org/review/26410
Reviewed-by: Guy Harris <guy@alum.mit.edu>
  • Loading branch information
guyharris committed Mar 10, 2018
1 parent dcc3875 commit b837511
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions mmdbresolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ main(int argc, char *argv[])
{
char addr_str[MAX_ADDR_LEN+1];
size_t mmdb_count = 0;
MMDB_s *mmdbs = NULL;
MMDB_s *mmdbs = NULL, *new_mmdbs;
int mmdb_err;

char *out_buf = (char *) malloc(OUT_BUF_SIZE);
Expand All @@ -74,11 +74,13 @@ main(int argc, char *argv[])
fprintf(stdout, "db.%zd.status: ", mmdb_count);
if (mmdb_err == MMDB_SUCCESS) {
mmdb_count++;
mmdbs = (MMDB_s *) realloc(mmdbs, mmdb_count * sizeof(MMDB_s));
if (mmdbs == NULL) {
new_mmdbs = (MMDB_s *) realloc(mmdbs, mmdb_count * sizeof(MMDB_s));
if (new_mmdbs == NULL) {
free(mmdbs);
fprintf(stdout, "ERROR out of memory\n");
return 1;
}
mmdbs = new_mmdbs;
mmdbs[mmdb_count - 1] = try_mmdb;
fprintf(stdout, "OK\n");
fprintf(stdout, "db.%zd.type: %s\n", mmdb_count, mmdbs[mmdb_count - 1].metadata.database_type);
Expand Down

0 comments on commit b837511

Please sign in to comment.