From stefan@fafoe.narf.at  Sat Feb 14 16:20:35 2004
Return-Path: <stefan@fafoe.narf.at>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP
	id 1F6AB16A4CE; Sat, 14 Feb 2004 16:20:35 -0800 (PST)
Received: from laika.ifs.tuwien.ac.at (laika.ifs.tuwien.ac.at [128.131.167.43])
	by mx1.FreeBSD.org (Postfix) with ESMTP
	id D1F2443D1F; Sat, 14 Feb 2004 16:20:34 -0800 (PST)
	(envelope-from stefan@fafoe.narf.at)
Received: from fafoe.narf.at (unknown [212.186.3.235])
	by laika.ifs.tuwien.ac.at (Postfix) with ESMTP
	id 21CC620A1; Sun, 15 Feb 2004 01:22:33 +0100 (CET)
Received: from wombat.fafoe.narf.at (wombat.fafoe.narf.at [192.168.1.42])
	by fafoe.narf.at (Postfix) with ESMTP
	id 0F05340EE; Sun, 15 Feb 2004 01:20:27 +0100 (CET)
Received: by wombat.fafoe.narf.at (Postfix, from userid 1001)
	id 0939A32A; Sun, 15 Feb 2004 01:20:26 +0100 (CET)
Message-Id: <20040215002026.0939A32A@wombat.fafoe.narf.at>
Date: Sun, 15 Feb 2004 01:20:26 +0100 (CET)
From: Stefan Farfeleder <stefan@fafoe.narf.at>
Reply-To: Stefan Farfeleder <stefan@fafoe.narf.at>
To: FreeBSD-gnats-submit@freebsd.org
Cc: stefan@fafoe.narf.at, marcel@freebsd.org
Subject: [patch] malloc(0) fails to call malloc_init()
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         62859
>Category:       bin
>Synopsis:       [patch] malloc(0) fails to call malloc_init()
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    phk
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Feb 14 16:30:17 PST 2004
>Closed-Date:    Sun Mar 07 12:41:47 PST 2004
>Last-Modified:  Sun Mar 07 12:41:47 PST 2004
>Originator:     Stefan Farfeleder
>Release:        FreeBSD 5.2-CURRENT i386
>Organization:
>Environment:
System: FreeBSD wombat.fafoe.narf.at 5.2-CURRENT FreeBSD 5.2-CURRENT #13: Thu Feb 5 23:10:05 CET 2004 stefan@wombat.fafoe.narf.at:/usr/home/stefan/freebsd/obj/usr/home/stefan/freebsd/src/sys/WOMBAT i386

>Description:
The function malloc_init() parses malloc()'s options from /etc/malloc.conf,
MALLOC_OPTIONS and _malloc_options.  It's the function imalloc() that calls
malloc_init(), and the former one is not called on malloc(0).  This isn't a
problem per se, but the v/V flag controls the behaviour of malloc(0) and so it
never returns a null pointer until malloc() is called with a positived size.

>How-To-Repeat:
This program demonstrates that the V flag is ignored:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
	_malloc_options = "V";
	printf("malloc(0) = %p\n", malloc(0));
	return (0);
}

>Fix:
This moves the malloc_init() calls into malloc() and realloc().
--- malloc.c.diff begins here ---
Index: src/lib/libc/stdlib/malloc.c
===================================================================
RCS file: /usr/home/ncvs/src/lib/libc/stdlib/malloc.c,v
retrieving revision 1.84
diff -I.svn -u -r1.84 malloc.c
--- src/lib/libc/stdlib/malloc.c	28 Nov 2003 18:03:22 -0000	1.84
+++ src/lib/libc/stdlib/malloc.c	14 Feb 2004 23:42:47 -0000
@@ -736,9 +736,6 @@
 {
     void *result;
 
-    if (!malloc_started)
-	malloc_init();
-
     if (suicide)
 	abort();
 
@@ -1111,6 +1108,9 @@
 {
     void *r;
 
+    if (!malloc_started)
+	malloc_init();
+
     _MALLOC_LOCK();
     malloc_func = " in malloc():";
     if (malloc_active++) {
@@ -1161,6 +1161,9 @@
 {
     void *r;
     int err = 0;
+
+    if (!malloc_started)
+	malloc_init();
 
     _MALLOC_LOCK();
     malloc_func = " in realloc():";
--- malloc.c.diff ends here ---
>Release-Note:
>Audit-Trail:

From: Stefan Farfeleder <stefan@fafoe.narf.at>
To: bug-followup@freebsd.org
Cc:  
Subject: Re: bin/62859: [patch] malloc(0) fails to call malloc_init()
Date: Tue, 2 Mar 2004 23:27:16 +0100

 --GPJrCs/72TxItFYR
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 The patch in the PR is OBE, here's a new one.
 
 Stefan
 
 --GPJrCs/72TxItFYR
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename="malloc.c.diff"
 
 Index: src/lib/libc/stdlib/malloc.c
 ===================================================================
 RCS file: /usr/home/ncvs/src/lib/libc/stdlib/malloc.c,v
 retrieving revision 1.86
 diff -I.svn -u -r1.86 malloc.c
 --- src/lib/libc/stdlib/malloc.c	21 Feb 2004 09:14:38 -0000	1.86
 +++ src/lib/libc/stdlib/malloc.c	23 Feb 2004 13:39:59 -0000
 @@ -729,9 +729,6 @@
  {
      void *result;
  
 -    if (!malloc_started)
 -	malloc_init();
 -
      if (suicide)
  	abort();
  
 @@ -764,11 +761,6 @@
      if (suicide)
  	abort();
  
 -    if (!malloc_started) {
 -	wrtwarning("malloc() has never been called\n");
 -	return (NULL);
 -    }
 -
      index = ptr2index(ptr);
  
      if (index < malloc_pageshift) {
 @@ -1061,11 +1053,6 @@
      if (ptr == NULL)
  	return;
  
 -    if (!malloc_started) {
 -	wrtwarning("malloc() has never been called\n");
 -	return;
 -    }
 -
      /* If we're already sinking, don't make matters any worse. */
      if (suicide)
  	return;
 @@ -1118,6 +1105,13 @@
     
      if (ptr == ZEROSIZEPTR)
  	ptr = NULL;
 +    if (!malloc_started) {
 +	malloc_init();
 +	if (ptr != NULL) {
 +	    wrtwarning("malloc() has never been called\n");
 +	    return (NULL);
 +	}
 +    }
      if (malloc_sysv && !size) {
  	if (ptr != NULL)
  	    ifree(ptr);
 
 --GPJrCs/72TxItFYR--
Responsible-Changed-From-To: freebsd-bugs->phk 
Responsible-Changed-By: kris 
Responsible-Changed-When: Sat Mar 6 00:48:01 PST 2004 
Responsible-Changed-Why:  
Assign to malloc author 

http://www.freebsd.org/cgi/query-pr.cgi?pr=62859 
State-Changed-From-To: open->closed 
State-Changed-By: phk 
State-Changed-When: Sun Mar 7 12:41:38 PST 2004 
State-Changed-Why:  


http://www.freebsd.org/cgi/query-pr.cgi?pr=62859 
>Unformatted:
