From nobody@FreeBSD.org  Thu Aug 15 04:09:24 2002
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 0555D37B400
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 Aug 2002 04:09:24 -0700 (PDT)
Received: from www.freebsd.org (www.FreeBSD.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id BC21043E70
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 Aug 2002 04:09:23 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.4/8.12.4) with ESMTP id g7FB9NOT063963
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 Aug 2002 04:09:23 -0700 (PDT)
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.4/8.12.4/Submit) id g7FB9NMM063962;
	Thu, 15 Aug 2002 04:09:23 -0700 (PDT)
Message-Id: <200208151109.g7FB9NMM063962@www.freebsd.org>
Date: Thu, 15 Aug 2002 04:09:23 -0700 (PDT)
From: Hovik Melikyan <hovik@melikyan.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: realloc() to 0 loses 16 bytes of memory regardless of the previous block size
X-Send-Pr-Version: www-1.0

>Number:         41684
>Category:       misc
>Synopsis:       realloc() to 0 loses 16 bytes of memory regardless of the previous block size
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Aug 15 04:10:02 PDT 2002
>Closed-Date:    Thu Aug 15 06:11:46 PDT 2002
>Last-Modified:  Thu Aug 15 06:11:46 PDT 2002
>Originator:     Hovik Melikyan
>Release:        4.6-RELEASE, also 4.5-PRERELEASE
>Organization:
NetFlute.com
>Environment:
FreeBSD london.imc.am 4.6-RELEASE FreeBSD 4.6-RELEASE #0: Tue Jun 11 06:14:12 GMT 2002     murray@builder.freebsdmall.com:/usr/src/sys/compile/GENERIC  i386

>Description:
realloc() with the size parameter set to 0 loses 16 bytes of memory regardless of the previous block size, e.g.

    p = realloc(p, 0);
>How-To-Repeat:
run this program and watch its resources with top:

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

int main()
{
    int i, j;
    void *p;

    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 1000; j++)
        {
            p = malloc(12345);  /* the block size doesn't matter */
            p = realloc(p, 0);  /* replace this with free(p) to avoid memory leak */
                                /* here we lose 16 bytes of memory */
        }
        puts("Press any key:");
        getchar();
    }
    return 0;
}

>Fix:
write your own wrapper for realloc() which would check the size parameter and call free() if it's 0.
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: fanf 
State-Changed-When: Thu Aug 15 06:09:34 PDT 2002 
State-Changed-Why:  
Not a bug. 
Both malloc(0) and realloc(p, 0) may return non-NULL, in 
which case the pointer must be free()ed in order to prevent 
a memory leak. 

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