Skip to content

Commit 6508009

Browse files
committed
rtld, libthr: Use nonstring attribute to silence compiler warning
With Clang 21 at least, we start to get warnings like: ``` rtld.c:333:58: error: initializer-string for character array is too long, array size is 4 but initializer has size 5 (including the null terminating character); did you mean to use the 'nonstring' attribute? [-Werror,-Wunterminated-string-initialization] 333 | static const char rtld_utrace_sig[RTLD_UTRACE_SIG_SZ] = RTLD_UTRACE_SIG; | ^~~~~~~~~~~~~~~ ./rtld_utrace.h:49:27: note: expanded from macro 'RTLD_UTRACE_SIG' 49 | #define RTLD_UTRACE_SIG "RTLD" | ``` Looking up the `nonstring` attribute [1], it does seem to be the correct thing --- indicating a char array which should not have a null terminator byte --- to use in these two cases. Note: I discovered this when fixing Nixpkgs's FreeBSD cross compilation support. I understand that FreeBSD may not officially support this version of Clang yet, and so the CPP that is needed is a bit annoying. But it still hopefully will make things easier once it comes time to bump the supported version of Clang to one with this attribute. I am hoping thus that this patch is acceptable, as just early readiness for when FreeBSD does upgrade to that version of Clang. [1]: https://clang.llvm.org/docs/AttributeReference.html#nonstring Signed-off-by: John Ericson <[email protected]>
1 parent cb1315c commit 6508009

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/libthr/thread/thr_printf.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,18 @@ _thread_printf(int fd, const char *fmt, ...)
5858
va_end(ap);
5959
}
6060

61+
/* GCC has had this for a long while now, but Clang only got it
62+
recently. */
63+
#if __has_attribute(__nonstring__)
64+
# define __nonstring __attribute__((__nonstring__))
65+
#else
66+
# define __nonstring
67+
#endif
68+
6169
void
6270
_thread_vprintf(int fd, const char *fmt, va_list ap)
6371
{
64-
static const char digits[16] = "0123456789abcdef";
72+
__nonstring static const char digits[16] = "0123456789abcdef";
6573
char buf[20];
6674
char *s;
6775
unsigned long r, u;

libexec/rtld-elf/rtld.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,20 @@ static void (*rtld_exit_ptr)(void);
343343
ld_utrace_log(e, h, mb, ms, r, n); \
344344
} while (0)
345345

346+
/* GCC has had this for a long while now, but Clang only got it
347+
recently. */
348+
#if __has_attribute(__nonstring__)
349+
# define __nonstring __attribute__((__nonstring__))
350+
#else
351+
# define __nonstring
352+
#endif
353+
346354
static void
347355
ld_utrace_log(int event, void *handle, void *mapbase, size_t mapsize,
348356
int refcnt, const char *name)
349357
{
350358
struct utrace_rtld ut;
351-
static const char rtld_utrace_sig[RTLD_UTRACE_SIG_SZ] = RTLD_UTRACE_SIG;
359+
__nonstring static const char rtld_utrace_sig[RTLD_UTRACE_SIG_SZ] = RTLD_UTRACE_SIG;
352360

353361
memset(&ut, 0, sizeof(ut)); /* clear holes */
354362
memcpy(ut.sig, rtld_utrace_sig, sizeof(ut.sig));

0 commit comments

Comments
 (0)