[patch] Let stddef.h redefine NULL if __need_NULL is set

Reid Kleckner rnk at google.com
Mon Apr 28 13:05:34 PDT 2014


Even if we commit this workaround, can we report this as a bug to upstream
Linux?  The test case you have shows how this definition of NULL is broken
on x86_64, even in C with gcc:

$ cat t.c
#define NULL 0
int printf(const char *, ...);
int main() {
  printf("%d %d %d %d %d %d %p\n", 1, 2, 3, 4, 5, 6, 0xdeadbeefdeadbeefULL);
  printf("%d %d %d %d %d %d %p\n", 1, 2, 3, 4, 5, 6, NULL);
}

$ gcc -w t.c -o t && ./t
1 2 3 4 5 6 0xdeadbeefdeadbeef
1 2 3 4 5 6 0xdeadbeef00000000



On Sun, Apr 27, 2014 at 2:50 PM, Nico Weber <thakis at chromium.org> wrote:

> Hi,
>
> linux/stddef.h does something like
>
> #undef NULL
> #define NULL 0
>
> when included for C++ translation units, and by transitivity it ends
> up being pulled in often on linux (see llvm bug for examples).
> -Wsentinel doesn't allow 0 as sentinel, leading to warnings such as
>
> small.cc:6:18: error: missing sentinel in function call
> [-Werror,-Wsentinel]
>   foo("bar", NULL);
>                  ^
>                  , NULL
>
>
> To "fix" this, files defines __need_NULL and re-include stddef.h,
> assuming that stddef.h redefines NULL if __need_NULL is defined
> (similar to the __need_wint_t code that's already there). The attached
> patch teaches clang's stddef.h about __need_NULL. (As far as I
> understand, this shouldn't interfere with modules – linux/stddef.h
> interferes with that, but it does so independently of this patch.)
>
> Fixes PR12997. Patch mostly by Lei Zhang <thestig at chromium.org>, test
> and tweaks by me.
>
> Nico
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140428/a8f0e036/attachment.html>


More information about the cfe-commits mailing list