Add a check for min data_section_size
This check is almost certainly not necessary due do how the database is
constructed, but it allows us to subtract 1, 2, or 3 from the
data_section_size with complete confidence that it won't underflow.
@@ -283,6 +283,16 @@ int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb)
}
mmdb->data_section_size = (uint32_t)mmdb->file_size - search_tree_size -
MMDB_DATA_SECTION_SEPARATOR;
+
+ // Although it is likely not possible to construct a database with valid
+ // valid metadata, as parsed above, and a data_section_size less than 3,
+ // we do this check as later we assume it is at least three when doing
+ // bound checks.
+ if (mmdb->data_section_size < 3) {
+ status = MMDB_INVALID_DATA_ERROR;
+ goto cleanup;
+ }
+
mmdb->metadata_section = metadata;
mmdb->ipv4_start_node.node_value = 0;
mmdb->ipv4_start_node.netmask = 0;
