[PATCH] malloc: Remove redundant NULL check
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Wed Jul 30 13:26:36 GMT 2025
On 30/07/25 06:57, Wilco Dijkstra wrote:
> Hi Cupertino,
>
>> I wonder if in glibc there is an assert macro we could use to better
>> protect this code from programmer mistakes.
>> Can we add asserts in malloc code ?
>
> assert() is used but it's broken in that those asserts are not removed in
> release builds. So we need to fix that before we add asserts in hot paths.
There is an additional issue where assert() itself calls malloc, so
assert itself might not work reliable if we hit a code path where
malloc is in an inconsistent state.
I sent a patch to remove malloc usage from assert some time ago [1].
[1] https://patchwork.sourceware.org/project/glibc/patch/20250331173100.3250183-1-adhemerval.zanella@linaro.org/
>
>> When revisiting this code, all I can think of is, let me set an assert
>> to protect 'e' from ever being NULL.
>>
>> The only reason this does not seg faults is that we only call this
>> function if the bin has at least one element. Well, this check is never
>> verified within the function context, making it very fragile from a
>> software engineering point of view.
>
> Well if you deference 'e' unconditionally immediately after assigning it
> then that is evidence that it cannot ever be NULL!
>
> Cheers,
> Wilco
More information about the Libc-alpha
mailing list