GNU bug report logs - #77835
[PATCH] Pacify GCC 15 -Wunterminated-string-initialization warnings

Previous Next

Package: emacs;

Reported by: Collin Funk <collin.funk1 <at> gmail.com>

Date: Wed, 16 Apr 2025 05:25:02 UTC

Severity: normal

Tags: patch

Done: Paul Eggert <eggert <at> cs.ucla.edu>

To reply to this bug, email your comments to 77835 AT debbugs.gnu.org.
There is no need to reopen the bug first.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#77835; Package emacs. (Wed, 16 Apr 2025 05:25:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Collin Funk <collin.funk1 <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 16 Apr 2025 05:25:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Collin Funk <collin.funk1 <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Pacify GCC 15 -Wunterminated-string-initialization warnings
Date: Tue, 15 Apr 2025 22:24:16 -0700
[Message part 1 (text/plain, inline)]
GCC 15.0 enables -Wunterminated-string-initialization when using
'-Wall -Wextra', which gets used when I run './configure'. This patch
silences the warnings:

fns.c: In function ‘hexbuf_digest’:
fns.c:6019:40: warning: initializer-string for array of ‘char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (17 chars into 16 available) [-Wunterminated-string-initialization]
 6019 |       static char const hexdigit[16] = "0123456789abcdef";
      |                                        ^~~~~~~~~~~~~~~~~~

Collin

