[PATCH][BZ #1349] Return requested size for malloc_usable_size when MALLOC_CHECK_ > 0

Andreas Jaeger aj@suse.com
Fri Aug 31 12:38:00 GMT 2012


On 08/31/2012 08:02 AM, Siddhesh Poyarekar wrote:
> Hi,
>
> malloc_usable_size returns the usable size in an allocated chunk, which
> may be >= the requested size. In the case of MALLOC_CHECK_ being
> exported to > 0 however, only the requested size is usable, since a
> magic value is written at the end of the request size to trap writes
> beyond request bounds. Hence, when MALLOC_CHECK_ is exported to > 0,
> malloc_usable_size() should return the request size. The attached patch
> does exactly that.
>
> There is also a test case included in the patch to verify that the
> problem is fixed. I have verified on Fedora-16 x86_64 that this does
> not cause any regressions.
>
> Regards,
> Siddhesh
>
> ChangeLog:
>
> 	* malloc/Makefile (tests): Add tst-malloc-usable test case.
> 	(tst-malloc-usable-ENV): Set environment for test case.
> 	* malloc/hooks.c (malloc_check_get_size): New function to get
> 	requested size.
> 	* malloc/malloc.c (musable): Use malloc_check_get_size.
> 	* malloc/tst-malloc-usable.c: New test case.
>

 > [...]
> +static int
> +do_test (void)
> +{
> +  void *p = malloc (7);
> +  if (!p)
> +    {
> +      printf ("memory allocation failed\n");
> +      return 1;
> +    }
> +  memset (p, 0, malloc_usable_size (p));
> +  free (p);
> +  return 0;

In this case malloc_usable_size () should return >= 7, so I suggest to 
check for that value as well. The memset/free is a good idea!

Overall the patch looks fine to me but I'd like others to review this as 
well.

Please resend with the suggested change if you agree with it,

Thanks a lot for tackling this issue!
Andreas
-- 
  Andreas Jaeger aj@{suse.com,opensuse.org} Twitter/Identica: jaegerandi
   SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
    GF: Jeff Hawn,Jennifer Guild,Felix Imendörffer,HRB16746 (AG Nürnberg)
     GPG fingerprint = 93A3 365E CE47 B889 DF7F  FED1 389A 563C C272 A126



More information about the Libc-alpha mailing list