diff mbox series

[12/25] child: be expicit about string truncation goal

Message ID 20220209222610.438470-13-mcgrof@kernel.org (mailing list archive)
State New, archived
Headers show
Series dbench: fix compile warnings and update a bit | expand

Commit Message

Luis Chamberlain Feb. 9, 2022, 10:25 p.m. UTC
Fix this compilation warning:

child.c:260:9: warning: ‘strncpy’ specified bound 256 equals destination
size [-Wstringop-truncation]
  260 |         strncpy(str, pstart, sizeof(str));

We do this by being explicit about our goal to truncate or use
the smaller string passed.

Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 child.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/child.c b/child.c
index d340860..2545e4c 100644
--- a/child.c
+++ b/child.c
@@ -251,13 +251,17 @@  static int parse_randomstring(char *line)
 	char *pstart, *pend, rndc[2];
 	unsigned int idx;
 	char str[256];
+	size_t min_len;
 
 again:
 	pstart = index(line, '[');
 	if (pstart == NULL) {
 		goto finished;
 	}
-	strncpy(str, pstart, sizeof(str));
+
+	/* Truncate or use the smaller size passed */
+	min_len = strlen(line) < sizeof(str) ? strlen(line) : sizeof(str);
+	strncpy(str, pstart, min_len);
 
 	pend = index(str, ']');
 	if (pstart == NULL) {