]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: compiler: add __nonstring macro
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Mon, 31 Mar 2025 09:47:45 +0000 (11:47 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 31 Mar 2025 11:50:28 +0000 (13:50 +0200)
GCC 15 throws the following warning on fixed-size char arrays if they do not
contain terminated NUL:

src/tools.c:2041:25: error: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (17 chars into 16 available) [-Werror=unterminated-string-initialization]
 2041 | const char hextab[16] = "0123456789ABCDEF";

We are using a couple of such definitions for some constants. Converting them
to flexible arrays, like: hextab[] = "0123456789ABCDEF" may have consequences,
as enlarged arrays won't fit anymore where they were possibly located due to
the memory alignement constraints.

GCC adds 'nonstring' variable attribute for such char arrays, but clang and
other compilers don't have it. Let's wrap 'nonstring' with our
__nonstring macro, which will test if the compiler supports this attribute.

This fixes the issue #2910.

include/haproxy/compiler.h
src/tools.c

index c19d2c46afbe14ea0fe73a59f143a90344f960ee..fc7679e8a33abbeaf393a05cd1ce5d39b2cd476f 100644 (file)
 #define __has_feature(x) 0
 #endif
 
+/* gcc 15 throws warning if fixed-size char array does not contain a terminating
+ * NUL. gcc has an attribute 'nonstring', which allows to suppress this warning
+ * for such array declarations. But it's not the case for clang and other
+ * compilers.
+ */
+#if __has_attribute(nonstring)
+#define __nonstring __attribute__ ((nonstring))
+#else
+#define __nonstring
+#endif
+
 #endif /* _HAPROXY_COMPILER_H */
index ce927f90c534bd91121beeeb03fe6d149a4d7766..5cd89be9f46d8108b99707eb7179748a1ff07c89 100644 (file)
@@ -65,6 +65,7 @@ extern void *__elf_aux_vector;
 #include <haproxy/api.h>
 #include <haproxy/applet.h>
 #include <haproxy/chunk.h>
+#include <haproxy/compiler.h>
 #include <haproxy/dgram.h>
 #include <haproxy/global.h>
 #include <haproxy/hlua.h>
@@ -2038,7 +2039,7 @@ int addr_is_local(const struct netns_entry *ns,
  *
  * Return the address of the \0 character, or NULL on error
  */
-const char hextab[16] = "0123456789ABCDEF";
+const char hextab[16] __nonstring = "0123456789ABCDEF";
 char *encode_string(char *start, char *stop,
                    const char escape, const long *map,
                    const char *string)