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.   
---
On Wed, 7 Jan 2026, Andrew Pinski wrote:

> > 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.

 Thank you for your review.  This is what I committed.

  Maciej

Changes from v1:

- Handle the NULL pointer case with an early return.
---
 libiberty/objalloc.c |    4 ++++
 1 file changed, 4 insertions(+)

binutils-libiberty-objalloc-free-null.diff
Index: binutils-gdb/libiberty/objalloc.c
===================================================================
--- binutils-gdb.orig/libiberty/objalloc.c
+++ binutils-gdb/libiberty/objalloc.c
@@ -178,6 +178,10 @@ objalloc_free (struct objalloc *o)
 {
   struct objalloc_chunk *l;
 
+  /* Handle a nullptr as being a no-op. */
+  if (o == NULL)
+    return;
+
   l = (struct objalloc_chunk *) o->chunks;
   while (l != NULL)
     {

Reply via email to