[PATCH 05/11] A pointer to a pointer is nonnull.

Peter Foley pefoley2@pefoley.com
Mon Mar 21 14:20:00 GMT 2016

On Sun, Mar 20, 2016 at 7:15 AM, Corinna Vinschen
<corinna-cygwin@cygwin.com> wrote:
> Eh, what?!?  How on earth can gcc assert memptr is always non-NULL?
> An application can call posix_memalign(NULL, 4096, 4096) just fine,
> can't it?  If so, *memptr = res crashes.

So, it looks like what's happening is that gcc special-cases
posix_memalign as a builtin function.

See https://github.com/gcc-mirror/gcc/blob/master/gcc/builtins.def#L831

This causes the below warning to be outputted for my testcase:

a.cc:9:25: warning: null argument where non-null required (argument 1)
a.cc: In function ‘int posix_memalign(void**, long unsigned int, long
unsigned int)’:
a.cc:3:3: warning: nonnull argument ‘memptr’ compared to NULL
   if (memptr)


extern "C" posix_memalign(void **memptr, unsigned long, unsigned long) {
  void *a = 0;
  if (memptr)
    *memptr = a;
  return 0;

int main() {

(Note that passing -fno-builtin causes the warning to go away, as
posix_memalign is no-longer special-cased)

In addition, both newlib and glibc appear to assume the memptr arg is nonnull.

Hope that makes this more understandable.



More information about the Cygwin-patches mailing list