From b8375119e39bebed34344cf1a6953af1532d2b62 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Fri, 9 Mar 2018 17:51:51 -0800 Subject: [PATCH] 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 --- mmdbresolve.c | 8 +++++--- 1 file 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);