diff mbox series

[02/30] libmultipath: fix gcc -Wstringop-truncation warning in set_value()

Message ID 20190607130552.13203-3-mwilck@suse.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show
Series multipath-tools: gcc9, VPD parsing, and get_uid fixes | expand

Commit Message

Martin Wilck June 7, 2019, 1:05 p.m. UTC
Fixes the following warning:

In function ‘strncat’,
    inlined from ‘set_value’ at parser.c:382:3:
/usr/include/bits/string_fortified.h:136:10: warning: ‘__builtin_strncat’
    output truncated before terminating nul copying as many bytes from a
    string as its length [-Wstringop-truncation]
  136 |   return __builtin___strncat_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parser.c: In function ‘set_value’:
parser.c:382:3: note: length computed here
  382 |   strncat(alloc, str, strlen(str));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

gcc's stringop checker expects that the size argument of strncat() is derived
from the destination, not source, side.
See https://developers.redhat.com/blog/2018/05/24/detecting-string-truncation-with-gcc-8/

Fix typo in error message along the way.

Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 libmultipath/parser.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/libmultipath/parser.c b/libmultipath/parser.c
index 92ef7cf5..e00c5fff 100644
--- a/libmultipath/parser.c
+++ b/libmultipath/parser.c
@@ -345,17 +345,13 @@  set_value(vector strvec)
 		if (alloc)
 			memcpy(alloc, str, size);
 		else
-			condlog(0, "can't allocate memeory for option '%s'",
-				(char *)VECTOR_SLOT(strvec, 0));
+			goto oom;
 		return alloc;
 	}
 	/* Even empty quotes counts as a value (An empty string) */
 	alloc = (char *) MALLOC(sizeof (char));
-	if (!alloc) {
-		condlog(0, "can't allocate memeory for option '%s'",
-			(char *)VECTOR_SLOT(strvec, 0));
-		return NULL;
-	}
+	if (!alloc)
+		goto oom;
 	for (i = 2; i < VECTOR_SIZE(strvec); i++) {
 		str = VECTOR_SLOT(strvec, i);
 		if (!str) {
@@ -373,15 +369,17 @@  set_value(vector strvec)
 		alloc = REALLOC(alloc, sizeof (char) * len);
 		if (!alloc) {
 			FREE(tmp);
-			condlog(0, "can't allocate memeory for option '%s'",
-				(char *)VECTOR_SLOT(strvec, 0));
-			return NULL;
+			goto oom;
 		}
 		if (*alloc != '\0')
 			strncat(alloc, " ", 1);
-		strncat(alloc, str, strlen(str));
+		strncat(alloc, str, len - strlen(alloc) - 1);
 	}
 	return alloc;
+oom:
+	condlog(0, "can't allocate memory for option '%s'",
+		(char *)VECTOR_SLOT(strvec, 0));
+	return NULL;
 }
 
 /* non-recursive configuration stream handler */