libmaxminddb/backport-Check-all-calloc-malloc-return-values.patch

58 lines
1.8 KiB
Diff
Raw Permalink Normal View History

2021-07-24 09:59:19 +08:00
From ec946c10d7bdad4215185b49d672d1508e0af4b1 Mon Sep 17 00:00:00 2001
From: Gregory Oschwald <goschwald@maxmind.com>
Date: Wed, 17 Feb 2021 14:18:06 -0800
Subject: [PATCH] Check all calloc/malloc return values. Closes #252.
---
bin/mmdblookup.c | 4 ++++
t/basic_lookup_t.c | 3 +++
t/threads_t.c | 3 +++
3 files changed, 10 insertions(+)
diff --git a/bin/mmdblookup.c b/bin/mmdblookup.c
index 7f78773..9a8eb67 100644
--- a/bin/mmdblookup.c
+++ b/bin/mmdblookup.c
@@ -185,6 +185,10 @@ LOCAL const char **get_options(int argc, char **argv, char **mmdb_file,
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];
diff --git a/t/basic_lookup_t.c b/t/basic_lookup_t.c
index 7855919..5da0673 100644
--- a/t/basic_lookup_t.c
+++ b/t/basic_lookup_t.c
@@ -31,6 +31,9 @@ void test_one_result(MMDB_s *mmdb, MMDB_lookup_result_s result,
// 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);
}
diff --git a/t/threads_t.c b/t/threads_t.c
index 23cd0ee..be060d3 100644
--- a/t/threads_t.c
+++ b/t/threads_t.c
@@ -68,6 +68,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);
--
1.8.3.1