Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 15 Aug 2002 04:09:23 -0700 (PDT)
From:      Hovik Melikyan <hovik@melikyan.com>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   misc/41684: realloc() to 0 loses 16 bytes of memory regardless of the previous block size
Message-ID:  <200208151109.g7FB9NMM063962@www.freebsd.org>

next in thread | raw e-mail | index | archive | help

>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:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Aug 15 04:10:02 PDT 2002
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200208151109.g7FB9NMM063962>