Subject: Re: bug#77835: [PATCH] Pacify GCC 15 -Wunterminated-string-initialization warnings
From: Paul Eggert <[email-masked]>
To: Eli Zaretskii <[email-masked]>, Collin Funk <[email-masked]>
Cc: [email-masked]
Date: Sat, 19 Apr 2025 22:41:33 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

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.

From e2fb12a40ca2b90dfedbfe916ed8a87345ca89f1 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 19 Apr 2025 19:22:37 -0700
Subject: [PATCH] Pacify GCC 15 -Wunterminated-string-initialization

* src/fns.c (hexbuf_digest):
* src/json.c (json_out_string):
Add ATTRIBUTE_NONSTRING to character arrays that are not strings.
---
 src/fns.c  | 2 +-
 src/json.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/fns.c b/src/fns.c
index 52f667a72a5..21916b6fb46 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -6014,7 +6014,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 beac242b709..44eae653eb5 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[16] ATTRIBUTE_NONSTRING = "0123456789ABCDEF";
   ptrdiff_t len = SBYTES (str);
   json_make_room (jo, len + 2);
   json_out_byte (jo, '"');
-- 
2.48.1