Jim Meyering wrote: > test-canonicalize.c:78:13: error: argument 1 null where non-null > expected [-Werror=nonnull] > result1 = canonicalize_file_name (null_ptr ()); > ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This should fix it. 2017-03-04 Bruno Haible <br...@clisp.org> tests: Avoid compiler warning about uses of null_ptr. * tests/null-ptr.h: New file. * tests/test-canonicalize.c: Include null-ptr.h. (null_ptr): Remove function. * tests/test-canonicalize-lgpl.c: Likewise. * tests/test-memmem.c: Likewise. * tests/test-ptsname_r.c: Likewise. * modules/canonicalize-tests (Files): Add tests/null-ptr.h. * modules/canonicalize-lgpl-tests: Likewise. * modules/memmem-tests: Likewise. * modules/ptsname_r-tests: Likewise. Reported by Jim Meyering. diff --git a/tests/null-ptr.h b/tests/null-ptr.h new file mode 100644 index 0000000..6743645 --- /dev/null +++ b/tests/null-ptr.h @@ -0,0 +1,33 @@ +/* Return a NULL pointer, without letting the compiler know it. + Copyright (C) 2017 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/>. */ + +#include <stdlib.h> + +/* Return NULL. + Usual compilers are not able to infer something about the return value. */ +static void * +null_ptr (void) +{ + unsigned int x = rand (); + unsigned int y = x * x; + if (y & 2) + return (void *) -1; + else + return (void *) 0; +} + +/* If you want to know why this always returns NULL, read + https://en.wikipedia.org/wiki/Quadratic_residue#Prime_power_modulus . */ diff --git a/tests/test-canonicalize-lgpl.c b/tests/test-canonicalize-lgpl.c index af20a0a..c449f98 100644 --- a/tests/test-canonicalize-lgpl.c +++ b/tests/test-canonicalize-lgpl.c @@ -33,16 +33,12 @@ SIGNATURE_CHECK (canonicalize_file_name, char *, (const char *)); #include "same-inode.h" #include "ignore-value.h" + +#include "null-ptr.h" #include "macros.h" #define BASE "t-can-lgpl.tmp" -static void * -null_ptr (void) -{ - return NULL; -} - int main (void) { diff --git a/tests/test-canonicalize.c b/tests/test-canonicalize.c index 0ba89fe..37a1126 100644 --- a/tests/test-canonicalize.c +++ b/tests/test-canonicalize.c @@ -30,16 +30,12 @@ #include "same-inode.h" #include "ignore-value.h" + +#include "null-ptr.h" #include "macros.h" #define BASE "t-can.tmp" -static void * -null_ptr (void) -{ - return NULL; -} - int main (void) { diff --git a/tests/test-memmem.c b/tests/test-memmem.c index bbca811..b2c23e6 100644 --- a/tests/test-memmem.c +++ b/tests/test-memmem.c @@ -26,15 +26,10 @@ SIGNATURE_CHECK (memmem, void *, (void const *, size_t, void const *, size_t)); #include <stdlib.h> #include <unistd.h> +#include "null-ptr.h" #include "zerosize-ptr.h" #include "macros.h" -static void * -null_ptr (void) -{ - return NULL; -} - int main (int argc, char *argv[]) { diff --git a/tests/test-ptsname_r.c b/tests/test-ptsname_r.c index ed75ce7..2faf4c7 100644 --- a/tests/test-ptsname_r.c +++ b/tests/test-ptsname_r.c @@ -31,6 +31,7 @@ SIGNATURE_CHECK (ptsname_r, int, (int, char *, size_t)); #include "same-inode.h" +#include "null-ptr.h" #include "macros.h" /* Compare two slave names. @@ -53,12 +54,6 @@ same_slave (const char *slave_name1, const char *slave_name2) && SAME_INODE (statbuf1, statbuf2))); } -static char * -null_ptr (void) -{ - return NULL; -} - static void test_errors (int fd, const char *slave) { diff --git a/modules/canonicalize-lgpl-tests b/modules/canonicalize-lgpl-tests index fd49d69..22b6b7f 100644 --- a/modules/canonicalize-lgpl-tests +++ b/modules/canonicalize-lgpl-tests @@ -1,6 +1,7 @@ Files: tests/test-canonicalize-lgpl.c tests/signature.h +tests/null-ptr.h tests/macros.h Depends-on: diff --git a/modules/canonicalize-tests b/modules/canonicalize-tests index 6019349..e043a8d 100644 --- a/modules/canonicalize-tests +++ b/modules/canonicalize-tests @@ -1,5 +1,6 @@ Files: tests/test-canonicalize.c +tests/null-ptr.h tests/macros.h Depends-on: diff --git a/modules/memmem-tests b/modules/memmem-tests index 4738d5c..28c0091 100644 --- a/modules/memmem-tests +++ b/modules/memmem-tests @@ -1,7 +1,8 @@ Files: tests/test-memmem.c -tests/zerosize-ptr.h tests/signature.h +tests/null-ptr.h +tests/zerosize-ptr.h tests/macros.h m4/mmap-anon.m4 diff --git a/modules/ptsname_r-tests b/modules/ptsname_r-tests index 50b3f81..8f7928e 100644 --- a/modules/ptsname_r-tests +++ b/modules/ptsname_r-tests @@ -1,6 +1,7 @@ Files: tests/test-ptsname_r.c tests/signature.h +tests/null-ptr.h tests/macros.h Depends-on: