Fix GCC's -Wunterminated-string-initialization warning in edns0.c.

GCC complains that writing the five-character "ODNS\0" string into
a four-element char magic[4] array truncates the NUL character.
The warning's rationale is that this is incompatible with C++, or
maybe non-intentional.

GCC 8 has added a nonstring variable attribute, clang 20.1 does
not yet support this, but clang's Git head does.

Add an ATTRIBUTE_NONSTRING macro, currently only defined on GCC >= 8
as __attribute__ ((nonstring)).  This successfully suppresses
the warning on Fedora Linux 42's default compiler.

The alternative would be to replace the "ODNS" literal by {0} and
instead memcpy(opt.magic, "ODNS", sizeof(opt.magic)); on the next line,
which is correct, C++ compatible, but also less concise.
This commit is contained in:
Matthias Andree
2025-05-20 21:07:27 +02:00
committed by Simon Kelley
parent c7a909ad65
commit 96bdb42d40
2 changed files with 7 additions and 1 deletions

View File

@@ -48,6 +48,12 @@
#define ATTRIBUTE_NORETURN #define ATTRIBUTE_NORETURN
#endif #endif
#if __GNUC__ + 0 >= 8 // clang 20.1.0 does not yet support this
#define ATTRIBUTE_NONSTRING __attribute__ ((nonstring))
#else
#define ATTRIBUTE_NONSTRING
#endif
/* get these before config.h for IPv6 stuff... */ /* get these before config.h for IPv6 stuff... */
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>

View File

@@ -494,7 +494,7 @@ int check_source(struct dns_header *header, size_t plen, unsigned char *pseudohe
#define UMBRELLA_DEVICESZ sizeof(daemon->umbrella_device) #define UMBRELLA_DEVICESZ sizeof(daemon->umbrella_device)
struct umbrella_opt { struct umbrella_opt {
u8 magic[4]; u8 magic[4] ATTRIBUTE_NONSTRING;
u8 version; u8 version;
u8 flags; u8 flags;
/* We have 4 possible fields since we'll never send both IPv4 and /* We have 4 possible fields since we'll never send both IPv4 and