From 1649bbf0300c086d98b855c03b1867e962883e9a Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 15 Oct 2022 09:29:35 -0700 Subject: Fix -Wstringop-truncation warnings in safe_strncpy() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In function ‘safe_strncpy’, inlined from ‘set_utmpx’ at sessreg.c:540:3, inlined from ‘main’ at sessreg.c:357:2: sessreg.c:204:11: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation] 204 | (void)strncpy(dest, src, n); | ^~~~~~~~~~~~~~~~~~~~~ In function ‘safe_strncpy’, inlined from ‘set_utmpx’ at sessreg.c:530:4, inlined from ‘main’ at sessreg.c:357:2: sessreg.c:204:11: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation] 204 | (void)strncpy(dest, src, n); | ^~~~~~~~~~~~~~~~~~~~~ In function ‘safe_strncpy’, inlined from ‘set_utmpx’ at sessreg.c:532:3, inlined from ‘main’ at sessreg.c:357:2: sessreg.c:204:11: warning: ‘strncpy’ specified bound 257 equals destination size [-Wstringop-truncation] 204 | (void)strncpy(dest, src, n); | ^~~~~~~~~~~~~~~~~~~~~ In function ‘safe_strncpy’, inlined from ‘set_utmpx’ at sessreg.c:540:3, inlined from ‘main’ at sessreg.c:357:2: sessreg.c:204:11: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation] 204 | (void)strncpy(dest, src, n); | ^~~~~~~~~~~~~~~~~~~~~ In function ‘safe_strncpy’, inlined from ‘main’ at sessreg.c:423:5: sessreg.c:204:11: warning: ‘strncpy’ specified bound 8 equals destination size [-Wstringop-truncation] 204 | (void)strncpy(dest, src, n); | ^~~~~~~~~~~~~~~~~~~~~ In function ‘safe_strncpy’, inlined from ‘main’ at sessreg.c:425:5: sessreg.c:204:11: warning: ‘strncpy’ specified bound 16 equals destination size [-Wstringop-truncation] 204 | (void)strncpy(dest, src, n); | ^~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Alan Coopersmith --- sessreg.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sessreg.c b/sessreg.c index c574f6e..d39ac74 100644 --- a/sessreg.c +++ b/sessreg.c @@ -201,9 +201,10 @@ sysnerr (int x, const char *s) static void safe_strncpy(char *dest, const char *src, size_t n) { - (void)strncpy(dest, src, n); - if (n > 0) + if (n > 0) { + strncpy(dest, src, n - 1); dest[n - 1] = '\0'; + } } int -- cgit v1.2.3