diff --git a/tools/thirdparty/maxminddb/data-pool.c b/tools/thirdparty/maxminddb/data-pool.c index 314d6cb2f..fb91928b1 100644 --- a/tools/thirdparty/maxminddb/data-pool.c +++ b/tools/thirdparty/maxminddb/data-pool.c @@ -12,7 +12,7 @@ // Allocate an MMDB_data_pool_s. It initially has space for size // MMDB_entry_data_list_s structs. MMDB_data_pool_s *data_pool_new(size_t const size) { - MMDB_data_pool_s* const pool = calloc(1, sizeof(MMDB_data_pool_s)); + MMDB_data_pool_s *const pool = calloc(1, sizeof(MMDB_data_pool_s)); if (!pool) { return NULL; } @@ -28,7 +28,6 @@ MMDB_data_pool_s *data_pool_new(size_t const size) { data_pool_destroy(pool); return NULL; } - memset(pool->blocks[0], 0, pool->size * sizeof(MMDB_entry_data_list_s)); pool->blocks[0]->pool = pool; pool->sizes[0] = size; @@ -96,7 +95,6 @@ MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const pool) { if (!pool->blocks[new_index]) { return NULL; } - memset(pool->blocks[new_index], 0, new_size * sizeof(MMDB_entry_data_list_s)); // We don't need to set this, but it's useful for introspection in tests. pool->blocks[new_index]->pool = pool; @@ -160,9 +158,13 @@ int main(void) { } static void test_can_multiply(void) { - { ok(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok"); } + { + ok(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok"); + } - { ok(!can_multiply(SIZE_MAX, 2, SIZE_MAX), "2*SIZE_MAX is not ok"); } + { + ok(!can_multiply(SIZE_MAX, 2, SIZE_MAX), "2*SIZE_MAX is not ok"); + } { ok(can_multiply(SIZE_MAX, 10240, sizeof(MMDB_entry_data_list_s)), diff --git a/tools/thirdparty/maxminddb/maxminddb-compat-util.h b/tools/thirdparty/maxminddb/maxminddb-compat-util.h index 3e67a483a..586bbce7f 100644 --- a/tools/thirdparty/maxminddb/maxminddb-compat-util.h +++ b/tools/thirdparty/maxminddb/maxminddb-compat-util.h @@ -150,7 +150,7 @@ static char *mmdb_strndup(const char *str, size_t n) { size_t len; char *copy; - len = strnlen(str, n); + len = mmdb_strnlen(str, n); if ((copy = malloc(len + 1)) == NULL) return (NULL); memcpy(copy, str, len); diff --git a/tools/thirdparty/maxminddb/maxminddb.c b/tools/thirdparty/maxminddb/maxminddb.c index 387aa77e4..848dbd082 100644 --- a/tools/thirdparty/maxminddb/maxminddb.c +++ b/tools/thirdparty/maxminddb/maxminddb.c @@ -781,12 +781,14 @@ 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; } @@ -794,6 +796,7 @@ static int populate_languages_metadata(MMDB_s *mmdb, 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 @@ -903,39 +906,53 @@ cleanup: return status; } +#ifdef _WIN32 +#pragma comment(lib, "ntdll.lib") +DECLSPEC_IMPORT NTSTATUS WINAPI RtlIpv6StringToAddressExA(PCSTR AddressString, IN6_ADDR* Address, PULONG ScopeId, PUSHORT Port); +DECLSPEC_IMPORT NTSTATUS WINAPI RtlIpv4StringToAddressExA(PCSTR AddressString, BOOLEAN Strict, IN_ADDR* Address, PUSHORT Port); +#endif + MMDB_lookup_result_s MMDB_lookup_string(const MMDB_s *const mmdb, const char *const ipstr, int *const gai_error, int *const mmdb_error) { MMDB_lookup_result_s result = {.found_entry = false, - .netmask = 0, + .netmask = 0, .entry = {.mmdb = mmdb, .offset = 0}}; - struct addrinfo *addresses = NULL; - *gai_error = resolve_any_address(ipstr, &addresses); + SOCKADDR_IN6 addr6; + SOCKADDR_IN addr4; - if (!*gai_error) { - result = MMDB_lookup_sockaddr(mmdb, addresses->ai_addr, mmdb_error); + memset(&addr6, 0, sizeof(addr6)); + addr6.sin6_family = AF_INET6; + + if (RtlIpv6StringToAddressExA(ipstr, &addr6.sin6_addr, &addr6.sin6_scope_id, &addr6.sin6_port) == 0) + { + return MMDB_lookup_sockaddr(mmdb, (struct sockaddr*)&addr6, mmdb_error); } - if (NULL != addresses) { - freeaddrinfo(addresses); + memset(&addr4, 0, sizeof(addr4)); + addr4.sin_family = AF_INET; + + if (RtlIpv4StringToAddressExA(ipstr, TRUE, &addr4.sin_addr, &addr4.sin_port) == 0) + { + return MMDB_lookup_sockaddr(mmdb, (struct sockaddr*)&addr4, mmdb_error); } return result; } static int resolve_any_address(const char *ipstr, struct addrinfo **addresses) { - struct addrinfo hints = { - .ai_family = AF_UNSPEC, - .ai_flags = AI_NUMERICHOST, - // We set ai_socktype so that we only get one result back - .ai_socktype = SOCK_STREAM}; - - int gai_status = getaddrinfo(ipstr, NULL, &hints, addresses); - if (gai_status) { - return gai_status; - } + //struct addrinfo hints = { + // .ai_family = AF_UNSPEC, + // .ai_flags = AI_NUMERICHOST, + // // We set ai_socktype so that we only get one result back + // .ai_socktype = SOCK_STREAM}; + // + //int gai_status = getaddrinfo(ipstr, NULL, &hints, addresses); + //if (gai_status) { + // return gai_status; + //} return 0; } @@ -984,7 +1001,7 @@ static int find_address_in_search_tree(const MMDB_s *const mmdb, return MMDB_UNKNOWN_DATABASE_FORMAT_ERROR; } - uint32_t value = 0; + uint64_t value = 0; uint16_t current_bit = 0; if (mmdb->metadata.ip_version == 6 && address_family == AF_INET) { value = mmdb->ipv4_start_node.node_value; @@ -998,6 +1015,7 @@ static int find_address_in_search_tree(const MMDB_s *const mmdb, uint8_t bit = 1U & (address[current_bit >> 3] >> (7 - (current_bit % 8))); + // Note that value*record_info.record_length can be larger than 2**32 record_pointer = &search_tree[value * record_info.record_length]; if (record_pointer + record_info.record_length > mmdb->data_section) { return MMDB_CORRUPT_SEARCH_TREE_ERROR; @@ -1669,6 +1687,8 @@ int MMDB_get_metadata_as_entry_data_list( int MMDB_get_entry_data_list(MMDB_entry_s *start, MMDB_entry_data_list_s **const entry_data_list) { + *entry_data_list = NULL; + MMDB_data_pool_s *const pool = data_pool_new(MMDB_POOL_INIT_SIZE); if (!pool) { return MMDB_OUT_OF_MEMORY_ERROR; @@ -1682,6 +1702,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) { @@ -2162,7 +2186,6 @@ static char *bytes_to_hex(uint8_t const *bytes, uint32_t size) { if (NULL == hex_string) { return NULL; } - memset(hex_string, 0, ((size * 2) + 1) * sizeof(char)); for (uint32_t i = 0; i < size; i++) { sprintf(hex_string + (2 * i), "%02X", bytes[i]); diff --git a/tools/thirdparty/maxminddb/maxminddb.h b/tools/thirdparty/maxminddb/maxminddb.h index d486145f1..840622e7a 100644 --- a/tools/thirdparty/maxminddb/maxminddb.h +++ b/tools/thirdparty/maxminddb/maxminddb.h @@ -18,14 +18,11 @@ extern "C" { #include #include /* libmaxminddb package version from configure */ -#define PACKAGE_VERSION "1.9.1" +#define PACKAGE_VERSION "1.12.2" #if defined(_MSC_VER) /* MSVC doesn't define signed size_t, copy it from configure */ #define ssize_t SSIZE_T - -/* MSVC doesn't support restricted pointers */ -#define restrict #endif #else #include