From 4a4ba1f2eb0be2da9e88560246730af87788295f Mon Sep 17 00:00:00 2001 From: "Kaleb S. KEITHLE" Date: Fri, 9 Nov 2018 09:45:05 -0500 Subject: core: fix strncpy warnings Since gcc-8.2.x (fedora-28 or so) gcc has been emitting warnings about buggy use of strncpy. Most uses that gcc warns about in our sources are exactly backwards; the 'limit' or len is the strlen/size of the _source param_, giving exactly zero protection against overruns. (Which was, after all, one of the points of using strncpy in the first place.) IOW, many warnings are about uses that look approximately like this: ... char dest[8]; char src[] = "this is a string longer than eight chars"; ... strncpy (dest, src, sizeof(src)); /* boom */ ... The len/limit should be sizeof(dest). Note: the above example has a definite over-run. In our source the overrun is typically only theoretical (but possibly exploitable.) Also strncpy doesn't null-terminate on truncation; snprintf does; prefer snprintf over strncpy. Mildly surprising that coverity doesn't warn/isn't warning about this. Change-Id: I022d5c6346a751e181ad44d9a099531c1172626e updates: bz#1193929 Signed-off-by: Kaleb S. KEITHLE --- rpc/rpc-lib/src/rpc-clnt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'rpc/rpc-lib/src') diff --git a/rpc/rpc-lib/src/rpc-clnt.c b/rpc/rpc-lib/src/rpc-clnt.c index 9582f02b461..2505998b3d4 100644 --- a/rpc/rpc-lib/src/rpc-clnt.c +++ b/rpc/rpc-lib/src/rpc-clnt.c @@ -116,8 +116,8 @@ call_bail(void *data) { trans = conn->trans; if (trans) { - strncpy(peerid, conn->trans->peerinfo.identifier, - sizeof(peerid) - 1); + (void)snprintf(peerid, sizeof(peerid), "%s", + conn->trans->peerinfo.identifier); } } pthread_mutex_unlock(&conn->lock); -- cgit