Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libutil If malloc, calloc, or realloc returns NULL when ...



details:   https://anonhg.NetBSD.org/src/rev/7ef08c361340
branches:  trunk
changeset: 783582:7ef08c361340
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sun Dec 30 17:36:00 2012 +0000

description:
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.

diffstat:

 lib/libutil/efun.c |  10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diffs (45 lines):

diff -r adf6c08b5766 -r 7ef08c361340 lib/libutil/efun.c
--- a/lib/libutil/efun.c        Sun Dec 30 16:13:57 2012 +0000
+++ b/lib/libutil/efun.c        Sun Dec 30 17:36:00 2012 +0000
@@ -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 @@
 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 @@
 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 @@
 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;
 }



Home | Main Index | Thread Index | Old Index