From c73912278bdfa4cf5e9f8352962d0b7b03662092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 12 Nov 2018 11:50:52 +0100 Subject: [PATCH 1/3] Abort on memory allocation failure (cherry picked from commit 8de2451756d7baaaf842a300d35b00a8d8b22cb0) --- lib/isc/mem.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 41383ed7ef..1f73805f6d 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -28,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -860,10 +862,36 @@ mem_putstats(isc__mem_t *ctx, void *ptr, size_t size) { static void * default_memalloc(void *arg, size_t size) { + void *ptr; UNUSED(arg); - if (size == 0U) + + if (size == 0U) { size = 1; - return (malloc(size)); + } + + ptr = malloc(size); + + /* + * If the space cannot be allocated, a null pointer is returned. If the + * size of the space requested is zero, the behavior is + * implementation-defined: either a null pointer is returned, or the + * behavior is as if the size were some nonzero value, except that the + * returned pointer shall not be used to access an object. + * [ISO9899 ยง 7.22.3] + * + * [ISO9899] + * ISO/IEC WG 9899:2011: Programming languages - C. + * International Organization for Standardization, Geneva, Switzerland. + * http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf + */ + + if (ptr == NULL && size != 0) { + char strbuf[ISC_STRERRORSIZE]; + strerror_r(errno, strbuf, sizeof(strbuf)); + isc_error_fatal(__FILE__, __LINE__, "malloc failed: %s", strbuf); + } + + return (ptr); } static void From 7d9e19b5eb1ec0fd073c8b0530a3ac9cdeb52171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 20 Dec 2018 11:20:10 +0100 Subject: [PATCH 2/3] Abort on allocation failure only if the memory functions are used internally in BIND 9 (cherry picked from commit c22241ae9009391c1d28085c5cf0009a6caef09c) --- lib/isc/mem.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 1f73805f6d..49b61c994c 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -862,6 +862,17 @@ mem_putstats(isc__mem_t *ctx, void *ptr, size_t size) { static void * default_memalloc(void *arg, size_t size) { + UNUSED(arg); + + if (size == 0U) { + size = 1; + } + + return (malloc(size)); +} + +static void * +internal_memalloc(void *arg, size_t size) { void *ptr; UNUSED(arg); @@ -2735,7 +2746,7 @@ isc_mem_create(size_t init_max_size, size_t target_size, isc_mem_t **mctxp) { if (isc_bind9) return (isc_mem_createx2(init_max_size, target_size, - default_memalloc, default_memfree, + internal_memalloc, default_memfree, NULL, mctxp, isc_mem_defaultflags)); LOCK(&createlock); @@ -2754,7 +2765,7 @@ isc_mem_create2(size_t init_max_size, size_t target_size, isc_mem_t **mctxp, { if (isc_bind9) return (isc_mem_createx2(init_max_size, target_size, - default_memalloc, default_memfree, + internal_memalloc, default_memfree, NULL, mctxp, flags)); return (isc_mem_createx2(init_max_size, target_size, From 8d532c111bff04311afce6920a0a8749ce3e4d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 12 Nov 2018 12:00:07 +0100 Subject: [PATCH 3/3] Add CHANGES entry for GL #674 (cherry picked from commit 37ff7f635b93ef6fd3c98e5432151e807ecb1cd3) --- CHANGES | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES b/CHANGES index 4e23fa28ad..d18ec89337 100644 --- a/CHANGES +++ b/CHANGES @@ -25,6 +25,8 @@ 5101. [bug] Fix default installation path for Python modules. [GL #730] +5098. [func] Failed memory allocations are now fatal. [GL #674] + 5097. [cleanup] Remove embedded ATF unit testing framework from BIND source distribution. [GL !875]