NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: xmalloc.c issue (after xfree(cp), cp is used in irealloc())



On Mon, May 14, 2012 at 06:08:47PM +0530, amol pise wrote:
> Hi,
> 
> I used netBSD-5 for my system and during code observation
> I have a seen in the  xmalloc.c the pointer "cp"  is used after xfree(cp).
> How it is possible ? please see the code below
> 371                 return(cp);
> 372         } else
> 373                 xfree(cp);
> 374         if ((res = imalloc(nbytes)) == NULL)
> 375                 return (NULL);
> 376         if (cp != res)          /* common optimization if "compacting" */
> 377                 memcpy(res, cp, (nbytes < onb) ? nbytes : onb);

If I read correctly the code:

The memory is released but ithe address is still a valid one, and even
if the operation is not aomic, if the value of cp is returned as the
value of res, this means that [cp,cp+nbytes] has still the correct
content.

The only problem will appear if between the xfree() and the imalloc(),
code allocates between cp and cp+nbytes, change the content, and free
between cp and cp+nbytes.

So as long as the code is atomic, there is no problem. If it is not,
there can be. But not due to the instructions in the code by itself; due
to some concurrent memory allocations and freeings.

-- 
        Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
                      http://www.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C


Home | Main Index | Thread Index | Old Index