Make some tests ISO C 99 compliant.
authorBruno Haible <bruno@clisp.org>
Wed, 13 May 2009 23:39:07 +0000 (01:39 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 13 May 2009 23:39:46 +0000 (01:39 +0200)
12 files changed:
ChangeLog
modules/memchr-tests
modules/memchr2-tests
modules/memcmp-tests
modules/memmem-tests
modules/memrchr-tests
tests/test-memchr.c
tests/test-memchr2.c
tests/test-memcmp.c
tests/test-memmem.c
tests/test-memrchr.c
tests/zerosize-ptr.h [new file with mode: 0644]

index d23fd83545560378660ac1e15288e458bb294b9a..e37b92af42eba49c1631fc5d892b7d0a9f6445dd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,38 @@
+2009-05-13  Bruno Haible  <bruno@clisp.org>
+
+       Make some tests ISO C 99 compliant.
+       * tests/zerosize-ptr.h: New file.
+       * tests/test-memchr.c: Include zerosize-ptr.h.
+       (main): Use a zero-size object pointer instead of NULL.
+       * tests/test-memchr2.c: Include zerosize-ptr.h.
+       (main): Use a zero-size object pointer instead of NULL.
+       * tests/test-memcmp.c: Include zerosize-ptr.h.
+       (main): Use a zero-size object pointer instead of NULL.
+       * tests/test-memmem.c: Include zerosize-ptr.h.
+       (main): Use a zero-size object pointer instead of NULL.
+       * tests/test-memrchr.c: Include zerosize-ptr.h.
+       (main): Use a zero-size object pointer instead of NULL.
+       * modules/memchr-tests (Files): Add tests/zerosize-ptr.h,
+       m4/mmap-anon.m4.
+       (Depends-on): Add getpagesize.
+       (configure.ac): Invoke gl_FUNC_MMAP_ANON. Check for mprotect.
+       * modules/memchr2-tests (Files): Add tests/zerosize-ptr.h,
+       m4/mmap-anon.m4.
+       (Depends-on): Add getpagesize.
+       (configure.ac): Invoke gl_FUNC_MMAP_ANON. Check for mprotect.
+       * modules/memcmp-tests (Files): Add tests/zerosize-ptr.h,
+       m4/mmap-anon.m4.
+       (Depends-on): Add getpagesize.
+       (configure.ac): Invoke gl_FUNC_MMAP_ANON. Check for mprotect.
+       * modules/memmem-tests (Files): Add tests/zerosize-ptr.h,
+       m4/mmap-anon.m4.
+       (Depends-on): Add getpagesize.
+       (configure.ac): Invoke gl_FUNC_MMAP_ANON. Check for mprotect.
+       * modules/memrchr-tests (Files): Add tests/zerosize-ptr.h,
+       m4/mmap-anon.m4.
+       (Depends-on): Add getpagesize.
+       (configure.ac): Invoke gl_FUNC_MMAP_ANON. Check for mprotect.
+
 2009-05-12  Bruno Haible  <bruno@clisp.org>
 
        Tests for module 'alignof'.
index 305ec4ac348e8df6fe999a0fb85ef2555d47c4f1..5ef506a9f41626c7e93d342b22fe1c980b90e259 100644 (file)
@@ -1,9 +1,14 @@
 Files:
 tests/test-memchr.c
+tests/zerosize-ptr.h
+m4/mmap-anon.m4
 
 Depends-on:
+getpagesize
 
 configure.ac:
+gl_FUNC_MMAP_ANON
+AC_CHECK_FUNCS_ONCE([mprotect])
 
 Makefile.am:
 TESTS += test-memchr
index 7485fdcf73aa6a22363e705d176bec4718b85f60..6056e2b9733613f6d5071e52120226294addbf41 100644 (file)
@@ -1,9 +1,14 @@
 Files:
 tests/test-memchr2.c
+tests/zerosize-ptr.h
+m4/mmap-anon.m4
 
 Depends-on:
+getpagesize
 
 configure.ac:
+gl_FUNC_MMAP_ANON
+AC_CHECK_FUNCS_ONCE([mprotect])
 
 Makefile.am:
 TESTS += test-memchr2
index 14edc38e42af1644d41cbb8ec12fff6795b4abf7..df5b7eb3e833c67aaf3e0b09bf722451341b07c0 100644 (file)
@@ -1,9 +1,14 @@
 Files:
 tests/test-memcmp.c
+tests/zerosize-ptr.h
+m4/mmap-anon.m4
 
 Depends-on:
+getpagesize
 
 configure.ac:
+gl_FUNC_MMAP_ANON
+AC_CHECK_FUNCS_ONCE([mprotect])
 
 Makefile.am:
 TESTS += test-memcmp
index b4a0bdbe5d890e6120a2b9fb15f473ff9336d033..e98932e43b4e701d33b69ec6439329ef23dc297a 100644 (file)
@@ -1,9 +1,14 @@
 Files:
 tests/test-memmem.c
+tests/zerosize-ptr.h
+m4/mmap-anon.m4
 
 Depends-on:
+getpagesize
 
 configure.ac:
+gl_FUNC_MMAP_ANON
+AC_CHECK_FUNCS_ONCE([mprotect])
 AC_CHECK_DECLS_ONCE([alarm])
 
 Makefile.am:
index bad0515aba7ea01626c06ea9b232117ba331c64e..282c5702845b45a52391f7af00f7271f5acc9c31 100644 (file)
@@ -1,9 +1,14 @@
 Files:
 tests/test-memrchr.c
+tests/zerosize-ptr.h
+m4/mmap-anon.m4
 
 Depends-on:
+getpagesize
 
 configure.ac:
+gl_FUNC_MMAP_ANON
+AC_CHECK_FUNCS_ONCE([mprotect])
 
 Makefile.am:
 TESTS += test-memrchr
index ebf3cfcdc5189adeec9ad821caa490948487aa78..bf99a82edb71dc304cc6eded614fb4ca396b5d9c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Free Software Foundation
+ * Copyright (C) 2008-2009 Free Software Foundation
  * Written by Eric Blake and Bruno Haible
  *
  * This program is free software: you can redistribute it and/or modify
@@ -22,6 +22,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "zerosize-ptr.h"
+
 #define ASSERT(expr) \
   do                                                                        \
     {                                                                       \
@@ -41,8 +43,6 @@
 int
 main ()
 {
-  void *nil = NULL; /* Use to avoid gcc attribute((nonnull)) warnings.  */
-
   size_t n = 0x100000;
   char *input = malloc (n);
   ASSERT (input);
@@ -58,7 +58,7 @@ main ()
   ASSERT (MEMCHR (input, 'a', n) == input);
 
   ASSERT (MEMCHR (input, 'a', 0) == NULL);
-  ASSERT (MEMCHR (nil, 'a', 0) == NULL);
+  ASSERT (MEMCHR (zerosize_ptr (), 'a', 0) == NULL);
 
   ASSERT (MEMCHR (input, 'b', n) == input + 1);
   ASSERT (MEMCHR (input, 'c', n) == input + 2);
index c804a4158256595564332654398641f9eac346e2..c7ba6b97353026286cf93eea5ce2cfd3b2b3c32a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Free Software Foundation
+ * Copyright (C) 2008-2009 Free Software Foundation
  * Written by Eric Blake
  *
  * This program is free software: you can redistribute it and/or modify
@@ -23,6 +23,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "zerosize-ptr.h"
+
 #define ASSERT(expr) \
   do                                                                        \
     {                                                                       \
@@ -58,7 +60,7 @@ main ()
   ASSERT (MEMCHR2 (input, 'b', 'a', n) == input);
 
   ASSERT (MEMCHR2 (input, 'a', 'b', 0) == NULL);
-  ASSERT (MEMCHR2 (NULL, 'a', 'b', 0) == NULL);
+  ASSERT (MEMCHR2 (zerosize_ptr (), 'a', 'b', 0) == NULL);
 
   ASSERT (MEMCHR2 (input, 'b', 'd', n) == input + 1);
   ASSERT (MEMCHR2 (input + 2, 'b', 'd', n - 2) == input + 1026);
index 50342cb968c099f5643a850c760962025335d910..d108198f97d5eb0e35148006846700f036877364 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Free Software Foundation
+ * Copyright (C) 2008-2009 Free Software Foundation
  * Written by Simon Josefsson
  *
  * This program is free software: you can redistribute it and/or modify
@@ -22,6 +22,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "zerosize-ptr.h"
+
 #define ASSERT(expr) \
   do                                                                        \
     {                                                                       \
 int
 main (void)
 {
-  void *nil = NULL; /* Use to avoid gcc attribute((nonnull)) warnings.  */
-
   /* Test equal / not equal distinction.  */
-  ASSERT (memcmp (nil, nil, 0) == 0);
+  ASSERT (memcmp (zerosize_ptr (), zerosize_ptr (), 0) == 0);
   ASSERT (memcmp ("foo", "foobar", 2) == 0);
   ASSERT (memcmp ("foo", "foobar", 3) == 0);
   ASSERT (memcmp ("foo", "foobar", 4) != 0);
index e429ac096a452aadd7027032b3063e73dcb5b798..7f0f9257a3ee863ae472d43828178b3562793520 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004, 2007, 2008 Free Software Foundation
+ * Copyright (C) 2004, 2007-2009 Free Software Foundation
  * Written by Bruno Haible and Eric Blake
  *
  * This program is free software: you can redistribute it and/or modify
@@ -24,6 +24,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include "zerosize-ptr.h"
+
 #define ASSERT(expr) \
   do                                                                        \
     {                                                                       \
@@ -78,9 +80,9 @@ main (int argc, char *argv[])
     ASSERT (result == input + 11);
   }
 
-  /* Check that length 0 does not dereference NULL.  */
+  /* Check that length 0 does not dereference the pointer.  */
   {
-    const char *result = memmem (NULL, 0, "foo", 3);
+    const char *result = memmem (zerosize_ptr (), 0, "foo", 3);
     ASSERT (result == NULL);
   }
 
index f829a226ae7e6aa82cf7dc3e63cea149bb121d72..63568c156619c8512c262f9d54ccfaa2a4e3bd0b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Free Software Foundation
+ * Copyright (C) 2008-2009 Free Software Foundation
  * Written by Eric Blake and Bruno Haible
  *
  * This program is free software: you can redistribute it and/or modify
@@ -22,6 +22,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "zerosize-ptr.h"
+
 #define ASSERT(expr) \
   do                                                                        \
     {                                                                       \
@@ -41,8 +43,6 @@
 int
 main ()
 {
-  void *nil = NULL; /* Use to avoid gcc attribute((nonnull)) warnings.  */
-
   size_t n = 0x100000;
   char *input = malloc (n);
   ASSERT (input);
@@ -58,7 +58,7 @@ main ()
   ASSERT (MEMRCHR (input, 'a', n) == input + n - 1);
 
   ASSERT (MEMRCHR (input, 'a', 0) == NULL);
-  ASSERT (MEMRCHR (nil, 'a', 0) == NULL);
+  ASSERT (MEMRCHR (zerosize_ptr (), 'a', 0) == NULL);
 
   ASSERT (MEMRCHR (input, 'b', n) == input + n - 2);
   ASSERT (MEMRCHR (input, 'c', n) == input + n - 3);
diff --git a/tests/zerosize-ptr.h b/tests/zerosize-ptr.h
new file mode 100644 (file)
index 0000000..fa00aee
--- /dev/null
@@ -0,0 +1,62 @@
+/* Return a pointer to a zero-size object in memory.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* ISO C 99 does not allow memcmp(), memchr() etc. to be invoked with a NULL
+   argument.  Therefore this file produces a non-NULL pointer which cannot
+   be dereferenced, if possible.  */
+
+#include <stdlib.h>
+
+#if HAVE_MPROTECT
+# include <fcntl.h>
+# include <unistd.h>
+# include <sys/types.h>
+# include <sys/mman.h>
+/* Define MAP_FILE when it isn't otherwise.  */
+# ifndef MAP_FILE
+#  define MAP_FILE 0
+# endif
+#endif
+
+/* Return a pointer to a zero-size object in memory, if possible.
+   Return NULL otherwise.  */
+
+static void *
+zerosize_ptr (void)
+{
+/* Use mmap and mprotect when they exist.  Don't test HAVE_MMAP, because it is
+   not defined on HP-UX 11 (since it does not support MAP_FIXED).  */
+#if HAVE_MPROTECT
+# if HAVE_MAP_ANONYMOUS
+  const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
+  const int fd = -1;
+# else /* !HAVE_MAP_ANONYMOUS */
+  const int flags = MAP_FILE | MAP_PRIVATE;
+  int fd = open ("/dev/zero", O_RDONLY, 0666);
+  if (fd >= 0)
+# endif
+    {
+      int pagesize = getpagesize ();
+      char *two_pages =
+       (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
+                      flags, fd, 0);
+      if (two_pages != (char *)(-1)
+         && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
+       return two_pages + pagesize;
+    }
+#endif
+  return NULL;
+}