If malloc, calloc, or realloc returns NULL when a size of 0 was

requested, which is allowed by pertinent standards, honor it instead
of bombing.

Do not do this for calloc(x, y) where x != 0 && y != 0 but x*y == 0;
in that case bomb.
This commit is contained in:
dholland 2012-12-30 17:36:00 +00:00
parent c18b7a38ac
commit 1e653dd9c8
1 changed files with 5 additions and 5 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: efun.c,v 1.6 2008/04/28 20:23:02 martin Exp $ */
/* $NetBSD: efun.c,v 1.7 2012/12/30 17:36:00 dholland Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
@ -35,7 +35,7 @@
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$NetBSD: efun.c,v 1.6 2008/04/28 20:23:02 martin Exp $");
__RCSID("$NetBSD: efun.c,v 1.7 2012/12/30 17:36:00 dholland Exp $");
#endif
#include <err.h>
@ -104,7 +104,7 @@ void *
emalloc(size_t n)
{
void *p = malloc(n);
if (p == NULL)
if (p == NULL && n != 0)
(*efunc)(1, "Cannot allocate %zu bytes", n);
return p;
}
@ -113,7 +113,7 @@ void *
ecalloc(size_t n, size_t s)
{
void *p = calloc(n, s);
if (p == NULL)
if (p == NULL && n != 0 && s != 0)
(*efunc)(1, "Cannot allocate %zu bytes", n);
return p;
}
@ -122,7 +122,7 @@ void *
erealloc(void *p, size_t n)
{
void *q = realloc(p, n);
if (q == NULL)
if (q == NULL && n != 0)
(*efunc)(1, "Cannot re-allocate %zu bytes", n);
return q;
}