From e2dfabdfb890a22815f8ef76cb90715a5da4df95 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 30 Oct 2026 19:38:40 +0000 Subject: [PATCH 01/17] Reject repeated metadata keys MMDB_open() looked up each metadata key with a first-match map scan, so a repeated key had only its first value read and validated. Readers that keep the last value could see different metadata for the same file. MMDB_open() now walks the top-level metadata map once and sorts its keys. It sorts the description languages after it stores them. Keys are compared up to the first NUL, because some callers cut keys there. If any key repeats, MMDB_open() returns MMDB_INVALID_METADATA_ERROR. Sorting keeps the check fast for a crafted map with many distinct keys. The walk decodes every top-level key and skips its value without following pointers. A malformed entry after the required keys, which MMDB_open() never read before, now makes it fail. The test builds small databases in memory, writes each to a unique file in TMPDIR, and removes it after MMDB_open(). Co-Authored-By: Claude Opus 5.5 --- Changes.md | 17 +++ doc/libmaxminddb.md | 8 + src/maxminddb.c | 154 ++++++++++++++++++- t/CMakeLists.txt | 1 + t/Makefile.am | 3 +- t/duplicate_metadata_key_t.c | 276 +++++++++++++++++++++++++++++++++++ 6 files changed, 457 insertions(+), 2 deletions(-) create mode 100644 t/duplicate_metadata_key_t.c diff --git a/Changes.md b/Changes.md index d766fb5d..daaf60c3 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,20 @@ +## next release + +- `MMDB_open()` now returns `MMDB_INVALID_METADATA_ERROR` if the top-level + metadata map or the `description` map repeats a key. Before, top-level + metadata lookups used only the first value of a repeated key, and the + `description` list kept every pair, while other readers may keep the last + value. Keys are compared up to the first NUL byte, so keys that differ only + after a NUL count as repeated. Databases with repeated metadata keys no longer + open. +- `MMDB_open()` now reads every top-level metadata key, not only the required + keys, and skips each value without following pointers in it. A malformed key + or value, or a map that claims more entries than it has, now causes + `MMDB_INVALID_DATA_ERROR`. A value nested deeper than the depth limit, or keys + whose total size exceeds the payload limit, causes + `MMDB_INVALID_METADATA_ERROR`. Some of these databases opened before. Metadata + whose root is not a map now causes `MMDB_INVALID_METADATA_ERROR`. + ## 1.14.1 - 2026-09-17 - Fixed a `-Wsign-compare` warning in `MMDB_open()` that broke builds with diff --git a/doc/libmaxminddb.md b/doc/libmaxminddb.md index 092bcff7..4d74bf3d 100644 --- a/doc/libmaxminddb.md +++ b/doc/libmaxminddb.md @@ -459,6 +459,14 @@ If metadata processing exceeds one of the decoder resource limits described under `MMDB_get_entry_data_list()`, this function returns `MMDB_INVALID_METADATA_ERROR`. +If the metadata root is not a map, or the top-level metadata map or the +`description` map repeats a key, this function returns +`MMDB_INVALID_METADATA_ERROR`. + +If a top-level metadata key or value is malformed, this function returns +`MMDB_INVALID_DATA_ERROR`. It does not follow pointers in values that it does +not use, so it does not detect a bad pointer target there. + ## `MMDB_close()` ```c diff --git a/src/maxminddb.c b/src/maxminddb.c index 95ca7a7d..4f1205ea 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -178,6 +178,11 @@ typedef struct decode_state_s { uint64_t bytes; } decode_state_s; +typedef struct metadata_key_s { + const char *name; + uint32_t size; +} metadata_key_s; + #define METADATA_MARKER "\xab\xcd\xefMaxMind.com" /* This is 128kb */ #define METADATA_BLOCK_MAX_SIZE 131072 @@ -191,6 +196,12 @@ static const uint8_t *find_metadata(const uint8_t *file_content, uint32_t *metadata_size); static int read_metadata(MMDB_s *mmdb); static MMDB_s make_fake_metadata_db(const MMDB_s *const mmdb); +static int check_metadata_keys_unique(const MMDB_s *const metadata_db); +static int next_map_key(const MMDB_s *const mmdb, + uint32_t *offset, + MMDB_entry_data_s *key); +static int check_keys_unique(metadata_key_s *keys, uint32_t count); +static int compare_metadata_keys(const void *a, const void *b); static int value_for_key_as_uint16(MMDB_entry_s *start, char *key, uint16_t *value); static int @@ -205,6 +216,8 @@ static int populate_languages_metadata(MMDB_s *mmdb, static int populate_description_metadata(MMDB_s *mmdb, MMDB_s *metadata_db, MMDB_entry_s *metadata_start); +static int check_languages_unique(MMDB_description_s **descriptions, + size_t count); static int resolve_any_address(const char *ipstr, struct addrinfo **addresses); static int find_address_in_search_tree(const MMDB_s *const mmdb, uint8_t const *address, @@ -590,9 +603,16 @@ static int read_metadata(MMDB_s *mmdb) { metadata values. */ MMDB_s metadata_db = make_fake_metadata_db(mmdb); + // Key lookups return the first match, but other readers can keep the + // last one. A repeated key could give each reader a different value. + int status = check_metadata_keys_unique(&metadata_db); + if (MMDB_SUCCESS != status) { + return status; + } + MMDB_entry_s metadata_start = {.mmdb = &metadata_db, .offset = 0}; - int status = value_for_key_as_uint32( + status = value_for_key_as_uint32( &metadata_start, "node_count", &mmdb->metadata.node_count); if (MMDB_SUCCESS != status) { return status; @@ -699,6 +719,115 @@ static MMDB_s make_fake_metadata_db(const MMDB_s *const mmdb) { return fake_metadata_db; } +static int check_metadata_keys_unique(const MMDB_s *const metadata_db) { + MMDB_entry_data_s map; + CHECKED_DECODE_ONE_FOLLOW(metadata_db, 0, &map); + if (MMDB_DATA_TYPE_MAP != map.type) { + DEBUG_MSG("metadata value is not a map"); + return MMDB_INVALID_METADATA_ERROR; + } + if (map.data_size < 2) { + return MMDB_SUCCESS; + } + // Each entry takes at least a key byte and a value byte. Reject a larger + // count before it sizes the allocation, with the error the walk would + // return when it reaches the end of the section. + if (map.offset_to_next > metadata_db->data_section_size || + map.data_size > + (metadata_db->data_section_size - map.offset_to_next) / 2) { + DEBUG_MSG("metadata map size exceeds the metadata section"); + return MMDB_INVALID_DATA_ERROR; + } + + metadata_key_s *keys = calloc(map.data_size, sizeof(metadata_key_s)); + if (NULL == keys) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + + int status = MMDB_SUCCESS; + uint32_t offset = map.offset_to_next; + uint64_t key_bytes = 0; + for (uint32_t i = 0; i < map.data_size; i++) { + MMDB_entry_data_s key; + status = next_map_key(metadata_db, &offset, &key); + if (MMDB_SUCCESS != status) { + goto cleanup; + } + // Pointer keys can reuse one long string many times. Charge each key + // against the payload budget to bound the scan and the sort below. + key_bytes += key.data_size; + if (key_bytes > (uint64_t)(MAXIMUM_DATA_STRUCTURE_BYTES)) { + DEBUG_MSG("metadata keys exceed the payload budget"); + status = MMDB_DECODER_LIMIT_ERROR; + goto cleanup; + } + // Compare up to the first NUL, as check_languages_unique does. Some + // callers, such as the PHP extension, cut keys there. + const char *nul = memchr(key.utf8_string, '\0', key.data_size); + keys[i].name = key.utf8_string; + if (NULL == nul) { + keys[i].size = key.data_size; + } else { + keys[i].size = (uint32_t)(nul - key.utf8_string); + } + } + + status = check_keys_unique(keys, map.data_size); + +cleanup: + free(keys); + return status; +} + +/* Decodes the map key at *offset, skips its value, and sets *offset to the + * next key. */ +static int next_map_key(const MMDB_s *const mmdb, + uint32_t *offset, + MMDB_entry_data_s *key) { + CHECKED_DECODE_ONE_FOLLOW(mmdb, *offset, key); + if (MMDB_DATA_TYPE_UTF8_STRING != key->type) { + return MMDB_INVALID_DATA_ERROR; + } + + MMDB_entry_data_s value; + CHECKED_DECODE_ONE(mmdb, key->offset_to_next, &value); + int status = skip_map_or_array(mmdb, &value, 0); + if (MMDB_SUCCESS != status) { + return status; + } + *offset = value.offset_to_next; + + return MMDB_SUCCESS; +} + +/* Sorts keys and returns MMDB_INVALID_METADATA_ERROR if two are equal. */ +static int check_keys_unique(metadata_key_s *keys, uint32_t count) { + qsort(keys, count, sizeof(metadata_key_s), compare_metadata_keys); + for (uint32_t i = 1; i < count; i++) { + if (0 == compare_metadata_keys(&keys[i - 1], &keys[i])) { + DEBUG_MSG("duplicate key in metadata"); + return MMDB_INVALID_METADATA_ERROR; + } + } + return MMDB_SUCCESS; +} + +static int compare_metadata_keys(const void *a, const void *b) { + const metadata_key_s *key_a = a; + const metadata_key_s *key_b = b; + if (key_a->size < key_b->size) { + return -1; + } + if (key_a->size > key_b->size) { + return 1; + } + // Pointer keys can share one long string. Skip comparing it to itself. + if (key_a->name == key_b->name) { + return 0; + } + return memcmp(key_a->name, key_b->name, key_a->size); +} + static int value_for_key_as_uint16(MMDB_entry_s *start, char *key, uint16_t *value) { MMDB_entry_data_s entry_data; @@ -925,12 +1054,35 @@ static int populate_description_metadata(MMDB_s *mmdb, } } + // Compare the languages as stored, so keys that differ only after a NUL + // count as repeated. + status = check_languages_unique(mmdb->metadata.description.descriptions, + mmdb->metadata.description.count); + cleanup: MMDB_free_entry_data_list(first_member); return status; } +static int check_languages_unique(MMDB_description_s **descriptions, + size_t count) { + if (count < 2) { + return MMDB_SUCCESS; + } + metadata_key_s *keys = calloc(count, sizeof(metadata_key_s)); + if (NULL == keys) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + for (size_t i = 0; i < count; i++) { + keys[i].name = descriptions[i]->language; + keys[i].size = (uint32_t)strlen(keys[i].name); + } + int status = check_keys_unique(keys, (uint32_t)count); + free(keys); + return status; +} + MMDB_lookup_result_s MMDB_lookup_string(const MMDB_s *const mmdb, const char *const ipstr, int *const gai_error, diff --git a/t/CMakeLists.txt b/t/CMakeLists.txt index bb9c23c7..61c51b60 100644 --- a/t/CMakeLists.txt +++ b/t/CMakeLists.txt @@ -33,6 +33,7 @@ if(UNIX) # or if (NOT WIN32) bad_data_size_t bad_epoch_t bad_indent_t + duplicate_metadata_key_t empty_container_metadata_t invalid_sockaddr_t max_depth_t diff --git a/t/Makefile.am b/t/Makefile.am index f2c01aca..903771f0 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -20,7 +20,8 @@ check_PROGRAMS = \ bad_pointers_t bad_databases_t bad_data_size_t bad_epoch_t bad_indent_t \ bad_search_tree_t \ basic_lookup_t data_entry_list_t \ - data-pool-t data_types_t double_close_t dump_t empty_container_metadata_t \ + data-pool-t data_types_t double_close_t dump_t duplicate_metadata_key_t \ + empty_container_metadata_t \ gai_error_t get_value_t \ get_value_pointer_bug_t invalid_sockaddr_t \ ipv4_start_cache_t ipv6_lookup_in_ipv4_t max_depth_t metadata_t \ diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c new file mode 100644 index 00000000..2f37a7cd --- /dev/null +++ b/t/duplicate_metadata_key_t.c @@ -0,0 +1,276 @@ +#define _XOPEN_SOURCE 700 + +#include "maxminddb_test_helper.h" + +#include + +// One IPv4 node whose 24-bit records both mark "no data", the data section +// separator, and the metadata marker. +#define DB_PREFIX \ + "\x00\x00\x01\x00\x00\x01" \ + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \ + "\xAB\xCD\xEF" \ + "MaxMind.com" + +// The metadata keys that MMDB_open requires, except description, without the +// map header. database_type starts at metadata offset 0x29. +#define REQUIRED_KEYS_BEFORE_DESCRIPTION \ + "\x4A" \ + "node_count" \ + "\xC1\x01" \ + "\x4B" \ + "record_size" \ + "\xA1\x18" \ + "\x4A" \ + "ip_version" \ + "\xA1\x04" \ + "\x4D" \ + "database_type" \ + "\x44" \ + "Test" \ + "\x49" \ + "languages" \ + "\x00\x04" \ + "\x5B" \ + "binary_format_major_version" \ + "\xA1\x02" \ + "\x5B" \ + "binary_format_minor_version" \ + "\xA0" \ + "\x4B" \ + "build_epoch" \ + "\x01\x02\x01" + +// The nine metadata keys that MMDB_open requires, without the map header. +#define REQUIRED_METADATA_KEYS \ + REQUIRED_KEYS_BEFORE_DESCRIPTION \ + "\x4B" \ + "description" \ + "\xE0" + +#define TEST_OPEN(db, expected, message) \ + test_open(db, sizeof(db) - 1, expected, message) + +static void +test_open(const char *db, size_t size, int expected, const char *message) { + const char *tmpdir = getenv("TMPDIR"); + if (NULL == tmpdir || '\0' == tmpdir[0]) { + tmpdir = "/tmp"; + } + char filename[4096]; + int length = snprintf(filename, + sizeof(filename), + "%s/duplicate_metadata_key_t_XXXXXX", + tmpdir); + if (length < 0 || (size_t)length >= sizeof(filename)) { + BAIL_OUT("TMPDIR is too long"); + } + int fd = mkstemp(filename); + if (-1 == fd) { + BAIL_OUT("could not create a temporary file in %s", tmpdir); + } + FILE *fp = fdopen(fd, "wb"); + if (NULL == fp) { + BAIL_OUT("could not open %s for writing", filename); + } + size_t written = fwrite(db, 1, size, fp); + if (0 != fclose(fp) || written != size) { + BAIL_OUT("could not write %s", filename); + } + + MMDB_s mmdb; + int status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb); + cmp_ok(status, "==", expected, message); + if (MMDB_SUCCESS == status) { + MMDB_close(&mmdb); + } + + if (0 != remove(filename)) { + BAIL_OUT("could not remove %s", filename); + } +} + +// An extra key whose value is 600 nested one-element arrays, deeper than the +// default MAXIMUM_DATA_STRUCTURE_DEPTH of 512. +static void test_deep_extra_value(void) { + const char head[] = DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x45" + "extra"; + const size_t head_size = sizeof(head) - 1; + const size_t depth = 600; + size_t size = head_size + depth * 2 + 1; + char *db = malloc(size); + if (NULL == db) { + BAIL_OUT("could not allocate the deep test database"); + } + memcpy(db, head, head_size); + for (size_t i = 0; i < depth; i++) { + db[head_size + i * 2] = '\x01'; + db[head_size + i * 2 + 1] = '\x04'; + } + db[size - 1] = '\xA0'; + + test_open(db, + size, + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects an extra value nested past the depth limit"); + free(db); +} + +// Pointer keys into a run of 0x5E bytes. The key at run offset j is a +// 24,443-byte string that starts with 100 - j bytes of 0x5E, so the keys are +// distinct, but together they exceed the 2 MiB payload budget. +static void test_long_pointer_keys(void) { + const size_t key_count = 100; + const size_t run = key_count + 3; + const size_t key_size = 285 + 0x5E5E; + const size_t blob_size = run + key_size; + const char head[] = DB_PREFIX "\xFD\x51" REQUIRED_METADATA_KEYS "\x44" + "blob" + "\x5E"; + const size_t head_size = sizeof(head) - 1; + const size_t prefix_size = sizeof(DB_PREFIX) - 1; + // The metadata offset of the blob content, after its 3-byte header. + const size_t blob_offset = head_size + 2 - prefix_size; + + size_t size = head_size + 2 + blob_size + key_count * 3; + char *db = malloc(size); + if (NULL == db) { + BAIL_OUT("could not allocate the long-key test database"); + } + memcpy(db, head, head_size); + size_t pos = head_size; + db[pos++] = (char)((blob_size - 285) >> 8); + db[pos++] = (char)((blob_size - 285) & 0xFF); + memset(db + pos, 0x5E, run); + memset(db + pos + run, 'a', key_size); + pos += blob_size; + for (size_t j = 0; j < key_count; j++) { + size_t target = blob_offset + j; + db[pos++] = (char)(0x20 | (target >> 8)); + db[pos++] = (char)(target & 0xFF); + db[pos++] = '\xA0'; + } + + test_open(db, + size, + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects metadata keys past the payload budget"); + free(db); +} + +int main(void) { + plan(NO_PLAN); + + TEST_OPEN(DB_PREFIX "\xE9" REQUIRED_METADATA_KEYS, + MMDB_SUCCESS, + "MMDB_open accepts metadata without repeated keys"); + + TEST_OPEN(DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x45" + "extra" + "\xA1\x05", + MMDB_SUCCESS, + "MMDB_open accepts an extra distinct metadata key"); + + // The key points at the database_type value, "Test", at offset 0x37. + TEST_OPEN(DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x20\x37" + "\xA1\x05", + MMDB_SUCCESS, + "MMDB_open accepts a distinct key reached through a pointer"); + + TEST_OPEN(DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x4D" + "database_type" + "\xA1\x05", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects a repeated database_type key"); + + TEST_OPEN(DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x4B" + "description" + "\xE0", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects a repeated description key"); + + TEST_OPEN(DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x20\x29" + "\xA1\x05", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects a repeated key reached through a pointer"); + + TEST_OPEN(DB_PREFIX "\xE9" REQUIRED_KEYS_BEFORE_DESCRIPTION "\x4B" + "description" + "\xE2" + "\x42" + "en" + "\x41" + "A" + "\x42" + "en" + "\x41" + "B", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects a repeated key in the description map"); + + TEST_OPEN(DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x4F" + "database_type\x00" + "x" + "\x44" + "Evil", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects top-level keys that differ only after a NUL"); + + TEST_OPEN(DB_PREFIX "\xE9" REQUIRED_KEYS_BEFORE_DESCRIPTION "\x4B" + "description" + "\xE2" + "\x42" + "en" + "\x41" + "A" + "\x44" + "en\x00" + "x" + "\x41" + "B", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects description keys that differ only after a " + "NUL"); + + TEST_OPEN(DB_PREFIX "\xE9" REQUIRED_KEYS_BEFORE_DESCRIPTION "\x4B" + "description" + "\xE2" + "\x42" + "en" + "\x41" + "A" + "\xA1\x01" + "\x41" + "B", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects a non-string key in the description map"); + + TEST_OPEN(DB_PREFIX "\xEB" REQUIRED_METADATA_KEYS, + MMDB_INVALID_DATA_ERROR, + "MMDB_open rejects a metadata map count larger than its entries"); + + TEST_OPEN(DB_PREFIX "\xFE\x00\x00" REQUIRED_METADATA_KEYS, + MMDB_INVALID_DATA_ERROR, + "MMDB_open rejects a metadata map count larger than the section"); + + TEST_OPEN(DB_PREFIX "\xA1\x05", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects metadata whose root is not a map"); + + TEST_OPEN(DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\xA1\x05" + "\xA1\x05", + MMDB_INVALID_DATA_ERROR, + "MMDB_open rejects a non-string key after the required keys"); + + TEST_OPEN(DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x45" + "extra" + "\x44" + "ab", + MMDB_INVALID_DATA_ERROR, + "MMDB_open rejects a truncated value after the required keys"); + + test_deep_extra_value(); + test_long_pointer_keys(); + + done_testing(); +} From 9a1daa371253a68dbe0ee79b451bf8461e7b2c4f Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 21:50:15 +0000 Subject: [PATCH 02/17] Check the MaxMind-DB repeated-key databases Update t/maxmind-db to the commit that adds libmaxminddb-repeated-metadata-key.mmdb and libmaxminddb-repeated-description-key.mmdb, and check that MMDB_open returns MMDB_INVALID_METADATA_ERROR for both. On main, both open. The submodule commit is on the branch for maxmind/MaxMind-DB#314. Co-Authored-By: Claude Opus 5.5 --- t/duplicate_metadata_key_t.c | 18 ++++++++++++++++++ t/maxmind-db | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c index 2f37a7cd..4c07329d 100644 --- a/t/duplicate_metadata_key_t.c +++ b/t/duplicate_metadata_key_t.c @@ -158,6 +158,21 @@ static void test_long_pointer_keys(void) { free(db); } +static void test_bad_data_file(const char *filename) { + char *path = bad_database_path(filename); + MMDB_s mmdb; + int status = MMDB_open(path, MMDB_MODE_MMAP, &mmdb); + cmp_ok(status, + "==", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects %s", + filename); + if (MMDB_SUCCESS == status) { + MMDB_close(&mmdb); + } + free(path); +} + int main(void) { plan(NO_PLAN); @@ -272,5 +287,8 @@ int main(void) { test_deep_extra_value(); test_long_pointer_keys(); + test_bad_data_file("libmaxminddb-repeated-metadata-key.mmdb"); + test_bad_data_file("libmaxminddb-repeated-description-key.mmdb"); + done_testing(); } diff --git a/t/maxmind-db b/t/maxmind-db index 1bc9af96..2e517d62 160000 --- a/t/maxmind-db +++ b/t/maxmind-db @@ -1 +1 @@ -Subproject commit 1bc9af960e6c57cbf5e77aa53a62d72740f21fde +Subproject commit 2e517d62c64ed69bac85b26b58f01006e9cd4fd4 From de11e2a646d87148b5ab0dc04db15dd43dc72fac Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 22:11:41 +0000 Subject: [PATCH 03/17] Reject a map or array key when skipping a map skip_map_or_array decoded each key but skipped only its header. A map or array key put the walk out of step, so its contents were read as the keys and values that followed. MMDB_get_value could then return the wrong value. In the metadata walk, a value of {["x"]: "fake"} before a repeated database_type made the walk read "fake" as a top-level key and miss the repeat. The spec requires string keys, so skip_map_or_array now returns MMDB_INVALID_DATA_ERROR for a map or array key. Scalar keys still skip as before, so MMDB_open keeps returning MMDB_INVALID_METADATA_ERROR for a non-string scalar key in the description map. A lookup benchmark that skips nested maps on GeoIP2-City-Test.mmdb was 0.5% slower, with default and with fixed function alignment. Co-Authored-By: Claude Opus 5.5 --- Changes.md | 5 +++++ src/maxminddb.c | 8 ++++++++ t/duplicate_metadata_key_t.c | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/Changes.md b/Changes.md index daaf60c3..9ff7de45 100644 --- a/Changes.md +++ b/Changes.md @@ -14,6 +14,11 @@ whose total size exceeds the payload limit, causes `MMDB_INVALID_METADATA_ERROR`. Some of these databases opened before. Metadata whose root is not a map now causes `MMDB_INVALID_METADATA_ERROR`. +- `MMDB_get_value()` and its variants, and `MMDB_open()`, now return + `MMDB_INVALID_DATA_ERROR` when they skip a nested map whose key is a map or an + array. Before, they skipped only the key's header and read the data after it + out of step. A lookup could return the wrong value, and a repeated metadata + key could go undetected. ## 1.14.1 - 2026-09-17 diff --git a/src/maxminddb.c b/src/maxminddb.c index 4f1205ea..a8f68b57 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -1584,6 +1584,14 @@ static int skip_map_or_array(const MMDB_s *const mmdb, while (size-- > 0) { CHECKED_DECODE_ONE( mmdb, entry_data->offset_to_next, entry_data); // key + // Keys must be strings. This loop skips only a key's header, so + // the contents of a map or array key would be read as the keys and + // values that follow it. + if (MMDB_DATA_TYPE_MAP == entry_data->type || + MMDB_DATA_TYPE_ARRAY == entry_data->type) { + DEBUG_MSG("map key is a map or an array"); + return MMDB_INVALID_DATA_ERROR; + } CHECKED_DECODE_ONE( mmdb, entry_data->offset_to_next, entry_data); // value int status = skip_map_or_array(mmdb, entry_data, depth + 1); diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c index 4c07329d..60e76384 100644 --- a/t/duplicate_metadata_key_t.c +++ b/t/duplicate_metadata_key_t.c @@ -284,6 +284,24 @@ int main(void) { MMDB_INVALID_DATA_ERROR, "MMDB_open rejects a truncated value after the required keys"); + // The extra value is {["x"]: "fake"}. Skipping only the header of the + // array key would make the walk read "fake" as a top-level key and miss + // the repeated database_type. + TEST_OPEN(DB_PREFIX "\xEB" REQUIRED_METADATA_KEYS "\x45" + "extra" + "\xE1" + "\x01\x04" + "\x41" + "x" + "\x44" + "fake" + "\x4D" + "database_type" + "\x44" + "Evil", + MMDB_INVALID_DATA_ERROR, + "MMDB_open rejects a nested map key that is an array"); + test_deep_extra_value(); test_long_pointer_keys(); From a7f533434e9beb0610bf0a971c6b2df72b380a0c Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 23:15:47 +0000 Subject: [PATCH 04/17] Walk a one-entry metadata map too The walk returned early for fewer than 2 entries, so it did not read a one-entry map, although the changelog says it reads every top-level key. It now returns early only for an empty map. The required-key lookups already reported the same errors for a one-entry map, so no result changes. Co-Authored-By: Claude Opus 5.5 --- src/maxminddb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/maxminddb.c b/src/maxminddb.c index a8f68b57..1ce3568c 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -726,7 +726,7 @@ static int check_metadata_keys_unique(const MMDB_s *const metadata_db) { DEBUG_MSG("metadata value is not a map"); return MMDB_INVALID_METADATA_ERROR; } - if (map.data_size < 2) { + if (0 == map.data_size) { return MMDB_SUCCESS; } // Each entry takes at least a key byte and a value byte. Reject a larger From d039cb1060a008179bf28a9774ff1e28b7e290d0 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 23:15:48 +0000 Subject: [PATCH 05/17] Drop the pointer-equality shortcut in the key comparison The key-bytes budget already bounds the total memcmp work, so the shortcut no longer guards anything. All crafted cases still take under 1 ms. Co-Authored-By: Claude Opus 5.5 --- src/maxminddb.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/maxminddb.c b/src/maxminddb.c index 1ce3568c..8dc32ca0 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -821,10 +821,6 @@ static int compare_metadata_keys(const void *a, const void *b) { if (key_a->size > key_b->size) { return 1; } - // Pointer keys can share one long string. Skip comparing it to itself. - if (key_a->name == key_b->name) { - return 0; - } return memcmp(key_a->name, key_b->name, key_a->size); } From f0416d128c24287d76aed53c2ca3d779ceeb0c3a Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 23:16:32 +0000 Subject: [PATCH 06/17] Cap the metadata and description maps at 1,024 keys C does not bound the cost of qsort. Glibc uses a merge sort, but a quicksort-based qsort, such as some BSD or MSVC versions, can take quadratic time on crafted input. About 32K short keys could then take seconds per MMDB_open. Real metadata has about 10 top-level keys and a few description languages, so a map with more than 1,024 keys now returns MMDB_INVALID_METADATA_ERROR. Co-Authored-By: Claude Opus 5.5 --- Changes.md | 5 +-- doc/libmaxminddb.md | 4 +-- src/maxminddb.c | 11 ++++++ t/duplicate_metadata_key_t.c | 65 ++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/Changes.md b/Changes.md index 9ff7de45..20ab98ab 100644 --- a/Changes.md +++ b/Changes.md @@ -5,8 +5,9 @@ metadata lookups used only the first value of a repeated key, and the `description` list kept every pair, while other readers may keep the last value. Keys are compared up to the first NUL byte, so keys that differ only - after a NUL count as repeated. Databases with repeated metadata keys no longer - open. + after a NUL count as repeated. A top-level metadata map or `description` map + with more than 1,024 keys also causes `MMDB_INVALID_METADATA_ERROR`. Databases + with repeated metadata keys no longer open. - `MMDB_open()` now reads every top-level metadata key, not only the required keys, and skips each value without following pointers in it. A malformed key or value, or a map that claims more entries than it has, now causes diff --git a/doc/libmaxminddb.md b/doc/libmaxminddb.md index 4d74bf3d..140cea5d 100644 --- a/doc/libmaxminddb.md +++ b/doc/libmaxminddb.md @@ -460,8 +460,8 @@ under `MMDB_get_entry_data_list()`, this function returns `MMDB_INVALID_METADATA_ERROR`. If the metadata root is not a map, or the top-level metadata map or the -`description` map repeats a key, this function returns -`MMDB_INVALID_METADATA_ERROR`. +`description` map repeats a key or has more than 1,024 keys, this function +returns `MMDB_INVALID_METADATA_ERROR`. If a top-level metadata key or value is malformed, this function returns `MMDB_INVALID_DATA_ERROR`. It does not follow pointers in values that it does diff --git a/src/maxminddb.c b/src/maxminddb.c index 8dc32ca0..cdef4a91 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -186,6 +186,9 @@ typedef struct metadata_key_s { #define METADATA_MARKER "\xab\xcd\xefMaxMind.com" /* This is 128kb */ #define METADATA_BLOCK_MAX_SIZE 131072 +/* Real metadata has about 10 top-level keys and a few description languages. + * The cap bounds the key sort, because C does not bound the cost of qsort. */ +#define METADATA_MAP_MAX_KEYS 1024 // 64 leads us to allocating 4 KiB on a 64bit system. #define MMDB_POOL_INIT_SIZE 64 @@ -738,6 +741,10 @@ static int check_metadata_keys_unique(const MMDB_s *const metadata_db) { DEBUG_MSG("metadata map size exceeds the metadata section"); return MMDB_INVALID_DATA_ERROR; } + if (map.data_size > METADATA_MAP_MAX_KEYS) { + DEBUG_MSG("metadata map has too many keys"); + return MMDB_INVALID_METADATA_ERROR; + } metadata_key_s *keys = calloc(map.data_size, sizeof(metadata_key_s)); if (NULL == keys) { @@ -1066,6 +1073,10 @@ static int check_languages_unique(MMDB_description_s **descriptions, if (count < 2) { return MMDB_SUCCESS; } + if (count > METADATA_MAP_MAX_KEYS) { + DEBUG_MSG("description map has too many keys"); + return MMDB_INVALID_METADATA_ERROR; + } metadata_key_s *keys = calloc(count, sizeof(metadata_key_s)); if (NULL == keys) { return MMDB_OUT_OF_MEMORY_ERROR; diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c index 60e76384..f566dd44 100644 --- a/t/duplicate_metadata_key_t.c +++ b/t/duplicate_metadata_key_t.c @@ -2,6 +2,7 @@ #include "maxminddb_test_helper.h" +#include #include // One IPv4 node whose 24-bit records both mark "no data", the data section @@ -158,6 +159,54 @@ static void test_long_pointer_keys(void) { free(db); } +// Builds metadata with count distinct keys, either in the top-level map +// (counting the nine required keys) or in the description map, and checks +// the MMDB_open status. count must be at least 285. +static void test_key_count(bool in_description, + size_t count, + int expected, + const char *message) { + const char required[] = REQUIRED_METADATA_KEYS; + const char before_description[] = + REQUIRED_KEYS_BEFORE_DESCRIPTION "\x4B" + "description"; + size_t size = sizeof(DB_PREFIX) + sizeof(required) + 3 + count * 8; + char *db = malloc(size); + if (NULL == db) { + BAIL_OUT("could not allocate the key-count test database"); + } + size_t pos = sizeof(DB_PREFIX) - 1; + memcpy(db, DB_PREFIX, pos); + size_t keys = count; + if (in_description) { + db[pos++] = '\xE9'; + memcpy(db + pos, before_description, sizeof(before_description) - 1); + pos += sizeof(before_description) - 1; + } else { + keys = count - 9; + } + // A map header with the 2-byte size form, for 285 through 65,820. + db[pos++] = '\xFE'; + db[pos++] = (char)((count - 285) >> 8); + db[pos++] = (char)((count - 285) & 0xFF); + if (!in_description) { + memcpy(db + pos, required, sizeof(required) - 1); + pos += sizeof(required) - 1; + } + for (size_t i = 0; i < keys; i++) { + char key[24]; + snprintf(key, sizeof(key), "k%04zu", i); + db[pos++] = '\x45'; + memcpy(db + pos, key, 5); + pos += 5; + db[pos++] = '\x41'; + db[pos++] = 'x'; + } + + test_open(db, pos, expected, message); + free(db); +} + static void test_bad_data_file(const char *filename) { char *path = bad_database_path(filename); MMDB_s mmdb; @@ -304,6 +353,22 @@ int main(void) { test_deep_extra_value(); test_long_pointer_keys(); + test_key_count(false, + 1024, + MMDB_SUCCESS, + "MMDB_open accepts 1,024 top-level metadata keys"); + test_key_count(false, + 1025, + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects 1,025 top-level metadata keys"); + test_key_count(true, + 1024, + MMDB_SUCCESS, + "MMDB_open accepts 1,024 description languages"); + test_key_count(true, + 1025, + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects 1,025 description languages"); test_bad_data_file("libmaxminddb-repeated-metadata-key.mmdb"); test_bad_data_file("libmaxminddb-repeated-description-key.mmdb"); From 109d392e2e121846f59ca2689902bf98af48a327 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 23:16:37 +0000 Subject: [PATCH 07/17] Document the NUL comparison for metadata keys Co-Authored-By: Claude Opus 5.5 --- doc/libmaxminddb.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/libmaxminddb.md b/doc/libmaxminddb.md index 140cea5d..14c96ea9 100644 --- a/doc/libmaxminddb.md +++ b/doc/libmaxminddb.md @@ -461,7 +461,8 @@ under `MMDB_get_entry_data_list()`, this function returns If the metadata root is not a map, or the top-level metadata map or the `description` map repeats a key or has more than 1,024 keys, this function -returns `MMDB_INVALID_METADATA_ERROR`. +returns `MMDB_INVALID_METADATA_ERROR`. It compares keys up to the first NUL +byte, so keys that differ only after a NUL count as repeated. If a top-level metadata key or value is malformed, this function returns `MMDB_INVALID_DATA_ERROR`. It does not follow pointers in values that it does From 7723f607f1a1e81d1152cd41c0011514dbbb985e Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 23:16:58 +0000 Subject: [PATCH 08/17] Move the bad-data checks into a test that runs on Windows duplicate_metadata_key_t is UNIX-only because it uses mkstemp, so Windows CI never ran the two bad-data checks. They need no temporary file, so they now live in repeated_metadata_key_t, which runs on every platform. Co-Authored-By: Claude Opus 5.5 --- t/CMakeLists.txt | 1 + t/Makefile.am | 2 +- t/duplicate_metadata_key_t.c | 18 ------------------ t/repeated_metadata_key_t.c | 25 +++++++++++++++++++++++++ 4 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 t/repeated_metadata_key_t.c diff --git a/t/CMakeLists.txt b/t/CMakeLists.txt index 61c51b60..bd7f4731 100644 --- a/t/CMakeLists.txt +++ b/t/CMakeLists.txt @@ -24,6 +24,7 @@ set(TEST_TARGET_NAMES overflow_bounds_t pointer_dos_t read_node_t + repeated_metadata_key_t version_t ) diff --git a/t/Makefile.am b/t/Makefile.am index 903771f0..ac09c48e 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -26,7 +26,7 @@ check_PROGRAMS = \ get_value_pointer_bug_t invalid_sockaddr_t \ ipv4_start_cache_t ipv6_lookup_in_ipv4_t max_depth_t metadata_t \ metadata_marker_t metadata_pointers_t no_map_get_value_t \ - overflow_bounds_t pointer_dos_t read_node_t \ + overflow_bounds_t pointer_dos_t read_node_t repeated_metadata_key_t \ threads_t version_t data_pool_t_LDFLAGS = $(AM_LDFLAGS) -lm diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c index f566dd44..b962dd5a 100644 --- a/t/duplicate_metadata_key_t.c +++ b/t/duplicate_metadata_key_t.c @@ -207,21 +207,6 @@ static void test_key_count(bool in_description, free(db); } -static void test_bad_data_file(const char *filename) { - char *path = bad_database_path(filename); - MMDB_s mmdb; - int status = MMDB_open(path, MMDB_MODE_MMAP, &mmdb); - cmp_ok(status, - "==", - MMDB_INVALID_METADATA_ERROR, - "MMDB_open rejects %s", - filename); - if (MMDB_SUCCESS == status) { - MMDB_close(&mmdb); - } - free(path); -} - int main(void) { plan(NO_PLAN); @@ -370,8 +355,5 @@ int main(void) { MMDB_INVALID_METADATA_ERROR, "MMDB_open rejects 1,025 description languages"); - test_bad_data_file("libmaxminddb-repeated-metadata-key.mmdb"); - test_bad_data_file("libmaxminddb-repeated-description-key.mmdb"); - done_testing(); } diff --git a/t/repeated_metadata_key_t.c b/t/repeated_metadata_key_t.c new file mode 100644 index 00000000..f8e94a6f --- /dev/null +++ b/t/repeated_metadata_key_t.c @@ -0,0 +1,25 @@ +#include "maxminddb_test_helper.h" + +// The repeated-key databases from MaxMind-DB. This test needs no temporary +// file, so it also runs on Windows. +static void test_bad_data_file(const char *filename) { + char *path = bad_database_path(filename); + MMDB_s mmdb; + int status = MMDB_open(path, MMDB_MODE_MMAP, &mmdb); + cmp_ok(status, + "==", + MMDB_INVALID_METADATA_ERROR, + "MMDB_open rejects %s", + filename); + if (MMDB_SUCCESS == status) { + MMDB_close(&mmdb); + } + free(path); +} + +int main(void) { + plan(NO_PLAN); + test_bad_data_file("libmaxminddb-repeated-metadata-key.mmdb"); + test_bad_data_file("libmaxminddb-repeated-description-key.mmdb"); + done_testing(); +} From 53f2df2b2f3aee08637a679c4fe909d3616bec8f Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 23:17:03 +0000 Subject: [PATCH 09/17] Drop the unreleased history from the skip changelog entry The repeated-key check is new in this release, so no released version could miss a repeated key through the skip bug. Co-Authored-By: Claude Opus 5.5 --- Changes.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Changes.md b/Changes.md index 20ab98ab..e59808ec 100644 --- a/Changes.md +++ b/Changes.md @@ -18,8 +18,7 @@ - `MMDB_get_value()` and its variants, and `MMDB_open()`, now return `MMDB_INVALID_DATA_ERROR` when they skip a nested map whose key is a map or an array. Before, they skipped only the key's header and read the data after it - out of step. A lookup could return the wrong value, and a repeated metadata - key could go undetected. + out of step. A lookup could return the wrong value. ## 1.14.1 - 2026-09-17 From fb1cabbbb060582f9ccb6035ec44849de961861f Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Thu, 5 Nov 2026 23:17:34 +0000 Subject: [PATCH 10/17] Test the skip fix through a data-section lookup The only test reached the change through the MMDB_open metadata walk. Add a record {"a": {["x"]: "y"}, "b": "z"} and check that MMDB_get_value for "b" returns MMDB_INVALID_DATA_ERROR. On main it returns MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR. The temporary-file code moves into write_temp_db so both tests can use it. Co-Authored-By: Claude Opus 5.5 --- t/duplicate_metadata_key_t.c | 72 ++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c index b962dd5a..8cc5e4d8 100644 --- a/t/duplicate_metadata_key_t.c +++ b/t/duplicate_metadata_key_t.c @@ -52,18 +52,18 @@ #define TEST_OPEN(db, expected, message) \ test_open(db, sizeof(db) - 1, expected, message) -static void -test_open(const char *db, size_t size, int expected, const char *message) { +// Writes db to a new file in TMPDIR, or /tmp, and puts its path in filename. +static void write_temp_db(const char *db, + size_t size, + char *filename, + size_t filename_size) { const char *tmpdir = getenv("TMPDIR"); if (NULL == tmpdir || '\0' == tmpdir[0]) { tmpdir = "/tmp"; } - char filename[4096]; - int length = snprintf(filename, - sizeof(filename), - "%s/duplicate_metadata_key_t_XXXXXX", - tmpdir); - if (length < 0 || (size_t)length >= sizeof(filename)) { + int length = snprintf( + filename, filename_size, "%s/duplicate_metadata_key_t_XXXXXX", tmpdir); + if (length < 0 || (size_t)length >= filename_size) { BAIL_OUT("TMPDIR is too long"); } int fd = mkstemp(filename); @@ -78,6 +78,12 @@ test_open(const char *db, size_t size, int expected, const char *message) { if (0 != fclose(fp) || written != size) { BAIL_OUT("could not write %s", filename); } +} + +static void +test_open(const char *db, size_t size, int expected, const char *message) { + char filename[4096]; + write_temp_db(db, size, filename, sizeof(filename)); MMDB_s mmdb; int status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb); @@ -207,6 +213,54 @@ static void test_key_count(bool in_description, free(db); } +// A record {"a": {["x"]: "y"}, "b": "z"}. Both search-tree records point to +// it. Looking up "b" skips the value of "a", which has an array key. +static void test_lookup_after_array_key(void) { + const char db[] = "\x00\x00\x11\x00\x00\x11" + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\xE2" + "\x41" + "a" + "\xE1" + "\x01\x04" + "\x41" + "x" + "\x41" + "y" + "\x41" + "b" + "\x41" + "z" + "\xAB\xCD\xEF" + "MaxMind.com" + "\xE9" REQUIRED_METADATA_KEYS; + char filename[4096]; + write_temp_db(db, sizeof(db) - 1, filename, sizeof(filename)); + + MMDB_s mmdb; + int status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb); + cmp_ok(status, "==", MMDB_SUCCESS, "MMDB_open opens the array-key record"); + if (MMDB_SUCCESS == status) { + int gai_error, mmdb_error; + MMDB_lookup_result_s result = + MMDB_lookup_string(&mmdb, "1.1.1.1", &gai_error, &mmdb_error); + cmp_ok(mmdb_error, "==", MMDB_SUCCESS, "lookup succeeded"); + ok(result.found_entry, "lookup found the record"); + MMDB_entry_data_s data; + status = MMDB_get_value(&result.entry, &data, "b", NULL); + cmp_ok(status, + "==", + MMDB_INVALID_DATA_ERROR, + "MMDB_get_value rejects a nested map key that is an array"); + MMDB_close(&mmdb); + } + + if (0 != remove(filename)) { + BAIL_OUT("could not remove %s", filename); + } +} + int main(void) { plan(NO_PLAN); @@ -355,5 +409,7 @@ int main(void) { MMDB_INVALID_METADATA_ERROR, "MMDB_open rejects 1,025 description languages"); + test_lookup_after_array_key(); + done_testing(); } From ca1e35578fcf567add3dd6531bc62fd1313c11f8 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 6 Nov 2026 18:43:43 +0000 Subject: [PATCH 11/17] Skip top-level metadata values from depth 1 The walk skipped each top-level metadata value from depth 0, but MMDB_get_metadata_as_entry_data_list decodes the same values from depth 1. A value with 511 nested arrays therefore passed MMDB_open, and then MMDB_get_metadata_as_entry_data_list failed with MMDB_DECODER_LIMIT_ERROR. Both now apply the same limit. The test checks both sides of the boundary and uses MAXIMUM_DATA_STRUCTURE_DEPTH if the build overrides it. Co-Authored-By: Claude Opus 5.5 --- src/maxminddb.c | 4 ++- t/duplicate_metadata_key_t.c | 50 ++++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/maxminddb.c b/src/maxminddb.c index cdef4a91..fdc0298a 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -796,9 +796,11 @@ static int next_map_key(const MMDB_s *const mmdb, return MMDB_INVALID_DATA_ERROR; } + // A top-level value is at depth 1, as in + // MMDB_get_metadata_as_entry_data_list, so both apply the same limit. MMDB_entry_data_s value; CHECKED_DECODE_ONE(mmdb, key->offset_to_next, &value); - int status = skip_map_or_array(mmdb, &value, 0); + int status = skip_map_or_array(mmdb, &value, 1); if (MMDB_SUCCESS != status) { return status; } diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c index 8cc5e4d8..4e9fb1a8 100644 --- a/t/duplicate_metadata_key_t.c +++ b/t/duplicate_metadata_key_t.c @@ -97,30 +97,56 @@ test_open(const char *db, size_t size, int expected, const char *message) { } } -// An extra key whose value is 600 nested one-element arrays, deeper than the -// default MAXIMUM_DATA_STRUCTURE_DEPTH of 512. -static void test_deep_extra_value(void) { +#ifndef MAXIMUM_DATA_STRUCTURE_DEPTH + #define MAXIMUM_DATA_STRUCTURE_DEPTH 512 +#endif + +// An extra key whose value is a chain of `arrays` nested one-element arrays +// around a uint16. The value is at depth 1, and the uint16 is at depth +// arrays + 1. +// MMDB_get_metadata_as_entry_data_list fails when a value reaches +// MAXIMUM_DATA_STRUCTURE_DEPTH, and MMDB_open must agree with it. +static void test_deep_extra_value(size_t arrays, int expected) { const char head[] = DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x45" "extra"; const size_t head_size = sizeof(head) - 1; - const size_t depth = 600; - size_t size = head_size + depth * 2 + 1; + size_t size = head_size + arrays * 2 + 1; char *db = malloc(size); if (NULL == db) { BAIL_OUT("could not allocate the deep test database"); } memcpy(db, head, head_size); - for (size_t i = 0; i < depth; i++) { + for (size_t i = 0; i < arrays; i++) { db[head_size + i * 2] = '\x01'; db[head_size + i * 2 + 1] = '\x04'; } db[size - 1] = '\xA0'; - test_open(db, - size, - MMDB_INVALID_METADATA_ERROR, - "MMDB_open rejects an extra value nested past the depth limit"); + char filename[4096]; + write_temp_db(db, size, filename, sizeof(filename)); free(db); + + MMDB_s mmdb; + int status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb); + cmp_ok(status, + "==", + expected, + "MMDB_open status for an extra value with %zu nested arrays", + arrays); + if (MMDB_SUCCESS == status) { + MMDB_entry_data_list_s *list = NULL; + status = MMDB_get_metadata_as_entry_data_list(&mmdb, &list); + cmp_ok(status, + "==", + MMDB_SUCCESS, + "MMDB_get_metadata_as_entry_data_list reads the same metadata"); + MMDB_free_entry_data_list(list); + MMDB_close(&mmdb); + } + + if (0 != remove(filename)) { + BAIL_OUT("could not remove %s", filename); + } } // Pointer keys into a run of 0x5E bytes. The key at run offset j is a @@ -390,7 +416,9 @@ int main(void) { MMDB_INVALID_DATA_ERROR, "MMDB_open rejects a nested map key that is an array"); - test_deep_extra_value(); + test_deep_extra_value(MAXIMUM_DATA_STRUCTURE_DEPTH - 2, MMDB_SUCCESS); + test_deep_extra_value(MAXIMUM_DATA_STRUCTURE_DEPTH - 1, + MMDB_INVALID_METADATA_ERROR); test_long_pointer_keys(); test_key_count(false, 1024, From edcbcb355de46e32fd10879d10ccf37c203ebad8 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 6 Nov 2026 18:44:17 +0000 Subject: [PATCH 12/17] Check the metadata major version before the strict walk The walk of every top-level metadata key ran before MMDB_open checked binary_format_major_version. A newer format can have metadata that this version cannot decode, so MMDB_open returned MMDB_INVALID_DATA_ERROR or MMDB_INVALID_METADATA_ERROR for it instead of MMDB_UNKNOWN_DATABASE_FORMAT_ERROR. read_metadata now looks up the major version first, as main effectively did by not reading the extra values. Co-Authored-By: Claude Opus 5.5 --- src/maxminddb.c | 17 ++++++++++++++--- t/duplicate_metadata_key_t.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/maxminddb.c b/src/maxminddb.c index fdc0298a..5d02fc6c 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -605,16 +605,27 @@ static int read_metadata(MMDB_s *mmdb) { want to use the same functions we use for the data section to get metadata values. */ MMDB_s metadata_db = make_fake_metadata_db(mmdb); + MMDB_entry_s metadata_start = {.mmdb = &metadata_db, .offset = 0}; + + // Check the major version before the strict walk below. A newer format + // can have metadata that this version cannot decode, and the caller must + // still get MMDB_UNKNOWN_DATABASE_FORMAT_ERROR for it. If this lookup + // fails or finds 0, the full read below reports the error. + uint16_t major_version = 0; + int status = value_for_key_as_uint16( + &metadata_start, "binary_format_major_version", &major_version); + if (MMDB_SUCCESS == status && 0 != major_version && 2 != major_version) { + DEBUG_MSGF("unsupported binary_format_major_version %i", major_version); + return MMDB_UNKNOWN_DATABASE_FORMAT_ERROR; + } // Key lookups return the first match, but other readers can keep the // last one. A repeated key could give each reader a different value. - int status = check_metadata_keys_unique(&metadata_db); + status = check_metadata_keys_unique(&metadata_db); if (MMDB_SUCCESS != status) { return status; } - MMDB_entry_s metadata_start = {.mmdb = &metadata_db, .offset = 0}; - status = value_for_key_as_uint32( &metadata_start, "node_count", &mmdb->metadata.node_count); if (MMDB_SUCCESS != status) { diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c index 4e9fb1a8..076977a5 100644 --- a/t/duplicate_metadata_key_t.c +++ b/t/duplicate_metadata_key_t.c @@ -287,6 +287,33 @@ static void test_lookup_after_array_key(void) { } } +// Major version 3 metadata with an extra value that this version cannot +// decode. MMDB_open must report the unknown format, not the bad value. +static void test_newer_major_version(void) { + char db[] = DB_PREFIX "\xEA" REQUIRED_METADATA_KEYS "\x45" + "extra" + "\x44" + "ab"; + const char key[] = "binary_format_major_version"; + size_t size = sizeof(db) - 1; + size_t version = 0; + for (size_t i = 0; i + sizeof(key) + 1 < size; i++) { + if (0 == memcmp(db + i, key, sizeof(key) - 1)) { + version = i + sizeof(key) - 1 + 1; + break; + } + } + if (0 == version || '\x02' != db[version]) { + BAIL_OUT("could not find binary_format_major_version in the fixture"); + } + db[version] = '\x03'; + + test_open(db, + size, + MMDB_UNKNOWN_DATABASE_FORMAT_ERROR, + "MMDB_open reports a newer major version before bad metadata"); +} + int main(void) { plan(NO_PLAN); @@ -438,6 +465,7 @@ int main(void) { "MMDB_open rejects 1,025 description languages"); test_lookup_after_array_key(); + test_newer_major_version(); done_testing(); } From e5548e3cf1186d3c65561d954a6be50d346f05a9 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 6 Nov 2026 18:45:06 +0000 Subject: [PATCH 13/17] Size the payload-budget test from MAXIMUM_DATA_STRUCTURE_BYTES The test hard-coded 100 keys of 24 KB for the default 2 MiB budget. A build with a larger MAXIMUM_DATA_STRUCTURE_BYTES, such as the 3 MiB example in the docs, opened the file and failed make check. The key count now comes from the macro, and the test skips when the budget is too large for the fixture. It passes with the default, 1 MiB, and 3 MiB budgets, and skips with 128 MiB. Co-Authored-By: Claude Opus 5.5 --- t/duplicate_metadata_key_t.c | 60 +++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/t/duplicate_metadata_key_t.c b/t/duplicate_metadata_key_t.c index 076977a5..d850d8b3 100644 --- a/t/duplicate_metadata_key_t.c +++ b/t/duplicate_metadata_key_t.c @@ -149,31 +149,61 @@ static void test_deep_extra_value(size_t arrays, int expected) { } } +#ifndef MAXIMUM_DATA_STRUCTURE_BYTES + #define MAXIMUM_DATA_STRUCTURE_BYTES (1U << 21) +#endif + +// Writes a map header for count pairs and returns its size. +static size_t write_map_header(char *buf, size_t count) { + if (count <= 28) { + buf[0] = (char)(0xE0 | count); + return 1; + } + if (count <= 284) { + buf[0] = '\xFD'; + buf[1] = (char)(count - 29); + return 2; + } + buf[0] = '\xFE'; + buf[1] = (char)((count - 285) >> 8); + buf[2] = (char)((count - 285) & 0xFF); + return 3; +} + // Pointer keys into a run of 0x5E bytes. The key at run offset j is a -// 24,443-byte string that starts with 100 - j bytes of 0x5E, so the keys are -// distinct, but together they exceed the 2 MiB payload budget. +// 24,443-byte string that starts with key_count - j bytes of 0x5E, so the keys +// are distinct, but together they exceed MAXIMUM_DATA_STRUCTURE_BYTES. static void test_long_pointer_keys(void) { - const size_t key_count = 100; - const size_t run = key_count + 3; const size_t key_size = 285 + 0x5E5E; + const size_t key_count = MAXIMUM_DATA_STRUCTURE_BYTES / key_size + 1; + // The keys must fit the map key cap, and each pointer has a one-byte + // offset payload. + skip(key_count > 1000, + 1, + "MAXIMUM_DATA_STRUCTURE_BYTES is too large for this fixture"); + + const size_t run = key_count + 3; const size_t blob_size = run + key_size; - const char head[] = DB_PREFIX "\xFD\x51" REQUIRED_METADATA_KEYS "\x44" - "blob" - "\x5E"; - const size_t head_size = sizeof(head) - 1; + const char keys[] = REQUIRED_METADATA_KEYS "\x44" + "blob" + "\x5E"; const size_t prefix_size = sizeof(DB_PREFIX) - 1; - // The metadata offset of the blob content, after its 3-byte header. - const size_t blob_offset = head_size + 2 - prefix_size; + const size_t keys_size = sizeof(keys) - 1; - size_t size = head_size + 2 + blob_size + key_count * 3; + size_t size = prefix_size + 3 + keys_size + 2 + blob_size + key_count * 3; char *db = malloc(size); if (NULL == db) { BAIL_OUT("could not allocate the long-key test database"); } - memcpy(db, head, head_size); - size_t pos = head_size; + memcpy(db, DB_PREFIX, prefix_size); + size_t pos = prefix_size; + pos += write_map_header(db + pos, 10 + key_count); + memcpy(db + pos, keys, keys_size); + pos += keys_size; db[pos++] = (char)((blob_size - 285) >> 8); db[pos++] = (char)((blob_size - 285) & 0xFF); + // The metadata offset of the blob content. + const size_t blob_offset = pos - prefix_size; memset(db + pos, 0x5E, run); memset(db + pos + run, 'a', key_size); pos += blob_size; @@ -185,10 +215,12 @@ static void test_long_pointer_keys(void) { } test_open(db, - size, + pos, MMDB_INVALID_METADATA_ERROR, "MMDB_open rejects metadata keys past the payload budget"); free(db); + + end_skip; } // Builds metadata with count distinct keys, either in the top-level map From d974272075279a59f01bcc76dd7e64050cad991a Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 6 Nov 2026 18:45:39 +0000 Subject: [PATCH 14/17] Check description keys with the same NUL rule as top-level keys check_languages_unique built a second key array from the strndup copies and used strlen on them, while the top-level check used memchr on the raw keys. The two applied the same rule in two ways. The description loop now fills the key array from the decoded list with the shared metadata_key helper and calls check_keys_unique directly. Co-Authored-By: Claude Opus 5.5 --- src/maxminddb.c | 67 ++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/src/maxminddb.c b/src/maxminddb.c index 5d02fc6c..f3256a33 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -203,6 +203,7 @@ static int check_metadata_keys_unique(const MMDB_s *const metadata_db); static int next_map_key(const MMDB_s *const mmdb, uint32_t *offset, MMDB_entry_data_s *key); +static metadata_key_s metadata_key(const char *name, uint32_t size); static int check_keys_unique(metadata_key_s *keys, uint32_t count); static int compare_metadata_keys(const void *a, const void *b); static int @@ -219,8 +220,6 @@ static int populate_languages_metadata(MMDB_s *mmdb, static int populate_description_metadata(MMDB_s *mmdb, MMDB_s *metadata_db, MMDB_entry_s *metadata_start); -static int check_languages_unique(MMDB_description_s **descriptions, - size_t count); static int resolve_any_address(const char *ipstr, struct addrinfo **addresses); static int find_address_in_search_tree(const MMDB_s *const mmdb, uint8_t const *address, @@ -779,15 +778,7 @@ static int check_metadata_keys_unique(const MMDB_s *const metadata_db) { status = MMDB_DECODER_LIMIT_ERROR; goto cleanup; } - // Compare up to the first NUL, as check_languages_unique does. Some - // callers, such as the PHP extension, cut keys there. - const char *nul = memchr(key.utf8_string, '\0', key.data_size); - keys[i].name = key.utf8_string; - if (NULL == nul) { - keys[i].size = key.data_size; - } else { - keys[i].size = (uint32_t)(nul - key.utf8_string); - } + keys[i] = metadata_key(key.utf8_string, key.data_size); } status = check_keys_unique(keys, map.data_size); @@ -820,6 +811,18 @@ static int next_map_key(const MMDB_s *const mmdb, return MMDB_SUCCESS; } +/* Returns the key to compare for a metadata map key. Keys are compared up to + * the first NUL, because some callers, such as the PHP extension, cut keys + * there. */ +static metadata_key_s metadata_key(const char *name, uint32_t size) { + metadata_key_s key = {.name = name, .size = size}; + const char *nul = memchr(name, '\0', size); + if (NULL != nul) { + key.size = (uint32_t)(nul - name); + } + return key; +} + /* Sorts keys and returns MMDB_INVALID_METADATA_ERROR if two are equal. */ static int check_keys_unique(metadata_key_s *keys, uint32_t count) { qsort(keys, count, sizeof(metadata_key_s), compare_metadata_keys); @@ -1009,6 +1012,7 @@ static int populate_description_metadata(MMDB_s *mmdb, } MMDB_entry_data_list_s *first_member = member; + metadata_key_s *keys = NULL; uint32_t map_size = member->entry_data.data_size; mmdb->metadata.description.count = 0; @@ -1019,6 +1023,17 @@ static int populate_description_metadata(MMDB_s *mmdb, MAYBE_CHECK_SIZE_OVERFLOW(map_size, SIZE_MAX / sizeof(MMDB_description_s *), MMDB_INVALID_METADATA_ERROR); + if (map_size > METADATA_MAP_MAX_KEYS) { + DEBUG_MSG("description map has too many keys"); + status = MMDB_INVALID_METADATA_ERROR; + goto cleanup; + } + + keys = calloc(map_size, sizeof(metadata_key_s)); + if (NULL == keys) { + status = MMDB_OUT_OF_MEMORY_ERROR; + goto cleanup; + } mmdb->metadata.description.descriptions = calloc(map_size, sizeof(MMDB_description_s *)); @@ -1046,6 +1061,8 @@ static int populate_description_metadata(MMDB_s *mmdb, goto cleanup; } + keys[i] = metadata_key(member->entry_data.utf8_string, + member->entry_data.data_size); mmdb->metadata.description.descriptions[i]->language = mmdb_strndup( member->entry_data.utf8_string, member->entry_data.data_size); @@ -1070,39 +1087,15 @@ static int populate_description_metadata(MMDB_s *mmdb, } } - // Compare the languages as stored, so keys that differ only after a NUL - // count as repeated. - status = check_languages_unique(mmdb->metadata.description.descriptions, - mmdb->metadata.description.count); + status = check_keys_unique(keys, map_size); cleanup: + free(keys); MMDB_free_entry_data_list(first_member); return status; } -static int check_languages_unique(MMDB_description_s **descriptions, - size_t count) { - if (count < 2) { - return MMDB_SUCCESS; - } - if (count > METADATA_MAP_MAX_KEYS) { - DEBUG_MSG("description map has too many keys"); - return MMDB_INVALID_METADATA_ERROR; - } - metadata_key_s *keys = calloc(count, sizeof(metadata_key_s)); - if (NULL == keys) { - return MMDB_OUT_OF_MEMORY_ERROR; - } - for (size_t i = 0; i < count; i++) { - keys[i].name = descriptions[i]->language; - keys[i].size = (uint32_t)strlen(keys[i].name); - } - int status = check_keys_unique(keys, (uint32_t)count); - free(keys); - return status; -} - MMDB_lookup_result_s MMDB_lookup_string(const MMDB_s *const mmdb, const char *const ipstr, int *const gai_error, From 46ba934eb68cf7b4e9a413219cd8998b08a3b5c6 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 6 Nov 2026 18:45:53 +0000 Subject: [PATCH 15/17] Move the metadata key comparison helpers below their callers metadata_key, check_keys_unique, and compare_metadata_keys came before populate_description_metadata, which also calls them. They now come after it, so each caller comes before the function that it calls. The code does not change. Co-Authored-By: Claude Opus 5.5 --- src/maxminddb.c | 78 ++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/maxminddb.c b/src/maxminddb.c index f3256a33..27cb9631 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -203,9 +203,6 @@ static int check_metadata_keys_unique(const MMDB_s *const metadata_db); static int next_map_key(const MMDB_s *const mmdb, uint32_t *offset, MMDB_entry_data_s *key); -static metadata_key_s metadata_key(const char *name, uint32_t size); -static int check_keys_unique(metadata_key_s *keys, uint32_t count); -static int compare_metadata_keys(const void *a, const void *b); static int value_for_key_as_uint16(MMDB_entry_s *start, char *key, uint16_t *value); static int @@ -220,6 +217,9 @@ static int populate_languages_metadata(MMDB_s *mmdb, static int populate_description_metadata(MMDB_s *mmdb, MMDB_s *metadata_db, MMDB_entry_s *metadata_start); +static metadata_key_s metadata_key(const char *name, uint32_t size); +static int check_keys_unique(metadata_key_s *keys, uint32_t count); +static int compare_metadata_keys(const void *a, const void *b); static int resolve_any_address(const char *ipstr, struct addrinfo **addresses); static int find_address_in_search_tree(const MMDB_s *const mmdb, uint8_t const *address, @@ -811,42 +811,6 @@ static int next_map_key(const MMDB_s *const mmdb, return MMDB_SUCCESS; } -/* Returns the key to compare for a metadata map key. Keys are compared up to - * the first NUL, because some callers, such as the PHP extension, cut keys - * there. */ -static metadata_key_s metadata_key(const char *name, uint32_t size) { - metadata_key_s key = {.name = name, .size = size}; - const char *nul = memchr(name, '\0', size); - if (NULL != nul) { - key.size = (uint32_t)(nul - name); - } - return key; -} - -/* Sorts keys and returns MMDB_INVALID_METADATA_ERROR if two are equal. */ -static int check_keys_unique(metadata_key_s *keys, uint32_t count) { - qsort(keys, count, sizeof(metadata_key_s), compare_metadata_keys); - for (uint32_t i = 1; i < count; i++) { - if (0 == compare_metadata_keys(&keys[i - 1], &keys[i])) { - DEBUG_MSG("duplicate key in metadata"); - return MMDB_INVALID_METADATA_ERROR; - } - } - return MMDB_SUCCESS; -} - -static int compare_metadata_keys(const void *a, const void *b) { - const metadata_key_s *key_a = a; - const metadata_key_s *key_b = b; - if (key_a->size < key_b->size) { - return -1; - } - if (key_a->size > key_b->size) { - return 1; - } - return memcmp(key_a->name, key_b->name, key_a->size); -} - static int value_for_key_as_uint16(MMDB_entry_s *start, char *key, uint16_t *value) { MMDB_entry_data_s entry_data; @@ -1096,6 +1060,42 @@ static int populate_description_metadata(MMDB_s *mmdb, return status; } +/* Returns the key to compare for a metadata map key. Keys are compared up to + * the first NUL, because some callers, such as the PHP extension, cut keys + * there. */ +static metadata_key_s metadata_key(const char *name, uint32_t size) { + metadata_key_s key = {.name = name, .size = size}; + const char *nul = memchr(name, '\0', size); + if (NULL != nul) { + key.size = (uint32_t)(nul - name); + } + return key; +} + +/* Sorts keys and returns MMDB_INVALID_METADATA_ERROR if two are equal. */ +static int check_keys_unique(metadata_key_s *keys, uint32_t count) { + qsort(keys, count, sizeof(metadata_key_s), compare_metadata_keys); + for (uint32_t i = 1; i < count; i++) { + if (0 == compare_metadata_keys(&keys[i - 1], &keys[i])) { + DEBUG_MSG("duplicate key in metadata"); + return MMDB_INVALID_METADATA_ERROR; + } + } + return MMDB_SUCCESS; +} + +static int compare_metadata_keys(const void *a, const void *b) { + const metadata_key_s *key_a = a; + const metadata_key_s *key_b = b; + if (key_a->size < key_b->size) { + return -1; + } + if (key_a->size > key_b->size) { + return 1; + } + return memcmp(key_a->name, key_b->name, key_a->size); +} + MMDB_lookup_result_s MMDB_lookup_string(const MMDB_s *const mmdb, const char *const ipstr, int *const gai_error, From 522dd449335323f87dec556d1bb37eeb2deb4a4d Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 6 Nov 2026 18:46:04 +0000 Subject: [PATCH 16/17] Document the map or array key error for lookups Only Changes.md said that MMDB_get_value and its variants now return MMDB_INVALID_DATA_ERROR when they skip a nested map whose key is a map or an array. The Data Lookup Functions section and the error code list now say so too. Co-Authored-By: Claude Opus 5.5 --- doc/libmaxminddb.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/libmaxminddb.md b/doc/libmaxminddb.md index 14c96ea9..45245255 100644 --- a/doc/libmaxminddb.md +++ b/doc/libmaxminddb.md @@ -381,8 +381,9 @@ status codes are: its major version is not 2. This library can only handle major version 2. - `MMDB_OUT_OF_MEMORY_ERROR` - a memory allocation call (`malloc`, etc.) failed. - `MMDB_INVALID_DATA_ERROR` - an entry in the data section contains invalid - data. For example, a `uint16` field is claiming to be more than 2 bytes long. - The database is probably damaged or was generated incorrectly. + data. For example, a `uint16` field is claiming to be more than 2 bytes long, + or a map has a key that is a map or an array. The database is probably damaged + or was generated incorrectly. - `MMDB_INVALID_LOOKUP_PATH_ERROR` - The lookup path passed to `MMDB_get_value`, `MMDB_vget_value`, or `MMDB_aget_value` contains an array offset that is larger than LONG_MAX or smaller than LONG_MIN. @@ -640,6 +641,9 @@ calls. For each of the three functions, the return value is a status code as defined above. +Map keys must be strings. If a lookup skips a nested map whose key is a map or +an array, these functions return `MMDB_INVALID_DATA_ERROR`. + ## `MMDB_get_entry_data_list()` ```c From 30ac950edad83aa105d85f4b4291eec84c87cdaa Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Fri, 6 Nov 2026 18:46:12 +0000 Subject: [PATCH 17/17] Check the repeated node_count and pointer-key fixtures Update t/maxmind-db to the current maxmind/MaxMind-DB#314 commit. It adds libmaxminddb-repeated-node-count.mmdb and libmaxminddb-repeated-pointer-key.mmdb, sets languages in the repeated-description fixture, and adds the spec rule that metadata keys must be unique. repeated_metadata_key_t now also checks that MMDB_open rejects the two new fixtures. Co-Authored-By: Claude Opus 5.5 --- t/maxmind-db | 2 +- t/repeated_metadata_key_t.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/t/maxmind-db b/t/maxmind-db index 2e517d62..4464b5f6 160000 --- a/t/maxmind-db +++ b/t/maxmind-db @@ -1 +1 @@ -Subproject commit 2e517d62c64ed69bac85b26b58f01006e9cd4fd4 +Subproject commit 4464b5f65fcbdff9d3ca16e5da6a6bf5d492cbbe diff --git a/t/repeated_metadata_key_t.c b/t/repeated_metadata_key_t.c index f8e94a6f..037038f3 100644 --- a/t/repeated_metadata_key_t.c +++ b/t/repeated_metadata_key_t.c @@ -21,5 +21,7 @@ int main(void) { plan(NO_PLAN); test_bad_data_file("libmaxminddb-repeated-metadata-key.mmdb"); test_bad_data_file("libmaxminddb-repeated-description-key.mmdb"); + test_bad_data_file("libmaxminddb-repeated-node-count.mmdb"); + test_bad_data_file("libmaxminddb-repeated-pointer-key.mmdb"); done_testing(); }