Skip to content

Commit 1f2881a

Browse files
committed
Merge pull request #9 from maxmind/greg/coverity
Test with Coverity and fix potential errors
2 parents 5aa2c9f + 72520fe commit 1f2881a

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,17 @@ notifications:
3434
- dev@maxmind.com
3535
on_success: change
3636
on_failure: always
37+
38+
env:
39+
global:
40+
- secure: "pVcpV/al5Q607TbRzl/sbkdsx5hUjxehaJm6t5tgWrFn45icwdZrPw3JWcpt0R57NhPvXHxcJdm4WBtcGElWoDtR52QOW3yYh+gRw23y1MJg+5qHIbh5R1sOC/fLJ9TzQzvvRH5QQ5bKIe1hRQW9Cpqm7nX5Zhq6SqnAzcG1emE="
41+
42+
addons:
43+
coverity_scan:
44+
project:
45+
name: "maxmind/MaxMind-DB-Reader-python"
46+
description: "Build submitted via Travis CI"
47+
notification_email: dev@maxmind.com
48+
build_command_prepend: "python setup.py clean"
49+
build_command: "python setup.py build"
50+
branch_pattern: .*coverity.*

maxminddb/extension/maxminddb.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static int Reader_init(PyObject *self, PyObject *args, PyObject *UNUSED(kwds))
7272

7373
Reader_obj *mmdb_obj = (Reader_obj *)self;
7474
if (!mmdb_obj) {
75+
free(mmdb);
7576
PyErr_NoMemory();
7677
return -1;
7778
}
@@ -146,12 +147,6 @@ static PyObject *Reader_get(PyObject *self, PyObject *args)
146147
ip_address, MMDB_strerror(status));
147148
MMDB_free_entry_data_list(entry_data_list);
148149
return NULL;
149-
} else if (NULL == entry_data_list) {
150-
PyErr_Format(
151-
MaxMindDB_error,
152-
"Error while looking up data for %s. Your database may be corrupt or you have found a bug in libmaxminddb.",
153-
ip_address);
154-
return NULL;
155150
}
156151

157152
MMDB_entry_data_list_s *original_entry_data_list = entry_data_list;
@@ -300,6 +295,14 @@ static void Metadata_dealloc(PyObject *self)
300295

301296
static PyObject *from_entry_data_list(MMDB_entry_data_list_s **entry_data_list)
302297
{
298+
if (NULL == entry_data_list || NULL == *entry_data_list) {
299+
PyErr_SetString(
300+
MaxMindDB_error,
301+
"Error while looking up data. Your database may be corrupt or you have found a bug in libmaxminddb."
302+
);
303+
return NULL;
304+
}
305+
303306
switch ((*entry_data_list)->entry_data.type) {
304307
case MMDB_DATA_TYPE_MAP:
305308
return from_map(entry_data_list);
@@ -351,6 +354,9 @@ static PyObject *from_map(MMDB_entry_data_list_s **entry_data_list)
351354
const uint32_t map_size = (*entry_data_list)->entry_data.data_size;
352355

353356
uint i;
357+
// entry_data_list cannot start out NULL (see from_entry_data_list). We
358+
// check it in the loop because it may become NULL.
359+
// coverity[check_after_deref]
354360
for (i = 0; i < map_size && entry_data_list; i++) {
355361
*entry_data_list = (*entry_data_list)->next;
356362

@@ -386,6 +392,9 @@ static PyObject *from_array(MMDB_entry_data_list_s **entry_data_list)
386392
}
387393

388394
uint i;
395+
// entry_data_list cannot start out NULL (see from_entry_data_list). We
396+
// check it in the loop because it may become NULL.
397+
// coverity[check_after_deref]
389398
for (i = 0; i < size && entry_data_list; i++) {
390399
*entry_data_list = (*entry_data_list)->next;
391400
PyObject *value = from_entry_data_list(entry_data_list);
@@ -436,8 +445,8 @@ static PyMethodDef Reader_methods[] = {
436445
"Get record for IP address" },
437446
{ "metadata", Reader_metadata, METH_NOARGS,
438447
"Returns metadata object for database" },
439-
{ "close", Reader_close, METH_NOARGS, "Closes database" },
440-
{ NULL, NULL, 0, NULL }
448+
{ "close", Reader_close, METH_NOARGS, "Closes database"},
449+
{ NULL, NULL, 0, NULL }
441450
};
442451

443452
static PyTypeObject Reader_Type = {

0 commit comments

Comments
 (0)