Skip to content

Commit

Permalink
Merge pull request #59 from maxmind/greg/fix-segfaults
Browse files Browse the repository at this point in the history
Fix segfault on invalid UTF-8. Closes #58.
  • Loading branch information
horgh authored May 4, 2020
2 parents fc3c57d + 79755e2 commit 3223e53
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
History
-------

1.5.3 (2020-05-04)
++++++++++++++++++

* Fix a segfault when decoding a database with a corrupt data section.
Reported by Robert Scott. GitHub #58.

1.5.2 (2019-12-20)
++++++++++++++++++

Expand Down
10 changes: 10 additions & 0 deletions extension/maxminddb.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ static int get_record(PyObject *self, PyObject *args, PyObject **record) {
*record = from_entry_data_list(&entry_data_list);
MMDB_free_entry_data_list(original_entry_data_list);

// from_entry_data_list will return NULL on errors.
if (*record == NULL) {
return -1;
}

return prefix_len;
}

Expand Down Expand Up @@ -526,6 +531,11 @@ static PyObject *from_map(MMDB_entry_data_list_s **entry_data_list) {
PyObject *key = PyUnicode_FromStringAndSize(
(char *)(*entry_data_list)->entry_data.utf8_string,
(*entry_data_list)->entry_data.data_size);
if (!key) {
// PyUnicode_FromStringAndSize will set an appropriate exception
// in this case.
return NULL;
}

*entry_data_list = (*entry_data_list)->next;

Expand Down
2 changes: 1 addition & 1 deletion tests/data
Submodule data updated 39 files
+ bad-data/maxminddb-python/bad-unicode-in-map-key.mmdb
+1 −1 source-data/GeoIP2-Anonymous-IP-Test.json
+488 −488 source-data/GeoIP2-City-Test.json
+4,383 −17 source-data/GeoIP2-Country-Test.json
+48 −34 source-data/GeoIP2-Enterprise-Test.json
+112 −69 source-data/GeoIP2-Precision-Enterprise-Test.json
+2,132 −0 source-data/GeoIP2-Static-IP-Score-Test.json
+2,832 −2,832 source-data/GeoIP2-User-Count-Test.json
+ test-data/GeoIP2-Anonymous-IP-Test.mmdb
+ test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb
+ test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb
+ test-data/GeoIP2-City-Test.mmdb
+ test-data/GeoIP2-Connection-Type-Test.mmdb
+ test-data/GeoIP2-Country-Test.mmdb
+ test-data/GeoIP2-DensityIncome-Test.mmdb
+ test-data/GeoIP2-Domain-Test.mmdb
+ test-data/GeoIP2-Enterprise-Test.mmdb
+ test-data/GeoIP2-ISP-Test.mmdb
+ test-data/GeoIP2-Precision-Enterprise-Test.mmdb
+ test-data/GeoIP2-Static-IP-Score-Test.mmdb
+ test-data/GeoIP2-User-Count-Test.mmdb
+ test-data/GeoLite2-ASN-Test.mmdb
+ test-data/MaxMind-DB-no-ipv4-search-tree.mmdb
+ test-data/MaxMind-DB-string-value-entries.mmdb
+ test-data/MaxMind-DB-test-broken-pointers-24.mmdb
+ test-data/MaxMind-DB-test-broken-search-tree-24.mmdb
+ test-data/MaxMind-DB-test-decoder.mmdb
+ test-data/MaxMind-DB-test-ipv4-24.mmdb
+ test-data/MaxMind-DB-test-ipv4-28.mmdb
+ test-data/MaxMind-DB-test-ipv4-32.mmdb
+ test-data/MaxMind-DB-test-ipv6-24.mmdb
+ test-data/MaxMind-DB-test-ipv6-28.mmdb
+ test-data/MaxMind-DB-test-ipv6-32.mmdb
+ test-data/MaxMind-DB-test-metadata-pointers.mmdb
+ test-data/MaxMind-DB-test-mixed-24.mmdb
+ test-data/MaxMind-DB-test-mixed-28.mmdb
+ test-data/MaxMind-DB-test-mixed-32.mmdb
+ test-data/MaxMind-DB-test-nested.mmdb
+20 −17 test-data/write-test-data.pl
9 changes: 9 additions & 0 deletions tests/reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@ def test_nondatabase(self):
):
open_database("README.rst", self.mode)

# This is from https://github.com/maxmind/MaxMind-DB-Reader-python/issues/58
def test_database_with_invalid_utf8_key(self):
reader = open_database(
"tests/data/bad-data/maxminddb-python/bad-unicode-in-map-key.mmdb",
self.mode,
)
with self.assertRaises(UnicodeDecodeError):
reader.get_with_prefix_len("163.254.149.39")

def test_too_many_constructor_args(self):
with self.assertRaises(TypeError):
self.readerClass[0]("README.md", self.mode, 1)
Expand Down

0 comments on commit 3223e53

Please sign in to comment.