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

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]



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@xxxxxxxxxx>
---
 child.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

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) {
-- 
2.34.1




[Index of Archives]     [Linux Filesystems Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux