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 <mwi...@suse.com>
---
 libmultipath/parser.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

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 */
-- 
2.21.0

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel

Reply via email to