diff options
author | Guy Harris <guy@alum.mit.edu> | 2018-03-09 17:51:51 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2018-03-10 01:52:51 +0000 |
commit | b8375119e39bebed34344cf1a6953af1532d2b62 (patch) | |
tree | 3830ecda9f5ba5e5cbf43bd622274e2d772ab8be /mmdbresolve.c | |
parent | dcc3875847d691776affae66dcfdb2ccfa5674e7 (diff) |
Avoid the realloc() warning from VS Code Analyzer.
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>
Diffstat (limited to 'mmdbresolve.c')
-rw-r--r-- | mmdbresolve.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/mmdbresolve.c b/mmdbresolve.c index 81fd75022d..5f6edafd9b 100644 --- a/mmdbresolve.c +++ b/mmdbresolve.c @@ -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); @@ -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); |