Subject: bug#17499: closed (Re: bug#17499: current dfa.c can malloc 0 bytes)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 15 May 2014 19:45:03 +0000

Your bug report

#17499: current dfa.c can malloc 0 bytes

which was filed against the grep package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
17499: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17499
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#17499: current dfa.c can malloc 0 bytes
From: Paul Eggert <[email-masked]>
To: Aharon Robbins <[email-masked]>
Cc: [email-masked]
Date: Thu, 15 May 2014 12:44:25 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

On 05/15/2014 10:34 AM, Aharon Robbins wrote:
> The wrapper only checks for NULL return.

Ah, thanks, that's the problem then: gawk's xmalloc is pickier than 
grep's, and the extra pickiness is incompatible with what dfa.c 
expects.  I see that gawk's xrealloc is also too picky, but gawk's 
xcalloc is OK.  Attached isa patch to gawk.  As this problem does not 
affect grep I'll close the grep bug.

>From 77b003848b85f83537b3f618cb0a810447329500 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Thu, 15 May 2014 12:37:48 -0700
Subject: [PATCH] Port to systems where malloc (0) and/or realloc(P, 0) returns
 NULL.

* gawkmisc.c (xmalloc):
* xalloc.h (realloc):
Do not fail if malloc(0) or realloc(P, 0) returns NULL.
Fail only when the allocator returns null when attempting to
allocate a nonzero number of bytes.
---
 ChangeLog  | 9 +++++++++
 gawkmisc.c | 5 +++--
 xalloc.h   | 2 +-
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 45125c1..20dac99 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2014-05-15  Paul Eggert  <[email-masked]>
+
+	Port to systems where malloc (0) and/or realloc(P, 0) returns NULL.
+	* gawkmisc.c (xmalloc):
+	* xalloc.h (realloc):
+	Do not fail if malloc(0) or realloc(P, 0) returns NULL.
+	Fail only when the allocator returns null when attempting to
+	allocate a nonzero number of bytes.
+
 2014-05-14         Arnold D. Robbins     <[email-masked]>
 
 	* custom.h (_GL_PURE): Move definition to here. Sigh.
diff --git a/gawkmisc.c b/gawkmisc.c
index a729d88..fff5cc5 100644
--- a/gawkmisc.c
+++ b/gawkmisc.c
@@ -51,7 +51,8 @@ extern pointer xmalloc(size_t bytes);	/* get rid of gcc warning */
 pointer
 xmalloc(size_t bytes)
 {
-	pointer p;
-	emalloc(p, pointer, bytes, "xmalloc");
+	pointer p = malloc(bytes);
+	if (!p && bytes)
+		xalloc_die ();	  
 	return p;
 }
diff --git a/xalloc.h b/xalloc.h
index 0d169cf..5ee4516 100644
--- a/xalloc.h
+++ b/xalloc.h
@@ -156,7 +156,7 @@ void *
 xrealloc(void *p, size_t size)
 {
    void *new_p = realloc(p, size);
-   if (new_p ==  0)
+   if (!new_p && size)
      xalloc_die ();
 
    return new_p;
-- 
1.9.0

Subject: current dfa.c can malloc 0 bytes
From: Aharon Robbins <[email-masked]>
To: [email-masked]
Date: Thu, 15 May 2014 19:22:40 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello.

It seems that code in the grep master dfa.c can call xmalloc with
an amount that is zero.  This is OK on GLIBC but is causing some failures
on other systems where malloc(0) fails.

This was not the case a while back (when gawk 4.1.1 was released in
April or so).

It'd be nice if this could be fixed sometime.  To test this in gawk,

Apply the patch below to either master or gawk-4.1-stable from the git repo
and rebuild, and make check.  Gawk 4.1.1 doesn't have this problem.

Thanks

Arnold
---------------
diff --git a/gawkmisc.c b/gawkmisc.c
index a729d88..b07281d 100644
--- a/gawkmisc.c
+++ b/gawkmisc.c
@@ -52,6 +52,8 @@ pointer
 xmalloc(size_t bytes)
 {
 	pointer p;
+	if (bytes == 0)
+		fprintf(stderr, "%s: 0 bytes!\n", __func__), fflush(stderr);
 	emalloc(p, pointer, bytes, "xmalloc");
 	return p;
 }