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

Nico Weber thakis at chromium.org
Mon Apr 28 13:16:42 PDT 2014


On Mon, Apr 28, 2014 at 1:05 PM, Reid Kleckner <rnk at google.com> wrote:

> Even if we commit this workaround, can we report this as a bug to upstream
> Linux?
>

As mentioned above, I'm guessing Linux probably doesn't want to depend on C
standard headers, so they wouldn't see this as a bug in Linux.

You could say that it's a bug in glibc that they include headers that pull
in Linux's internal linux/stddef.h in glibc headers used by applications,
but based on previous interactions with glibc I would guess that their
reply would be "we do include this internal linux header, but we're careful
to define __need_NULL and re-include stddef.h to fix the issues caused by
that". (I'm happy to file a bug like this if you think it has a chance of
not being WontFixed immediately.)


>  The test case you have shows how this definition of NULL is broken on
> x86_64, even in C with gcc:
>

The Linux header does

#if c++
#define NULL 0
#else
#define NULL (void*)0
#endif

so that should be fine (?)


> $ 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/6ab651e9/attachment.html>


More information about the cfe-commits mailing list