From ea773070121c9e0ef55680af4d9b45a8be6bfe1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Thu, 14 Nov 2013 15:28:04 +0200 Subject: [PATCH 2/2] make MMDB_aget_value()'s path argument const correct --- include/maxminddb.h | 2 +- src/maxminddb.c | 49 ++++++++++++++++++------------------------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/include/maxminddb.h b/include/maxminddb.h index 599d4d5..3c6e645 100644 --- a/include/maxminddb.h +++ b/include/maxminddb.h @@ -178,7 +178,7 @@ typedef struct MMDB_search_node_s { extern int MMDB_vget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, va_list va_path); extern int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, - char **path); + const char *const * path); extern int MMDB_get_metadata_as_entry_data_list( MMDB_s *mmdb, MMDB_entry_data_list_s **entry_data_list); extern int MMDB_get_entry_data_list(MMDB_entry_s *start, diff --git a/src/maxminddb.c b/src/maxminddb.c index 3bf7154..fc82c9a 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -132,9 +132,9 @@ LOCAL int populate_result(MMDB_s *mmdb, uint32_t node_count, uint32_t value, uint16_t netmask, MMDB_lookup_result_s *result); LOCAL uint32_t get_left_28_bit_record(const uint8_t *record); LOCAL uint32_t get_right_28_bit_record(const uint8_t *record); -LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb, +LOCAL int lookup_path_in_array(const char *path_elem, MMDB_s *mmdb, MMDB_entry_data_s *entry_data); -LOCAL int lookup_path_in_map(char *path_elem, MMDB_s *mmdb, +LOCAL int lookup_path_in_map(const char *path_elem, MMDB_s *mmdb, MMDB_entry_data_s *entry_data); LOCAL int skip_map_or_array(MMDB_s *mmdb, MMDB_entry_data_s *entry_data); LOCAL int decode_one_follow(MMDB_s *mmdb, uint32_t offset, @@ -402,7 +402,7 @@ LOCAL MMDB_s make_fake_metadata_db(MMDB_s *mmdb) LOCAL uint32_t value_for_key_as_uint16(MMDB_entry_s *start, char *key) { MMDB_entry_data_s entry_data; - char *path[] = { key, NULL }; + const char *path[] = { key, NULL }; MMDB_aget_value(start, &entry_data, path); return entry_data.uint16; } @@ -410,7 +410,7 @@ LOCAL uint32_t value_for_key_as_uint16(MMDB_entry_s *start, char *key) LOCAL uint32_t value_for_key_as_uint32(MMDB_entry_s *start, char *key) { MMDB_entry_data_s entry_data; - char *path[] = { key, NULL }; + const char *path[] = { key, NULL }; MMDB_aget_value(start, &entry_data, path); return entry_data.uint32; } @@ -418,7 +418,7 @@ LOCAL uint32_t value_for_key_as_uint32(MMDB_entry_s *start, char *key) LOCAL uint64_t value_for_key_as_uint64(MMDB_entry_s *start, char *key) { MMDB_entry_data_s entry_data; - char *path[] = { key, NULL }; + const char *path[] = { key, NULL }; MMDB_aget_value(start, &entry_data, path); return entry_data.uint64; } @@ -426,7 +426,7 @@ LOCAL uint64_t value_for_key_as_uint64(MMDB_entry_s *start, char *key) LOCAL char *value_for_key_as_string(MMDB_entry_s *start, char *key) { MMDB_entry_data_s entry_data; - char *path[] = { key, NULL }; + const char *path[] = { key, NULL }; MMDB_aget_value(start, &entry_data, path); return strndup((char *)entry_data.utf8_string, entry_data.data_size); } @@ -436,7 +436,7 @@ LOCAL int populate_languages_metadata(MMDB_s *mmdb, MMDB_s *metadata_db, { MMDB_entry_data_s entry_data; - char *path[] = { "languages", NULL }; + const char *path[] = { "languages", NULL }; MMDB_aget_value(metadata_start, &entry_data, path); if (MMDB_DATA_TYPE_ARRAY != entry_data.type) { @@ -487,7 +487,7 @@ LOCAL int populate_description_metadata(MMDB_s *mmdb, MMDB_s *metadata_db, { MMDB_entry_data_s entry_data; - char *path[] = { "description", NULL }; + const char *path[] = { "description", NULL }; MMDB_aget_value(metadata_start, &entry_data, path); if (MMDB_DATA_TYPE_MAP != entry_data.type) { @@ -831,43 +831,30 @@ int MMDB_get_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, ...) int MMDB_vget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, va_list va_path) { - char **path = NULL; - + const char **path = NULL; + const char *path_elem; int i = 0; - char *path_elem; - while (NULL != (path_elem = va_arg(va_path, char *))) { - path = realloc(path, sizeof(char *) * (i + 1)); + + while (NULL != (path_elem = va_arg(va_path, const char *))) { + path = realloc(path, sizeof(const char *) * (i + 2)); if (NULL == path) { return MMDB_OUT_OF_MEMORY_ERROR; } - path[i] = strdup(path_elem); - if (NULL == path[i]) { - return MMDB_OUT_OF_MEMORY_ERROR; - } + path[i] = path_elem; i++; } - - path = realloc(path, sizeof(char *) * (i + 1)); - if (NULL == path) { - return MMDB_OUT_OF_MEMORY_ERROR; - } path[i] = NULL; int status = MMDB_aget_value(start, entry_data, path); - i = 0; - char *elem; - while (NULL != (elem = path[i++])) { - free(elem); - } free(path); return status; } int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, - char **path) + const char *const * path) { MMDB_s *mmdb = start->mmdb; uint32_t offset = start->offset; @@ -888,7 +875,7 @@ int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, return MMDB_INVALID_LOOKUP_PATH_ERROR; } - char *path_elem; + const char *path_elem; while (NULL != (path_elem = *(path++))) { DEBUG_NL; DEBUG_MSGF("path elem = %s", path_elem); @@ -921,7 +908,7 @@ int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, return MMDB_SUCCESS; } -LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb, +LOCAL int lookup_path_in_array(const char *path_elem, MMDB_s *mmdb, MMDB_entry_data_s *entry_data) { uint32_t size = entry_data->data_size; @@ -952,7 +939,7 @@ LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb, return MMDB_SUCCESS; } -LOCAL int lookup_path_in_map(char *path_elem, MMDB_s *mmdb, +LOCAL int lookup_path_in_map(const char *path_elem, MMDB_s *mmdb, MMDB_entry_data_s *entry_data) { uint32_t size = entry_data->data_size; -- 1.8.4.1