use a common definition of NULL as 0L for C and C++
authorRich Felker <dalias@aerifal.cx>
Sat, 19 Jan 2013 01:35:26 +0000 (20:35 -0500)
committerRich Felker <dalias@aerifal.cx>
Sat, 19 Jan 2013 01:35:26 +0000 (20:35 -0500)
the historical mess of having different definitions for C and C++
comes from the historical C definition as (void *)0 and the fact that
(void *)0 can't be used in C++ because it does not convert to other
pointer types implicitly. however, using plain 0 in C++ exposed bugs
in C++ programs that call variadic functions with NULL as an argument
and (wrongly; this is UB) expect it to arrive as a null pointer. on
64-bit machines, the high bits end up containing junk. glibc dodges
the issue by using a GCC extension __null to define NULL; this is
observably non-conforming because a conforming application could
observe the definition of NULL via stringizing and see that it is
neither an integer constant expression with value zero nor such an
expression cast to void.

switching to 0L eliminates the issue and provides compatibility with
broken applications, since on all musl targets, long and pointers have
the same size, representation, and argument-passing convention. we
could maintain separate C and C++ definitions of NULL (i.e. just use
0L on C++ and use (void *)0 on C) but after careful analysis, it seems
extremely difficult for a C program to even determine whether NULL has
integer or pointer type, much less depend in subtle, unintentional
ways, on whether it does. C89 seems to have no way to make the
distinction. on C99, the fact that (int)(void *)0 is not an integer
constant expression, along with subtle VLA/sizeof semantics, can be
used to make the distinction, but many compilers are non-conforming
and give the wrong result to this test anyway. on C11, _Generic can
trivially make the distinction, but it seems unlikely that code
targetting C11 would be so backwards in caring which definition of
NULL an implementation uses.

as such, the simplest path of using the same definition for NULL in
both C and C++ was chosen. the #undef directive was also removed so
that the compiler can catch and give a warning or error on
redefinition if buggy programs have defined their own versions of
NULL prior to inclusion of standard headers.

include/locale.h
include/stddef.h
include/stdio.h
include/stdlib.h
include/string.h
include/time.h
include/unistd.h
include/wchar.h

index c6cc1ec..f6a29b2 100644 (file)
@@ -7,12 +7,7 @@ extern "C" {
 
 #include <features.h>
 
 
 #include <features.h>
 
-#undef NULL
-#ifdef __cplusplus
-#define NULL 0
-#else
-#define NULL ((void*)0)
-#endif
+#define NULL 0L
 
 #define LC_CTYPE    0
 #define LC_NUMERIC  1
 
 #define LC_CTYPE    0
 #define LC_NUMERIC  1
index a5bb9ec..9d52248 100644 (file)
@@ -1,12 +1,7 @@
 #ifndef _STDDEF_H
 #define _STDDEF_H
 
 #ifndef _STDDEF_H
 #define _STDDEF_H
 
-#undef NULL
-#ifdef __cplusplus
-#define NULL 0
-#else
-#define NULL ((void*)0)
-#endif
+#define NULL 0L
 
 #define __NEED_ptrdiff_t
 #define __NEED_size_t
 
 #define __NEED_ptrdiff_t
 #define __NEED_size_t
index 645e622..f648801 100644 (file)
@@ -20,12 +20,7 @@ extern "C" {
 
 #include <bits/alltypes.h>
 
 
 #include <bits/alltypes.h>
 
-#undef NULL
-#ifdef __cplusplus
-#define NULL 0
-#else
-#define NULL ((void*)0)
-#endif
+#define NULL 0L
 
 #undef EOF
 #define EOF (-1)
 
 #undef EOF
 #define EOF (-1)
index 2d1505b..671d188 100644 (file)
@@ -7,12 +7,7 @@ extern "C" {
 
 #include <features.h>
 
 
 #include <features.h>
 
-#undef NULL
-#ifdef __cplusplus
-#define NULL 0
-#else
-#define NULL ((void*)0)
-#endif
+#define NULL 0L
 
 #define __NEED_size_t
 #define __NEED_wchar_t
 
 #define __NEED_size_t
 #define __NEED_wchar_t
index c2f8eb5..d441233 100644 (file)
@@ -7,12 +7,7 @@ extern "C" {
 
 #include <features.h>
 
 
 #include <features.h>
 
-#undef NULL
-#ifdef __cplusplus
-#define NULL 0
-#else
-#define NULL ((void*)0)
-#endif
+#define NULL 0L
 
 #define __NEED_size_t
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
 
 #define __NEED_size_t
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
index fbc47db..4a681b4 100644 (file)
@@ -7,13 +7,7 @@ extern "C" {
 
 #include <features.h>
 
 
 #include <features.h>
 
-#undef NULL
-#ifdef __cplusplus
-#define NULL 0
-#else
-#define NULL ((void*)0)
-#endif
-
+#define NULL 0L
 
 #define __NEED_size_t
 #define __NEED_time_t
 
 #define __NEED_size_t
 #define __NEED_time_t
index 89015ad..07e49e9 100644 (file)
@@ -15,12 +15,7 @@ extern "C" {
 #define SEEK_CUR 1
 #define SEEK_END 2
 
 #define SEEK_CUR 1
 #define SEEK_END 2
 
-#undef NULL
-#ifdef __cplusplus
-#define NULL 0
-#else
-#define NULL ((void*)0)
-#endif
+#define NULL 0L
 
 #define __NEED_size_t
 #define __NEED_ssize_t
 
 #define __NEED_size_t
 #define __NEED_ssize_t
index 35706c7..9ceddd5 100644 (file)
@@ -26,12 +26,7 @@ extern "C" {
 
 #include <bits/wchar.h>
 
 
 #include <bits/wchar.h>
 
-#undef NULL
-#ifdef __cplusplus
-#define NULL 0
-#else
-#define NULL ((void*)0)
-#endif
+#define NULL 0L
 
 #undef WEOF
 #define WEOF (-1)
 
 #undef WEOF
 #define WEOF (-1)