From e66e2bb891b06aaec6f18b1676ff208f034f071d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Diot?= Date: Wed, 12 Jun 2024 12:15:24 +0200 Subject: [PATCH] Squashed 'src/deps/src/libmaxminddb/' changes from e26013e1d2..7acfe43a72 7acfe43a72 Bumped version to 1.10.0 3fcb19e6e2 Set release date 19bc81a013 Update test submodule 8493aff051 Add change log entry for #346 feb088a7ff Merge pull request #349 from maxmind/greg/fix-4-gb-search-tree-lookups f6a3ccf945 Do not cause integer overflow f33d0224c2 Merge pull request #346 from Zopolis4/moregnuinstalldirs 8b4bf0dec9 Make greater use of GNUInstallDirs c7f2d1dc99 Merge pull request #345 from maxmind/greg/update-json-comments cbd4c945b4 Fix some typos 1e5187230b Clarify that mmdblookup does not produce JSON a7c9f3bb56 Merge pull request #343 from maxmind/greg/changelog-342 573bac55b6 Add changes entry for #342 5c5d875902 Merge pull request #342 from clwluvw/cmake 878e2ca407 cmake: generate install targets optionally f19112465d cmake: make binary build optional baeb0439ce Merge pull request #340 from maxmind/greg/1.9.1 git-subtree-dir: src/deps/src/libmaxminddb git-subtree-split: 7acfe43a72a5043d01cc3dd6429005acdf812cb3 --- CMakeLists.txt | 43 +++-- Changes.md | 14 +- bin/CMakeLists.txt | 10 +- configure.ac | 2 +- doc/mmdblookup.md | 6 +- include/maxminddb_config.h.cmake.in | 2 +- include/maxminddb_config.h.in | 2 +- src/maxminddb.c | 3 +- t/maxmind-db | 2 +- t/metadata_t.c | 6 +- t/read_node_t.c | 279 +++++++++++++++------------- 11 files changed, 204 insertions(+), 165 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 80d55d2e4..f8fc2ca99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.9) project(maxminddb LANGUAGES C - VERSION 1.9.1 + VERSION 1.10.0 ) set(MAXMINDDB_SOVERSION 0.0.7) set(CMAKE_C_STANDARD 99) @@ -13,6 +13,8 @@ if (WIN32) endif() option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" OFF) option(BUILD_TESTING "Build test programs" ON) +option(MAXMINDDB_BUILD_BINARIES "Build binaries" ON) +option(MAXMINDDB_INSTALL "Generate the install target" ON) include(GNUInstallDirs) @@ -90,17 +92,20 @@ set(MAXMINDB_HEADERS ) set_target_properties(maxminddb PROPERTIES PUBLIC_HEADER "${MAXMINDB_HEADERS}") -install(TARGETS maxminddb - EXPORT maxminddb) +if (MAXMINDDB_INSTALL) + install(TARGETS maxminddb + EXPORT maxminddb) -# This is required to work with FetchContent -install(EXPORT maxminddb - FILE maxminddb-config.cmake - NAMESPACE maxminddb:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/maxminddb) + # This is required to work with FetchContent + install(EXPORT maxminddb + FILE maxminddb-config.cmake + NAMESPACE maxminddb:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/maxminddb) +endif() -# We always want to build mmdblookup -add_subdirectory(bin) +if (MAXMINDDB_BUILD_BINARIES) + add_subdirectory(bin) +endif() if (BUILD_TESTING) enable_testing() @@ -110,14 +115,16 @@ endif() # Generate libmaxminddb.pc file for pkg-config # Set the required variables as same with autotools set(prefix ${CMAKE_INSTALL_PREFIX}) -set(exec_prefix \${prefix}) -set(libdir \${exec_prefix}/lib) -set(includedir \${prefix}/include) +set(exec_prefix ${CMAKE_INSTALL_PREFIX}) +set(libdir ${CMAKE_INSTALL_LIBDIR}) +set(includedir ${CMAKE_INSTALL_INCLUDEDIR}) set(PACKAGE_VERSION ${maxminddb_VERSION}) -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/libmaxminddb.pc.in - ${CMAKE_CURRENT_BINARY_DIR}/src/libmaxminddb.pc - @ONLY) +if (MAXMINDDB_INSTALL) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/libmaxminddb.pc.in + ${CMAKE_CURRENT_BINARY_DIR}/src/libmaxminddb.pc + @ONLY) -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/src/libmaxminddb.pc - DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/src/libmaxminddb.pc + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) +endif() diff --git a/Changes.md b/Changes.md index e31c34f3b..a14efa111 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,15 @@ +## 1.10.0 - 2024-06-10 + +* When building with CMake, it is now possible to disable the building + of binaries (e.g., `mmdblookup`) with the `MAXMINDDB_BUILD_BINARIES` + option and the install target generation with the `MAXMINDDB_INSTALL` + option. Pull request by Seena Fallah. GitHub #342. +* CMake now makes greater use of GNUInstallDirs. Pull request by Maximilian + Downey Twiss. GitHub #346. +* The reader can now lookup records on a database with a search tree + that is greater than 4 gigabytes without sometimes returning erroneous + results due to an integer overflow. + ## 1.9.1 - 2024-01-09 * `SSIZE_MAX` is now defined conditionally on Windows. The 1.9.0 @@ -230,7 +242,7 @@ code to think it had found valid metadata when none existed. In addition, this could lead to an attempt to read past the end of the database entirely. Finally, if there are multiple metadata markers in the database, - we treat the final one as the start of the metdata, instead of the first. + we treat the final one as the start of the metadata, instead of the first. Implemented by Tobias Stoeckmann. GitHub #102. * Don't attempt to mmap a file that is too large to be mmapped on the system. Implemented by Tobias Stoeckmann. GitHub #101. diff --git a/bin/CMakeLists.txt b/bin/CMakeLists.txt index b2d6a8ce6..ea69ce3af 100644 --- a/bin/CMakeLists.txt +++ b/bin/CMakeLists.txt @@ -15,8 +15,10 @@ if(NOT MSVC) target_link_libraries(mmdblookup maxminddb pthread) - install( - TARGETS mmdblookup - DESTINATION bin - ) + if (MAXMINDDB_INSTALL) + install( + TARGETS mmdblookup + DESTINATION ${CMAKE_INSTALL_BINDIR} + ) + endif() endif() diff --git a/configure.ac b/configure.ac index 579d3522b..a56e406f6 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.63]) -AC_INIT([libmaxminddb], [1.9.1], [support@maxmind.com]) +AC_INIT([libmaxminddb], [1.10.0], [support@maxmind.com]) AC_CONFIG_SRCDIR([include/maxminddb.h]) AC_CONFIG_HEADERS([config.h include/maxminddb_config.h]) diff --git a/doc/mmdblookup.md b/doc/mmdblookup.md index 47946310b..3ae8d8947 100644 --- a/doc/mmdblookup.md +++ b/doc/mmdblookup.md @@ -9,8 +9,10 @@ mmdblookup --file [FILE PATH] --ip [IP ADDRESS] [DATA PATH] # DESCRIPTION `mmdblookup` looks up an IP address in the specified MaxMind DB file. The -record for the IP address is displayed in a JSON-like structure with type -annotations. +record for the IP address is displayed with `{}` to denote maps and `[]` to +denote arrays. The values are followed by type annotations. This output is +_not_ JSON and is not intended to be used as such. If you need JSON, please +see [`mmdbinspect`](https://github.com/maxmind/mmdbinspect). If an IP's data entry resolves to a map or array, you can provide a lookup path to only show part of that data. diff --git a/include/maxminddb_config.h.cmake.in b/include/maxminddb_config.h.cmake.in index 8b1977f86..5a825f4f4 100644 --- a/include/maxminddb_config.h.cmake.in +++ b/include/maxminddb_config.h.cmake.in @@ -2,7 +2,7 @@ #define MAXMINDDB_CONFIG_H #ifndef MMDB_UINT128_USING_MODE -/* Define as 1 if we use unsigned int __atribute__ ((__mode__(TI))) for uint128 values */ +/* Define as 1 if we use unsigned int __attribute__ ((__mode__(TI))) for uint128 values */ #cmakedefine MMDB_UINT128_USING_MODE @MMDB_UINT128_USING_MODE@ #endif diff --git a/include/maxminddb_config.h.in b/include/maxminddb_config.h.in index 314d559d3..cec0f1091 100644 --- a/include/maxminddb_config.h.in +++ b/include/maxminddb_config.h.in @@ -2,7 +2,7 @@ #define MAXMINDDB_CONFIG_H #ifndef MMDB_UINT128_USING_MODE -/* Define as 1 if we use unsigned int __atribute__ ((__mode__(TI))) for uint128 values */ +/* Define as 1 if we use unsigned int __attribute__ ((__mode__(TI))) for uint128 values */ #define MMDB_UINT128_USING_MODE 0 #endif diff --git a/src/maxminddb.c b/src/maxminddb.c index 47e4ae151..55a3ce20b 100644 --- a/src/maxminddb.c +++ b/src/maxminddb.c @@ -947,7 +947,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; @@ -961,6 +961,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; diff --git a/t/maxmind-db b/t/maxmind-db index 31a33b3c0..880f6b4b5 160000 --- a/t/maxmind-db +++ b/t/maxmind-db @@ -1 +1 @@ -Subproject commit 31a33b3c09ac53028216e82f3d8a6d33749a8df5 +Subproject commit 880f6b4b5eb6c12ea9d5c70dd201dec2cb5639a2 diff --git a/t/metadata_t.c b/t/metadata_t.c index 8acf7c73b..a7e58560d 100644 --- a/t/metadata_t.c +++ b/t/metadata_t.c @@ -3,8 +3,8 @@ void test_metadata(MMDB_s *mmdb, const char *mode_desc) { cmp_ok(mmdb->metadata.node_count, "==", - 37, - "node_count is 37 - %s", + 163, + "node_count is 163 - %s", mode_desc); cmp_ok(mmdb->metadata.record_size, "==", @@ -197,7 +197,7 @@ void test_metadata_as_data_entry_list(MMDB_s *mmdb, const char *mode_desc) { if (strcmp(key_name, "node_count") == 0) { MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next; - cmp_ok(value->entry_data.uint32, "==", 37, "node_count == 37"); + cmp_ok(value->entry_data.uint32, "==", 163, "node_count == 163"); } else if (strcmp(key_name, "record_size") == 0) { MMDB_entry_data_list_s *value = entry_data_list = entry_data_list->next; diff --git a/t/read_node_t.c b/t/read_node_t.c index 8941ade61..da4ec8492 100644 --- a/t/read_node_t.c +++ b/t/read_node_t.c @@ -74,50 +74,55 @@ void run_24_bit_record_tests(int mode, const char *mode_desc) { MMDB_s *mmdb = open_ok(path, mode, mode_desc); free(path); - const uint32_t tests[7][5] = { - {0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY}, - { - 80, - 81, - MMDB_RECORD_TYPE_SEARCH_NODE, - 197, - MMDB_RECORD_TYPE_SEARCH_NODE, - }, - { - 96, - 97, - MMDB_RECORD_TYPE_SEARCH_NODE, - 242, - MMDB_RECORD_TYPE_EMPTY, - }, - { - 103, - 242, - MMDB_RECORD_TYPE_EMPTY, - 104, - MMDB_RECORD_TYPE_SEARCH_NODE, - }, - { - 127, - 242, - MMDB_RECORD_TYPE_EMPTY, - 315, - MMDB_RECORD_TYPE_DATA, - }, - { - 132, - 329, - MMDB_RECORD_TYPE_DATA, - 242, - MMDB_RECORD_TYPE_EMPTY, - }, - { - 241, - 96, - MMDB_RECORD_TYPE_SEARCH_NODE, - 242, - MMDB_RECORD_TYPE_EMPTY, - }}; + const uint32_t tests[7][5] = {{ + 0, + 1, + MMDB_RECORD_TYPE_SEARCH_NODE, + 435, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 80, + 81, + MMDB_RECORD_TYPE_SEARCH_NODE, + 323, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 96, + 97, + MMDB_RECORD_TYPE_SEARCH_NODE, + 148, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 103, + 444, + MMDB_RECORD_TYPE_EMPTY, + 104, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 127, + 444, + MMDB_RECORD_TYPE_EMPTY, + 514, + MMDB_RECORD_TYPE_DATA, + }, + { + 132, + 527, + MMDB_RECORD_TYPE_DATA, + 444, + MMDB_RECORD_TYPE_EMPTY, + }, + { + 241, + 444, + MMDB_RECORD_TYPE_EMPTY, + 242, + MMDB_RECORD_TYPE_SEARCH_NODE, + }}; run_read_node_tests(mmdb, tests, 7, 24); MMDB_close(mmdb); @@ -130,50 +135,55 @@ void run_28_bit_record_tests(int mode, const char *mode_desc) { MMDB_s *mmdb = open_ok(path, mode, mode_desc); free(path); - const uint32_t tests[7][5] = { - {0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY}, - { - 80, - 81, - MMDB_RECORD_TYPE_SEARCH_NODE, - 197, - MMDB_RECORD_TYPE_SEARCH_NODE, - }, - { - 96, - 97, - MMDB_RECORD_TYPE_SEARCH_NODE, - 242, - MMDB_RECORD_TYPE_EMPTY, - }, - { - 103, - 242, - MMDB_RECORD_TYPE_EMPTY, - 104, - MMDB_RECORD_TYPE_SEARCH_NODE, - }, - { - 127, - 242, - MMDB_RECORD_TYPE_EMPTY, - 315, - MMDB_RECORD_TYPE_DATA, - }, - { - 132, - 329, - MMDB_RECORD_TYPE_DATA, - 242, - MMDB_RECORD_TYPE_EMPTY, - }, - { - 241, - 96, - MMDB_RECORD_TYPE_SEARCH_NODE, - 242, - MMDB_RECORD_TYPE_EMPTY, - }}; + const uint32_t tests[7][5] = {{ + 0, + 1, + MMDB_RECORD_TYPE_SEARCH_NODE, + 435, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 80, + 81, + MMDB_RECORD_TYPE_SEARCH_NODE, + 323, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 96, + 97, + MMDB_RECORD_TYPE_SEARCH_NODE, + 148, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 103, + 444, + MMDB_RECORD_TYPE_EMPTY, + 104, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 127, + 444, + MMDB_RECORD_TYPE_EMPTY, + 514, + MMDB_RECORD_TYPE_DATA, + }, + { + 132, + 527, + MMDB_RECORD_TYPE_DATA, + 444, + MMDB_RECORD_TYPE_EMPTY, + }, + { + 241, + 444, + MMDB_RECORD_TYPE_EMPTY, + 242, + MMDB_RECORD_TYPE_SEARCH_NODE, + }}; run_read_node_tests(mmdb, tests, 7, 28); MMDB_close(mmdb); @@ -186,50 +196,55 @@ void run_32_bit_record_tests(int mode, const char *mode_desc) { MMDB_s *mmdb = open_ok(path, mode, mode_desc); free(path); - const uint32_t tests[7][5] = { - {0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242, MMDB_RECORD_TYPE_EMPTY}, - { - 80, - 81, - MMDB_RECORD_TYPE_SEARCH_NODE, - 197, - MMDB_RECORD_TYPE_SEARCH_NODE, - }, - { - 96, - 97, - MMDB_RECORD_TYPE_SEARCH_NODE, - 242, - MMDB_RECORD_TYPE_EMPTY, - }, - { - 103, - 242, - MMDB_RECORD_TYPE_EMPTY, - 104, - MMDB_RECORD_TYPE_SEARCH_NODE, - }, - { - 127, - 242, - MMDB_RECORD_TYPE_EMPTY, - 315, - MMDB_RECORD_TYPE_DATA, - }, - { - 132, - 329, - MMDB_RECORD_TYPE_DATA, - 242, - MMDB_RECORD_TYPE_EMPTY, - }, - { - 241, - 96, - MMDB_RECORD_TYPE_SEARCH_NODE, - 242, - MMDB_RECORD_TYPE_EMPTY, - }}; + const uint32_t tests[7][5] = {{ + 0, + 1, + MMDB_RECORD_TYPE_SEARCH_NODE, + 435, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 80, + 81, + MMDB_RECORD_TYPE_SEARCH_NODE, + 323, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 96, + 97, + MMDB_RECORD_TYPE_SEARCH_NODE, + 148, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 103, + 444, + MMDB_RECORD_TYPE_EMPTY, + 104, + MMDB_RECORD_TYPE_SEARCH_NODE, + }, + { + 127, + 444, + MMDB_RECORD_TYPE_EMPTY, + 514, + MMDB_RECORD_TYPE_DATA, + }, + { + 132, + 527, + MMDB_RECORD_TYPE_DATA, + 444, + MMDB_RECORD_TYPE_EMPTY, + }, + { + 241, + 444, + MMDB_RECORD_TYPE_EMPTY, + 242, + MMDB_RECORD_TYPE_SEARCH_NODE, + }}; run_read_node_tests(mmdb, tests, 7, 32);