https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93156

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to jim from comment #3)
> Hi Andrew, thank you for the prompt investigation.
> I'm probably just being dense, but how can the compiler ever generate code
> for that null_ptr function that results in -1?

It is not that null_ptr can result in -1 directly but rather if it is inlined
and then later on passed to a non-null function, it will change it to be the
non-null case.

> 
> Your comment shows you know this already, but for others, here's a comment
> from the sources in gnulib:
> 
> /* If you want to know why this always returns NULL, read
>    https://en.wikipedia.org/wiki/Quadratic_residue#Prime_power_modulus .  */
> 

I am saying that is not the best way of getting the compiler not to notice it
as being a choice between a non-null and a null pointer.  Using a volatile
variable makes the compiler NOT do any optimization and will not see the
non-null pointer choice.

> Note that at least some versions of canonicalize_file_name *can* take a NULL
> argument, so the use of the nonnull attribute in that declaration is wrong.

The question comes, where is the nonnull attribute coming from?  But I don't
think it matters here.
Using:
static void *
null_ptr (void)
{
  typedef void *voidptr;
  volatile voidptr n = 0;
  return n;
}
Will allow all compilers not to optimize anything to be known about null_ptr()
function even with inlining being involved.

Reply via email to