Skip to content

Commit 3223e53

Browse files
authored
Merge pull request #59 from maxmind/greg/fix-segfaults
Fix segfault on invalid UTF-8. Closes #58.
2 parents fc3c57d + 79755e2 commit 3223e53

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

HISTORY.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
History
44
-------
55

6+
1.5.3 (2020-05-04)
7+
++++++++++++++++++
8+
9+
* Fix a segfault when decoding a database with a corrupt data section.
10+
Reported by Robert Scott. GitHub #58.
11+
612
1.5.2 (2019-12-20)
713
++++++++++++++++++
814

extension/maxminddb.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ static int get_record(PyObject *self, PyObject *args, PyObject **record) {
204204
*record = from_entry_data_list(&entry_data_list);
205205
MMDB_free_entry_data_list(original_entry_data_list);
206206

207+
// from_entry_data_list will return NULL on errors.
208+
if (*record == NULL) {
209+
return -1;
210+
}
211+
207212
return prefix_len;
208213
}
209214

@@ -526,6 +531,11 @@ static PyObject *from_map(MMDB_entry_data_list_s **entry_data_list) {
526531
PyObject *key = PyUnicode_FromStringAndSize(
527532
(char *)(*entry_data_list)->entry_data.utf8_string,
528533
(*entry_data_list)->entry_data.data_size);
534+
if (!key) {
535+
// PyUnicode_FromStringAndSize will set an appropriate exception
536+
// in this case.
537+
return NULL;
538+
}
529539

530540
*entry_data_list = (*entry_data_list)->next;
531541

tests/data

Submodule data updated 39 files

tests/reader_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@ def test_nondatabase(self):
286286
):
287287
open_database("README.rst", self.mode)
288288

289+
# This is from https://github.com/maxmind/MaxMind-DB-Reader-python/issues/58
290+
def test_database_with_invalid_utf8_key(self):
291+
reader = open_database(
292+
"tests/data/bad-data/maxminddb-python/bad-unicode-in-map-key.mmdb",
293+
self.mode,
294+
)
295+
with self.assertRaises(UnicodeDecodeError):
296+
reader.get_with_prefix_len("163.254.149.39")
297+
289298
def test_too_many_constructor_args(self):
290299
with self.assertRaises(TypeError):
291300
self.readerClass[0]("README.md", self.mode, 1)

0 commit comments

Comments
 (0)