summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2022-10-15 09:29:35 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2022-10-16 12:27:09 -0700
commit1649bbf0300c086d98b855c03b1867e962883e9a (patch)
treeadd7e6dfbe581b73e9dd2e3ae0a50c98bf332c3e
parent5e791f5e412de52c2e110c38f3091102afb4a8a0 (diff)
Fix -Wstringop-truncation warnings in safe_strncpy()
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 <alan.coopersmith@oracle.com>
-rw-r--r--sessreg.c5
1 files 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