[PATCH] libiberty: Make `objalloc_free' `free'-like WRT null pointer
Andrew Pinski
andrew.pinski@oss.qualcomm.com
Wed Jan 7 22:18:22 GMT 2026
On Wed, Jan 7, 2026 at 1:31 AM Maciej W. Rozycki <macro@redhat.com> wrote:
>
> Inspired by a suggestion from Jan Beulich to make one of `objalloc_free'
> callers `free'-like with respect to null pointer argument handling make
> the function return with no action taken rather than crashing when such
> a pointer is passed. This is to make the API consistent with ISO C and
> to relieve all the callers from having to check for a null pointer.
>
> libiberty/
> * objalloc.c (objalloc_free): Don't use the pointer passed if
> null.
> ---
> Hi,
>
> This has been raised in a binutils patch review[1]. OK to apply, and
> then right away, since libiberty is an auxiliary library and technically
> not a part of the compiler suite, plus the change should be trivially
> safe?
>
> References:
>
> [1] "BFD: Prevent a crash on freeing a BFD's uninitialized section hash",
> <https://inbox.sourceware.org/binutils/23685606-02a1-4416-978a-3c9907c5a171@suse.com/>.
>
> Maciej
> ---
> libiberty/objalloc.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> binutils-libiberty-objalloc-free-null.diff
> Index: binutils-gdb/libiberty/objalloc.c
> ===================================================================
> --- binutils-gdb.orig/libiberty/objalloc.c
> +++ binutils-gdb/libiberty/objalloc.c
> @@ -178,7 +178,7 @@ objalloc_free (struct objalloc *o)
> {
> struct objalloc_chunk *l;
>
> - l = (struct objalloc_chunk *) o->chunks;
> + l = o != NULL ? (struct objalloc_chunk *) o->chunks : NULL;
I think the following would be cleaner and easier to understand:
```
/* Handle a nullptr as being a no-op. */
if (o == NULL)
return;
l = (struct objalloc_chunk *) o->chunks;
```
Ok with that change.
Thanks,
Andrew
> while (l != NULL)
> {
> struct objalloc_chunk *next;
>
More information about the Binutils
mailing list