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>
This commit is contained in:
Guy Harris 2018-03-09 17:51:51 -08:00
parent dcc3875847
commit b8375119e3
1 changed files with 5 additions and 3 deletions

View File

@ -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);