unistd: warn on use of environ without module
authorEric Blake <ebb9@byu.net>
Thu, 31 Dec 2009 20:28:59 +0000 (13:28 -0700)
committerEric Blake <ebb9@byu.net>
Mon, 11 Jan 2010 13:51:55 +0000 (06:51 -0700)
_GL_WARN_ON_USE only works for functions; so we wrap access to
environ through a function when CFLAGS has -DGNULIB_POSIXCHECK=1.

* modules/unistd (Depends-on): Add warn-on-use.
(Makefile.am): Provide new substitutions.
* m4/unistd_h.m4 (gl_UNISTD_H): Check for inline and environ.
* lib/unistd.in.h (environ): Wrap with a warning helper function.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
lib/unistd.in.h
m4/unistd_h.m4
modules/unistd

index c898b9a..5fa0e6c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2010-01-11  Eric Blake  <ebb9@byu.net>
 
+       unistd: warn on use of environ without module
+       * modules/unistd (Depends-on): Add warn-on-use.
+       (Makefile.am): Provide new substitutions.
+       * m4/unistd_h.m4 (gl_UNISTD_H): Check for inline and environ.
+       * lib/unistd.in.h (environ): Wrap with a warning helper function.
+
        stdio: warn on suspicious uses
        * modules/stdio (Depends-on): Add warn-on-use.
        (Makefile.am): Provide new substitutions.
index a353b0c..018dd5f 100644 (file)
 
 /* The definition of _GL_ARG_NONNULL is copied here.  */
 
+/* The definition of _GL_WARN_ON_USE is copied here.  */
+
 
 /* OS/2 EMX lacks these macros.  */
 #ifndef STDIN_FILENO
@@ -250,11 +252,17 @@ extern char **environ;
 #  endif
 # endif
 #elif defined GNULIB_POSIXCHECK
-# undef environ
-# define environ \
-    (GL_LINK_WARNING ("environ is unportable - " \
-                      "use gnulib module environ for portability"), \
-     environ)
+# if HAVE_RAW_DECL_ENVIRON
+static inline char ***
+rpl_environ (void)
+{
+  return &environ;
+}
+_GL_WARN_ON_USE (rpl_environ, "environ is unportable - "
+                 "use gnulib module environ for portability");
+#  undef environ
+#  define environ (*rpl_environ ())
+# endif
 #endif
 
 
index f3074ea..aacec2b 100644 (file)
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 37
+# unistd_h.m4 serial 38
 dnl Copyright (C) 2006-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -11,6 +11,7 @@ AC_DEFUN([gl_UNISTD_H],
   dnl Use AC_REQUIRE here, so that the default behavior below is expanded
   dnl once only, before all statements that occur in other macros.
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_REQUIRE([AC_C_INLINE])
 
   gl_CHECK_NEXT_HEADERS([unistd.h])
 
@@ -21,6 +22,15 @@ AC_DEFUN([gl_UNISTD_H],
     HAVE_UNISTD_H=0
   fi
   AC_SUBST([HAVE_UNISTD_H])
+
+  dnl Check for declarations of anything we want to poison if the
+  dnl corresponding gnulib module is not in use.
+  gl_WARN_ON_USE_PREPARE([[#include <unistd.h>
+/* Some systems declare environ in the wrong header.  */
+#ifndef __GLIBC__
+# include <stdlib.h>
+#endif
+    ]], [environ])
 ])
 
 AC_DEFUN([gl_UNISTD_MODULE_INDICATOR],
index f557837..055b989 100644 (file)
@@ -6,10 +6,11 @@ m4/unistd_h.m4
 lib/unistd.in.h
 
 Depends-on:
+arg-nonnull
 include_next
 link-warning
-arg-nonnull
 stddef
+warn-on-use
 
 configure.ac:
 gl_UNISTD_H
@@ -19,7 +20,7 @@ BUILT_SOURCES += unistd.h
 
 # We need the following in order to create an empty placeholder for
 # <unistd.h> when the system doesn't have one.
-unistd.h: unistd.in.h $(LINK_WARNING_H) $(ARG_NONNULL_H)
+unistd.h: unistd.in.h $(LINK_WARNING_H) $(WARN_ON_USE_H) $(ARG_NONNULL_H)
        $(AM_V_GEN)rm -f $@-t $@ && \
        { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
          sed -e 's|@''HAVE_UNISTD_H''@|$(HAVE_UNISTD_H)|g' \
@@ -121,6 +122,7 @@ unistd.h: unistd.in.h $(LINK_WARNING_H) $(ARG_NONNULL_H)
              -e 's|@''UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS''@|$(UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS)|g' \
              -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
              -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
+             -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
              < $(srcdir)/unistd.in.h; \
        } > $@-t && \
        mv $@-t $@