Skip to content

Commit

Permalink
Merge pull request #356 from pkillarjun/memory-leak
Browse files Browse the repository at this point in the history
fix: memory leak in MMDB_open()
  • Loading branch information
horgh authored Nov 8, 2024
2 parents d308c75 + 69af4e0 commit 70abedb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/maxminddb.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,19 +744,22 @@ static int populate_languages_metadata(MMDB_s *mmdb,
mmdb->metadata.languages.count = 0;
mmdb->metadata.languages.names = calloc(array_size, sizeof(char *));
if (NULL == mmdb->metadata.languages.names) {
MMDB_free_entry_data_list(first_member);
return MMDB_OUT_OF_MEMORY_ERROR;
}

for (uint32_t i = 0; i < array_size; i++) {
member = member->next;
if (MMDB_DATA_TYPE_UTF8_STRING != member->entry_data.type) {
MMDB_free_entry_data_list(first_member);
return MMDB_INVALID_METADATA_ERROR;
}

mmdb->metadata.languages.names[i] = mmdb_strndup(
member->entry_data.utf8_string, member->entry_data.data_size);

if (NULL == mmdb->metadata.languages.names[i]) {
MMDB_free_entry_data_list(first_member);
return MMDB_OUT_OF_MEMORY_ERROR;
}
// We assign this as we go so that if we fail a calloc and need to
Expand Down Expand Up @@ -1646,6 +1649,10 @@ int MMDB_get_entry_data_list(MMDB_entry_s *start,

int const status =
get_entry_data_list(start->mmdb, start->offset, list, pool, 0);
if (MMDB_SUCCESS != status) {
data_pool_destroy(pool);
return status;
}

*entry_data_list = data_pool_to_list(pool);
if (!*entry_data_list) {
Expand Down
2 changes: 0 additions & 2 deletions t/bad_pointers_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ void run_tests(int mode, const char *mode_desc) {
MMDB_INVALID_DATA_ERROR,
"MMDB_get_entry_data_list returns MMDB_INVALID_DATA_ERROR for "
"bad pointer in data section");

MMDB_free_entry_data_list(entry_data_list);
}

{
Expand Down

0 comments on commit 70abedb

Please sign in to comment.