diff mbox series

[13/25] child: do not overlap on memcpy()

Message ID 20220209222610.438470-14-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:

gcc -g -O2 -Wall -W -I. -DVERSION=\"4.00\"
-DDATADIR=\"/usr/local/share\" `pkg-config --cflags libtirpc`
-Wimplicit-fallthrough=2   -c -o child.o child.c
In function ‘parse_randomstring’,
    inlined from ‘child_run’ at child.c:465:8:
child.c:291:17: warning: ‘memcpy’ accessing 255 bytes at offsets 0 and 1
overlaps 254 bytes at offset 1 [-Wrestrict]
  291 |                 memcpy(str, str+1, sizeof(str)-1);

We fix this by using a copy of the string to a original string
so to avoid having memcpy() overlap.

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

Patch

diff --git a/child.c b/child.c
index 2545e4c..715452e 100644
--- a/child.c
+++ b/child.c
@@ -250,6 +250,7 @@  static int parse_randomstring(char *line)
 	int num;
 	char *pstart, *pend, rndc[2];
 	unsigned int idx;
+	char str_orig[256];
 	char str[256];
 	size_t min_len;
 
@@ -262,6 +263,7 @@  again:
 	/* Truncate or use the smaller size passed */
 	min_len = strlen(line) < sizeof(str) ? strlen(line) : sizeof(str);
 	strncpy(str, pstart, min_len);
+	strncpy(str_orig, pstart, min_len);
 
 	pend = index(str, ']');
 	if (pstart == NULL) {
@@ -288,7 +290,7 @@  finished:
 	}
 	/* remote initial " */
 	while (str[0] == '"') {
-		memcpy(str, str+1, sizeof(str)-1);
+		memcpy(str, str_orig+1, sizeof(str_orig)-1);
 	}
 	/* remote trailing " */
 	while (1) {