Skip to content

Commit

Permalink
Check all calloc/malloc return values. Closes #252.
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Feb 17, 2021
1 parent 367ffaa commit ec946c1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.5.1

* With `libmaxminddb` on Windows and `mmdblookup` generally, there were
instances where the return value of `calloc` was not checked, which could
lead to issues in low memory situations or when resource limits had been
set. Reported by cve-reporting. GitHub #252.
* The formatting of the manpages has been improved and the script that
generates them now supports `lowdown` in addition to `pandoc`. Pull request
by Faidon Liambotis. GitHub #248.
Expand Down
4 changes: 4 additions & 0 deletions bin/mmdblookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ static const char **get_options(int argc,

const char **lookup_path =
calloc((argc - optind) + 1, sizeof(const char *));
if (!lookup_path) {
fprintf(stderr, "calloc(): %s\n", strerror(errno));
exit(1);
}
int i;
for (i = 0; i < argc - optind; i++) {
lookup_path[i] = argv[i + optind];
Expand Down
3 changes: 3 additions & 0 deletions src/maxminddb.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb) {
static LPWSTR utf8_to_utf16(const char *utf8_str) {
int wide_chars = MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, NULL, 0);
wchar_t *utf16_str = (wchar_t *)calloc(wide_chars, sizeof(wchar_t));
if (!utf16_str) {
return NULL;
}

if (MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, utf16_str, wide_chars) <
1) {
Expand Down
3 changes: 3 additions & 0 deletions t/basic_lookup_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ void test_one_result(MMDB_s *mmdb,
// something like "::1.2.3.4", not just "1.2.3.4".
int maxlen = strlen(expect) + 3;
real_expect = malloc(maxlen);
if (!real_expect) {
BAIL_OUT("could not allocate memory");
}
snprintf(real_expect, maxlen, "::%s", expect);
}

Expand Down
3 changes: 3 additions & 0 deletions t/threads_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ void *run_one_thread(void *arg) {
const char *ip = thread_arg->ip_to_lookup;

test_result_s *result = malloc(sizeof(test_result_s));
if (!result) {
BAIL_OUT("could not allocate memory");
}
test_one_ip(mmdb, ip, result);

pthread_exit((void *)result);
Expand Down

0 comments on commit ec946c1

Please sign in to comment.