Commit ff7c8b21 (2021) to pacify -Wsuggest-attribute=pure with GCC 10.3
induces the code to be removed with clang 21,22 with -fno-inline.
I don't see the same code elimination on GCC 15,
but the the incorrect warning is still there.
(Note xalloc_die() is marked _Noreturn so GCC is wrong here).

You can see the difference using split(1).
Without -fno-inline it behaves as expected:

  $ src/split --number=r/9223372036854775808
  split: memory exhausted

and with the -fno-inline:

  $ src/split --number=r/9223372036854775807
  Segmentation fault         (core dumped)

* lib/xmalloc.c (check_nonnull): Remove the "pure" attribute,
and suppress the incorrect GCC warning with pragmas.
---
 lib/xmalloc.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lib/xmalloc.c b/lib/xmalloc.c
index b67cd4c223..b889004381 100644
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -27,13 +27,24 @@
 #include <stdint.h>
 #include <string.h>
 
-static void * _GL_ATTRIBUTE_PURE
+/* Suppress GCC's -Wsuggest-attribute=pure
+   incorrectly generated by GCC versions up to at least GCC 15.1.
+   Note with attribute pure, `clang -fno-inline` would eliminate
+   calls to check_nonnull().  */
+#if _GL_GNUC_PREREQ (4, 7)
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
+#endif
+static void *
 check_nonnull (void *p)
 {
   if (!p)
     xalloc_die ();
   return p;
 }
+#if _GL_GNUC_PREREQ (4, 7)
+# pragma GCC diagnostic pop
+#endif
 
 /* Allocate S bytes of memory dynamically, with error checking.  */
 
-- 
2.52.0


Reply via email to