Re: [PATCH][RESEND] do not redefine userspace's NULL #define

From: Lubos Lunak
Date: Fri Apr 13 2012 - 19:18:51 EST


On Saturday 14 of April 2012, Linus Torvalds wrote:
> On Fri, Apr 13, 2012 at 3:01 PM, Peter Seebach
> > Maybe the thing to do would be to ensure that NULL goes to __null,
> > then define that to be ((void *) 0) if the compiler doesn't provide
> > it? ÂThe magic behavior of __null seems like it'd be preferable
> > where it is available.
>
> So if gcc guarantees that __null has the correct semantics, I could
> imagine replacing the kernel ((void *)0) with __null.

__null apparently exists only with g++, C does not have the stronger type
safety that prevents ((void*)0) from being usable in C++, so __null is not
needed there. So this is really only about userspace C++ code.

So how about this one? It does not change anything in the C mode and keeps it
the way you want, and it fixes the C++ mode.

8<-----
headers: do not redefine userspace's NULL #define when compiling C++ code

GCC's NULL is actually __null when compiling C++, which allows detecting some
questionable NULL usage and warn about it, so do not redefine NULL to 0.
Use the 0 definition only if NULL does not exists, to keep kernel headers
self-contained (and GCC's stddef.h will possibly redefine it later to __null).
In C mode ((void*)0) works fine.

Signed-off-by: LuboÅ LuÅÃk <l.lunak@xxxxxxx>
---
include/linux/stddef.h | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index 6a40c76..13dfc92 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -3,10 +3,12 @@

#include <linux/compiler.h>

-#undef NULL
#if defined(__cplusplus)
+#ifndef NULL
#define NULL 0
+#endif
#else
+#undef NULL
#define NULL ((void *)0)
#endif

--
1.7.7


--
Lubos Lunak
l.lunak@xxxxxxx
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/