Conversation

The malloc_object_size API is non-standard and is seriously flawed. It's undefined behavior to use more of the allocation than you requested. It's a real problem with both Clang and GCC due to the alloc_size attributes on the malloc API. It's broken to use more of the allocation.
1
3
It will trigger the -fsanitize=object-size (included in -fsanitize=undefined) and _FORTIFY_SOURCE checks. Using malloc_object_size is also not efficient. In modern malloc implementations, it usually requires reading out-of-line metadata since they avoid per-allocation metadata.
1
2
Extended malloc/realloc APIs providing the size as an output parameter is more efficient but it's still not ideal and runs into the same alloc_size issue. The best way to do it is really just having the collection types be aware of the malloc size classes to choose good sizes.
1
Replying to and
Makes sense. I deal in small allocations so much I forgot about large ones 😂 thanks for the info! (And yeah, I’ve been looking into tuning collection growth strategies specifically to avoid the overhead of asking for the size. It’s particularly steep for us for silly reasons)
1
2
Show replies