Discussion:
coreutils snapshot vs gcc7-to-be: new test-calloc-gnu.c and test-canonicalize.c warnings
(too old to reply)
Jim Meyering
2017-03-04 00:30:49 UTC
Permalink
Raw Message
Building the latest coreutils snapshot with gcc just built from its
own latest git, I hit this:

test-calloc-gnu.c: In function 'main':
test-calloc-gnu.c:32:5: warning: product '2305843009213693952 * 8'
of arguments 1 and 2 exceeds 'SIZE_MAX' [-Walloc-size-larger-than=]
p = calloc ((size_t) -1 / 8 + 1, 8);
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../lib/stdlib.h:36:0,
from test-calloc-gnu.c:19:
/usr/include/stdlib.h:467:14: note: in a call to allocation function
'calloc' declared here
extern void *calloc (size_t __nmemb, size_t __size)
^~~~~~
and this:

test-canonicalize.c: In function 'main':
test-canonicalize.c:78:13: error: argument 1 null where non-null
expected [-Werror=nonnull]
result1 = canonicalize_file_name (null_ptr ());
~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../lib/stdlib.h:36:0,
from ../lib/canonicalize.h:20,
from test-canonicalize.c:21:
/usr/include/stdlib.h:723:14: note: in a call to function
'canonicalize_file_name' declared here
extern char *canonicalize_file_name (const char *__name)
^~~~~~~~~~~~~~~~~~~~~~
Bruno Haible
2017-03-04 10:33:15 UTC
Permalink
Raw Message
Post by Jim Meyering
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 <***@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:
Jim Meyering
2017-03-04 22:41:08 UTC
Permalink
Raw Message
Post by Bruno Haible
Post by Jim Meyering
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.
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.
Confirmed. Thank you, Bruno.
Jim Meyering
2017-03-04 22:54:11 UTC
Permalink
Raw Message
Post by Jim Meyering
Building the latest coreutils snapshot with gcc just built from its
test-calloc-gnu.c:32:5: warning: product '2305843009213693952 * 8'
of arguments 1 and 2 exceeds 'SIZE_MAX' [-Walloc-size-larger-than=]
p = calloc ((size_t) -1 / 8 + 1, 8);
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../lib/stdlib.h:36:0,
/usr/include/stdlib.h:467:14: note: in a call to allocation function
'calloc' declared here
extern void *calloc (size_t __nmemb, size_t __size)
^~~~~~
I've just fixed that with the attached and just-pushed gnulib patch:
Bruno Haible
2017-03-05 01:21:52 UTC
Permalink
Raw Message
Hi Jim,
You "fixed" it by disabling a valuable test! I propose to restore the test
and instead only disable GCC's ability to produce a warning. Like this:


2017-03-04 Bruno Haible <***@clisp.org>

test-calloc-gnu: Reenable test also for GCC 7.
* tests/test-calloc-gnu.c (eight): New function.
(main): Don't skip test; use eight() instead.

diff --git a/tests/test-calloc-gnu.c b/tests/test-calloc-gnu.c
index 3f964c3..7f5a1e1 100644
--- a/tests/test-calloc-gnu.c
+++ b/tests/test-calloc-gnu.c
@@ -18,6 +18,20 @@

#include <stdlib.h>

+/* Return 8.
+ Usual compilers are not able to infer something about the return value. */
+static unsigned int
+eight (void)
+{
+ unsigned int x = rand ();
+ unsigned int y = x * x * x * x;
+ x++; y |= x * x * x * x;
+ x++; y |= x * x * x * x;
+ x++; y |= x * x * x * x;
+ y = y >> 1;
+ return y & -y;
+}
+
int
main ()
{
@@ -27,19 +41,15 @@ main ()
return 1;
free (p);

-#if __GNUC__ < 7
- /* GCC7's -Werror=alloc-size-larger-than= would cause the following error
- to be detected at compile time, so skip the test for GCC7 and newer. */
-
/* Check that calloc fails when requested to allocate a block of memory
- larger than SIZE_MAX bytes. */
- p = calloc ((size_t) -1 / 8 + 1, 8);
+ larger than SIZE_MAX bytes.
+ We use eight (), not 8, to avoid a compiler warning from GCC 7. */
+ p = calloc ((size_t) -1 / 8 + 1, eight ());
if (p != NULL)
{
free (p);
return 1;
}
-#endif

return 0;
}
Jim Meyering
2017-03-05 01:37:23 UTC
Permalink
Raw Message
Post by Bruno Haible
Hi Jim,
You "fixed" it by disabling a valuable test! I propose to restore the test
test-calloc-gnu: Reenable test also for GCC 7.
* tests/test-calloc-gnu.c (eight): New function.
(main): Don't skip test; use eight() instead.
Hi Bruno,
Thanks for your vigilance. I do indeed prefer your change.

Loading...