[PATCH v2] malloc: Improve calloc [BZ #34582]

Yury Khrustalev yury.khrustalev@arm.com
Wed Sep 16 09:19:37 GMT 2026


On Tue, Sep 15, 2026 at 04:52:18PM +0000, Wilco Dijkstra wrote:
> Hi Yury,
> 
> > Does it make sense to add reproducers from BZ 34582 as testcases to the
> > tst-calloc test? With this patch some assertions in the reproducers still
> > fail (though it seems like the reproducers rely on UB).
> 
> I'm not sure there is any point since corruption of the malloc state will result
> in crashes - just not the original crash.
> 
> > I think these two macros should depend on each other: I'd suggest to
> > define 'memsize(p)' as 'chunk2usable(chunksize(p))'.
> 
> Done.
> 
> > Also, the name of the 'chunk2usable' macro should probably be something
> > like 'chunk_size_to_usable_size' since we have macros that begin with 'chunk2'
> > and work with pointers rather than sizes.
> 
> Done.
> 
> > Here variable 'nb' is undeclared if USE_TCACHE is false (use of variable
> > outside #if USE_TCACHE ... #endif).
> 
> That's why #if is a bad idea in code... So USE_TCACHE is always true for this
> function, or it would give an error.
> 
> > Also, I think __libc_calloc2's argument name should be 'usable[_size]',
> > same as __libc_calloc_small.
> 
> I've changed it to usable_size as that is clearer indeed.
> 

Thanks for improving the change!

> 
> 
> v2: Use chunksize2usable and usable_size
> 
> Add __libc_calloc_small for small calloc sizes to avoid the complex logic of 
> __libc_calloc2 which is unnecessary overhead for small blocks.  Cleanup
> __libc_calloc2 and only clear the size of the requested chunk (since we
> typically get the exact size after splitting off any tail).  This fixes
> BZ #34582.

LGTM.

Reviewed-by: Yury Khrustalev <yury.khrustalev@arm.com>

Kind regards,
Yury



More information about the Libc-alpha mailing list