Package: cppcheck
Version: 1.34-1

cppcheck erroneously reports e memory leak when used by realloc in the code 
snippet shown below.

cppcheck -a test.c
Checking test.c...
[test.c:27]: (all) Memory leak: buf

test.c
~~~~

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

void func()
{
   char * buf, * new_buf;

   buf = calloc( 10000, sizeof(char) );

   if ( ! ( new_buf = realloc ( (char*)buf, 20000) ) )
     free(buf); // ENOMEM
   else
     free(new_buf); // OK
}

int main()
{
   while(1)
     func();
   return 0;
}


If the area pointed to by ptr was moved by realloc(), or in other words the  
the reallocation succeeds, a free(ptr) is called internally, hence there is no 
a memory leak with `buf', and we should free(new_buf).

If the reallocation fails the original block is left untouched, i.e. not freed 
or moved, hence we should free(buf), and should *not* call free(new_buf);


thanks

-- 
pub 4096R/0E4BD0AB 2003-03-18 <people.fccf.net/danchev/key pgp.mit.edu>



-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to