Browse Source

restore type of NULL to void * except when used in C++ programs

unfortunately this eliminates the ability of the compiler to diagnose
some dangerous/incorrect usage, but POSIX requires (as an extension to
the C language, i.e. CX shaded) that NULL have type void *. plain C
allows it to be defined as any null pointer constant.

the definition 0L is preserved for C++ rather than reverting to plain
0 to avoid dangerous behavior in non-conforming programs which use
NULL as a variadic sentinel. (it's impossible to use (void *)0 for C++
since C++ lacks the proper implicit pointer conversions, and other
popular alternatives like the GCC __null extension seem non-conforming
to the standard's requirements.)
Rich Felker 10 years ago
parent
commit
c8a9c22173
8 changed files with 33 additions and 0 deletions
  1. 4 0
      include/locale.h
  2. 4 0
      include/stddef.h
  3. 4 0
      include/stdio.h
  4. 4 0
      include/stdlib.h
  5. 4 0
      include/string.h
  6. 5 0
      include/time.h
  7. 4 0
      include/unistd.h
  8. 4 0
      include/wchar.h

+ 4 - 0
include/locale.h

@@ -7,7 +7,11 @@ extern "C" {
 
 #include <features.h>
 
+#ifdef __cplusplus
 #define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
 
 #define LC_CTYPE    0
 #define LC_NUMERIC  1

+ 4 - 0
include/stddef.h

@@ -1,7 +1,11 @@
 #ifndef _STDDEF_H
 #define _STDDEF_H
 
+#ifdef __cplusplus
 #define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
 
 #define __NEED_ptrdiff_t
 #define __NEED_size_t

+ 4 - 0
include/stdio.h

@@ -21,7 +21,11 @@ extern "C" {
 
 #include <bits/alltypes.h>
 
+#ifdef __cplusplus
 #define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
 
 #undef EOF
 #define EOF (-1)

+ 4 - 0
include/stdlib.h

@@ -7,7 +7,11 @@ extern "C" {
 
 #include <features.h>
 
+#ifdef __cplusplus
 #define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
 
 #define __NEED_size_t
 #define __NEED_wchar_t

+ 4 - 0
include/string.h

@@ -7,7 +7,11 @@ extern "C" {
 
 #include <features.h>
 
+#ifdef __cplusplus
 #define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
 
 #define __NEED_size_t
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \

+ 5 - 0
include/time.h

@@ -7,7 +7,12 @@ extern "C" {
 
 #include <features.h>
 
+#ifdef __cplusplus
 #define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
+
 
 #define __NEED_size_t
 #define __NEED_time_t

+ 4 - 0
include/unistd.h

@@ -15,7 +15,11 @@ extern "C" {
 #define SEEK_CUR 1
 #define SEEK_END 2
 
+#ifdef __cplusplus
 #define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
 
 #define __NEED_size_t
 #define __NEED_ssize_t

+ 4 - 0
include/wchar.h

@@ -33,7 +33,11 @@ extern "C" {
 #define WCHAR_MIN (-1-0x7fffffff+L'\0')
 #endif
 
+#ifdef __cplusplus
 #define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
 
 #undef WEOF
 #define WEOF 0xffffffffU