From nobody@FreeBSD.org  Wed Aug  1 17:10:24 2001
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21])
	by hub.freebsd.org (Postfix) with ESMTP id A891637B401
	for <freebsd-gnats-submit@FreeBSD.org>; Wed,  1 Aug 2001 17:10:20 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.11.4/8.11.4) id f720AKf99349;
	Wed, 1 Aug 2001 17:10:20 -0700 (PDT)
	(envelope-from nobody)
Message-Id: <200108020010.f720AKf99349@freefall.freebsd.org>
Date: Wed, 1 Aug 2001 17:10:20 -0700 (PDT)
From: Farooq Mela <fmela0@sm.socccd.cc.ca.us>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Realloc() doesn't (by default) comply with the ANSI C standard, and realloc(ptr, 0) with malloc_sysv set will cause an out of memory error if malloc_xmalloc is also set.
X-Send-Pr-Version: www-1.0

>Number:         29376
>Category:       misc
>Synopsis:       Realloc() doesn't (by default) comply with the ANSI C standard, and realloc(ptr, 0) with malloc_sysv set will cause an out of memory error if malloc_xmalloc is also set.
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    phk
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Aug 01 17:20:19 PDT 2001
>Closed-Date:    Sat Nov 17 02:57:10 PST 2001
>Last-Modified:  Sat Nov 17 02:58:08 PST 2001
>Originator:     Farooq Mela
>Release:        4.3-STABLE
>Organization:
>Environment:
FreeBSD apollo 4.3-STABLE FreeBSD 4.3-STABLE #4: Sun Jul 15 00:58:15 PDT 2001     farooq@apollo:/usr/src/sys/compile/APOLLO  i386
>Description:
The ANSI C standard requires that realloc(ptr, 0) behave the same as free(ptr). This behaviour is available on FreeBSD only if the malloc_options includes 'V' (sysv-style). Secondly, if both 'V' and 'X' are in malloc_options, specifying SysV-style and abort-on-out-of-memory behaviour, then realloc(ptr, 0) will result in realloc free'ing the pointer, and then thinking it is out of memory and abort()ing.
>How-To-Repeat:
#inlude <stdlib.h>

int
main(void)
{
	extern char *malloc_options;
	void *p=NULL;

	malloc_options="VX";	/* set malloc flags */
	p=realloc(p, 50); /* allocate 50 bytes */
	p=realloc(p, 0);  /* this will cause realloc to abort() */
	exit(0);
}
>Fix:
Change line 1132 of /usr/src/lib/libc/stdlib/malloc.c:

-	if (malloc_xmalloc && !r)
+	if (malloc_xmalloc && !r && size)

Change line 1121:

-	if (malloc_sysv && !size)
+	if (ptr && !size)

This will cause the implementation to conform with ANSI as well as fix the problem where it thinks it is out of memory when it is really just freeing the pointer.
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->phk 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Thu Aug 2 02:13:38 PDT 2001 
Responsible-Changed-Why:  
Malloc is phk's baby. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=29376 
State-Changed-From-To: open->closed 
State-Changed-By: phk 
State-Changed-When: Sat Nov 17 02:57:10 PST 2001 
State-Changed-Why:  
I've fixed the realloc(foo,0) problem with option VX 

The other issue (return value of malloc(0)) breaks too much 
software to be changed currently. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=29376 
>Unformatted:
