Define behavior of malloc(0)

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.
pull/394/head
bradh352 4 years ago
parent 3c0bc079b4
commit 485fb660dc
  1. 9
      src/lib/ares_library_init.c
  2. 2
      test/ares-test.cc

@ -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

@ -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 {

Loading…
Cancel
Save