[0001-Pacify-GCC-15-Wunterminated-string-initialization-wa.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#77835; Package emacs. (Wed, 16 Apr 2025 09:41:04 GMT) Full text and rfc822 format available.

Message #8 received at 77835 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Collin Funk <collin.funk1 <at> gmail.com>,
 Paul Eggert <eggert <at> cs.ucla.edu>
Cc: 77835 <at> debbugs.gnu.org
Subject: Re: bug#77835: [PATCH] Pacify GCC 15
 -Wunterminated-string-initialization warnings
Date: Wed, 16 Apr 2025 12:40:40 +0300
> From: Collin Funk <collin.funk1 <at> gmail.com>
> Date: Tue, 15 Apr 2025 22:24:16 -0700
> 
> GCC 15.0 enables -Wunterminated-string-initialization when using
> '-Wall -Wextra', which gets used when I run './configure'. This patch
> silences the warnings:
> 
> fns.c: In function ‘hexbuf_digest’:
> fns.c:6019:40: warning: initializer-string for array of ‘char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (17 chars into 16 available) [-Wunterminated-string-initialization]
>  6019 |       static char const hexdigit[16] = "0123456789abcdef";
>       |                                        ^~~~~~~~~~~~~~~~~~

Thanks.

> >From 1df4a867e9440885a5de846eb6fcd17e327fc328 Mon Sep 17 00:00:00 2001
> From: Collin Funk <collin.funk1 <at> gmail.com>
> Date: Tue, 15 Apr 2025 22:14:53 -0700
> Subject: [PATCH] Pacify GCC 15 -Wunterminated-string-initialization warnings
> 
> * src/fns.c: Include <attribute.h>.
> (hexbuf_digest): Mark a variable with ATTRIBUTE_NONSTRING.
> * src/json.c: Include <attribute.h>.
> (json_out_string): Mark a variable with ATTRIBUTE_NONSTRING.
> ---
>  src/fns.c  | 3 ++-
>  src/json.c | 3 ++-
>  2 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/src/fns.c b/src/fns.c
> index 3f109a81836..94367d69557 100644
> --- a/src/fns.c
> +++ b/src/fns.c
> @@ -19,6 +19,7 @@ Copyright (C) 1985-2025 Free Software Foundation, Inc.
>  
>  #include <config.h>
>  
> +#include <attribute.h>
>  #include <stdlib.h>
>  #include <sys/random.h>
>  #include <unistd.h>
> @@ -6016,7 +6017,7 @@ hexbuf_digest (char *hexbuf, void const *digest, int digest_size)
>  
>    for (int i = digest_size - 1; i >= 0; i--)
>      {
> -      static char const hexdigit[16] = "0123456789abcdef";
> +      static char const hexdigit[16] ATTRIBUTE_NONSTRING = "0123456789abcdef";
>        int p_i = p[i];
>        hexbuf[2 * i] = hexdigit[p_i >> 4];
>        hexbuf[2 * i + 1] = hexdigit[p_i & 0xf];
> diff --git a/src/json.c b/src/json.c
> index 5795c582ce0..7a34a5042c6 100644
> --- a/src/json.c
> +++ b/src/json.c
> @@ -19,6 +19,7 @@ Copyright (C) 2017-2025 Free Software Foundation, Inc.
>  
>  #include <config.h>
>  
> +#include <attribute.h>
>  #include <errno.h>
>  #include <stddef.h>
>  #include <stdint.h>
> @@ -323,7 +324,7 @@ json_out_string (json_out_t *jo, Lisp_Object str, int skip)
>  {
>    /* FIXME: this code is slow, make faster! */
>  
> -  static const char hexchar[16] = "0123456789ABCDEF";
> +  static const char hexchar[16] ATTRIBUTE_NONSTRING = "0123456789ABCDEF";
>    ptrdiff_t len = SBYTES (str);
>    json_make_room (jo, len + 2);
>    json_out_byte (jo, '"');

IMO, this is unnecessarily complex, and will also need us to make sure
this attribute is supported everywhere.  Doesn't the below fix the
problem?

Paul, WDYT?

diff --git a/src/fns.c b/src/fns.c
index 3f109a8..6e7943f 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -6016,7 +6016,7 @@ hexbuf_digest (char *hexbuf, void const *digest, int digest_size)
 
   for (int i = digest_size - 1; i >= 0; i--)
     {
-      static char const hexdigit[16] = "0123456789abcdef";
+      static char const hexdigit[17] = "0123456789abcdef";
       int p_i = p[i];
       hexbuf[2 * i] = hexdigit[p_i >> 4];
       hexbuf[2 * i + 1] = hexdigit[p_i & 0xf];
diff --git a/src/json.c b/src/json.c
index 5795c58..94a82df 100644
--- a/src/json.c
+++ b/src/json.c
@@ -323,7 +323,7 @@ json_out_string (json_out_t *jo, Lisp_Object str, int skip)
 {
   /* FIXME: this code is slow, make faster! */
 
-  static const char hexchar[16] = "0123456789ABCDEF";
+  static const char hexchar[17] = "0123456789ABCDEF";
   ptrdiff_t len = SBYTES (str);
   json_make_room (jo, len + 2);
   json_out_byte (jo, '"');




Reply sent to Paul Eggert <eggert <at> cs.ucla.edu>:
You have taken responsibility. (Sun, 20 Apr 2025 05:42:02 GMT) Full text and rfc822 format available.

Notification sent to Collin Funk <collin.funk1 <at> gmail.com>:
bug acknowledged by developer. (Sun, 20 Apr 2025 05:42:03 GMT) Full text and rfc822 format available.

Message #13 received at 77835-done <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Eli Zaretskii <eliz <at> gnu.org>, Collin Funk <collin.funk1 <at> gmail.com>
Cc: 77835-done <at> debbugs.gnu.org
Subject: Re: bug#77835: [PATCH] Pacify GCC 15
 -Wunterminated-string-initialization warnings
Date: Sat, 19 Apr 2025 22:41:33 -0700
[Message part 1 (text/plain, inline)]
On 2025-04-16 02:40, Eli Zaretskii wrote:
> -      static char const hexdigit[16] = "0123456789abcdef";
> +      static char const hexdigit[17] = "0123456789abcdef";

Although that would pacify GCC, it would make the code more confusing 
because it doesn't clearly indicate to the human reader that hexdigit is 
unusual because it is used only as a 16-element array, not as a 
null-terminated string.

I ran into this problem independently and installed the obvious patch 
(attached). I didn't recall your email suggesting this other solution 
until just now. If you prefer the more-confusing solution please feel to 
install it, but at least please add comments explaining what's going on. 
Thanks.

Closing the bug report as the bug is fixed now, one way or another.
[0001-Pacify-GCC-15-Wunterminated-string-initialization.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#77835; Package emacs. (Sun, 20 Apr 2025 08:11:02 GMT) Full text and rfc822 format available.

Message #16 received at 77835 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: 77835 <at> debbugs.gnu.org, collin.funk1 <at> gmail.com
Subject: Re: bug#77835: [PATCH] Pacify GCC 15
 -Wunterminated-string-initialization warnings
Date: Sun, 20 Apr 2025 11:10:04 +0300
> Date: Sat, 19 Apr 2025 22:41:33 -0700
> Cc: 77835-done <at> debbugs.gnu.org
> From: Paul Eggert <eggert <at> cs.ucla.edu>
> 
> On 2025-04-16 02:40, Eli Zaretskii wrote:
> > -      static char const hexdigit[16] = "0123456789abcdef";
> > +      static char const hexdigit[17] = "0123456789abcdef";
> 
> Although that would pacify GCC, it would make the code more confusing 
> because it doesn't clearly indicate to the human reader that hexdigit is 
> unusual because it is used only as a 16-element array, not as a 
> null-terminated string.
> 
> I ran into this problem independently and installed the obvious patch 
> (attached). I didn't recall your email suggesting this other solution 
> until just now. If you prefer the more-confusing solution please feel to 
> install it, but at least please add comments explaining what's going on. 

How portable is the attribute you used to fix this?  AFAICT, it is
supported since GCC 4.8, but our INSTALL indicates that we support
older versions of GCC.  Or does Gnulib solve this?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#77835; Package emacs. (Sun, 20 Apr 2025 08:25:02 GMT) Full text and rfc822 format available.

Message #19 received at 77835 <at> debbugs.gnu.org (full text, mbox):

From: Collin Funk <collin.funk1 <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 77835 <at> debbugs.gnu.org, Paul Eggert <eggert <at> cs.ucla.edu>
Subject: Re: bug#77835: [PATCH] Pacify GCC 15
 -Wunterminated-string-initialization warnings
Date: Sun, 20 Apr 2025 01:24:33 -0700
Eli Zaretskii <eliz <at> gnu.org> writes:

> How portable is the attribute you used to fix this?  AFAICT, it is
> supported since GCC 4.8, but our INSTALL indicates that we support
> older versions of GCC.  Or does Gnulib solve this?

The ATTRIBUTE_* macros are equal to _GL_ATTRIBUTE_* defined in
gnulib-common.m4. For older compilers it is defined to expand to
nothing. So everything should work fine.

Collin




This bug report was last modified 4 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.