Ticket #105 (NULL checks)
Andy Dougherty
doughera at lafayette.edu
Wed Dec 31 20:26:07 UTC 2008
On Wed, 31 Dec 2008, chromatic wrote:
> On Wednesday 31 December 2008 08:03:31 Mark Glines wrote:
>
> > Ok, I've gone and read through RT #49316 and RT #50684. It sounds an
> > awful lot like attribute_nonnull is an optimization, not a constraint as
> > I had originally thought.
>
> If we use -Wnonnull (implied by -Wall), we'll get compile warnings when we
> pass potentially null parameters to functions which require non-null
> parameters. There may be optimization possibilities there, but I believe the
> primary purpose was to get these warnings.
Except gcc can't always detect if the pointers are "potentially null".
The problem arises when pointers that are not supposed to be null end up
being null due to a bug elsewhere in the program. In those cases, the
attribute_nonnull actually gets in the way by allowing gcc to optimize
away checks for such buggy pointers.
This is not a hypothetical what-if situation. I have lost many hours to
just this situation. See my long example in ticket RT #50684. To save
everyone from looking it up, I will extract the relevant example here:
=begin quote
Consider this function defined on line 763 in src/headers.c:
static int
sweep_cb_buf(PARROT_INTERP, ARGMOD(Small_Object_Pool *pool),
SHIM(int flag), SHIM(void *arg))
Yesterday, I was trying to debug a problem where pool was NULL. I tried
adding various checks to the function, unaware that 712 lines above,
on line 51, pool was listed as nonnull. Gcc kept optimizing away my
checks, so parrot kept crashing and I was deceived into thinking the
problem wasn't a null pool. I finally remembered that I had done this
dance once before and that I first had to remove the nonnull decoration
before I could get back to debugging.
So what did __attribute__nonnull actually gain me here? It is technically
correct. pool should not be null here. However, it didn't seem to help
in any way. Without it, parrot crashed. With it, parrot crashed in the
same place, but I couldn't put in any if (!pool) checks without hunting
down and eliminating the nonnull attribute. And since it is defined some
distance away, it might not always be obvious whether it's there or not.
=end quote
--
Andy Dougherty doughera at lafayette.edu
More information about the parrot-dev
mailing list