diff --git a/src/lib/ares_library_init.c b/src/lib/ares_library_init.c index 67563499..e0055d44 100644 --- a/src/lib/ares_library_init.c +++ b/src/lib/ares_library_init.c @@ -40,13 +40,18 @@ static unsigned int ares_initialized; static int ares_init_flags; /* library-private global vars with visibility across the whole library */ + +/* Some systems may return either NULL or a valid pointer on malloc(0). c-ares should + * never call malloc(0) so lets return NULL so we're more likely to find an issue if it + * were to occur. */ + +static void *default_malloc(size_t size) { if (size == 0) { return NULL; } return malloc(size); } + #if defined(WIN32) /* We need indirections to handle Windows DLL rules. */ -static void *default_malloc(size_t size) { return malloc(size); } static void *default_realloc(void *p, size_t size) { return realloc(p, size); } static void default_free(void *p) { free(p); } #else -# define default_malloc malloc # define default_realloc realloc # define default_free free #endif diff --git a/test/ares-test.cc b/test/ares-test.cc index 9cfb64b3..0dae8fa5 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -135,7 +135,7 @@ bool LibraryTest::ShouldAllocFail(size_t size) { // static void* LibraryTest::amalloc(size_t size) { - if (ShouldAllocFail(size)) { + if (ShouldAllocFail(size) || size == 0) { if (verbose) std::cerr << "Failing malloc(" << size << ") request" << std::endl; return nullptr; } else {