diff mbox series

[v2] cachefiles: Add __nonstring annotations for unterminated strings

Message ID 20250312200825.it.269-kees@kernel.org (mailing list archive)
State New
Headers show
Series [v2] cachefiles: Add __nonstring annotations for unterminated strings | expand

Commit Message

Kees Cook March 12, 2025, 8:08 p.m. UTC
When a character array without a terminating NUL character has a static
initializer, GCC 15's -Wunterminated-string-initialization will only
warn if the array lacks the "nonstring" attribute[1]. Mark the array
with __nonstring to correctly identify the char array as "not a C string"
and thereby eliminate the warning:

../fs/cachefiles/key.c:12:9: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (65 chars into 64 available) [-Wunterminated-string-initialization]
   12 |         "0123456789"                    /* 0 - 9 */
      |         ^~~~~~~~~~~~

Since the array is used only for single character look-ups, use
__nonstring:

fs/cachefiles/key.c:123:                name[len++] = cachefiles_charmap[acc & 63];
fs/cachefiles/key.c:125:                name[len++] = cachefiles_charmap[acc & 63];
fs/cachefiles/key.c:127:                name[len++] = cachefiles_charmap[acc & 63];
fs/cachefiles/key.c:129:                name[len++] = cachefiles_charmap[acc & 63];

Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178 [1]
Signed-off-by: Kees Cook <kees@kernel.org>
---
 v2: improve commit log
 v1: https://lore.kernel.org/lkml/20250310222402.work.925-kees@kernel.org/
Cc: David Howells <dhowells@redhat.com>
Cc: netfs@lists.linux.dev
---
 fs/cachefiles/key.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/fs/cachefiles/key.c b/fs/cachefiles/key.c
index bf935e25bdbe..b48525680e73 100644
--- a/fs/cachefiles/key.c
+++ b/fs/cachefiles/key.c
@@ -8,7 +8,7 @@ 
 #include <linux/slab.h>
 #include "internal.h"
 
-static const char cachefiles_charmap[64] =
+static const char cachefiles_charmap[64] __nonstring =
 	"0123456789"			/* 0 - 9 */
 	"abcdefghijklmnopqrstuvwxyz"	/* 10 - 35 */
 	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"	/* 36 - 61 */