libmultipath: fix -Wstringop-overflow warning in merge_words()
authorMartin Wilck <mwilck@suse.com>
Mon, 24 Jun 2019 09:27:29 +0000 (11:27 +0200)
committerChristophe Varoqui <christophe.varoqui@opensvc.com>
Wed, 3 Jul 2019 06:30:37 +0000 (08:30 +0200)
Fixes the following warning from gcc 9:

In file included from /usr/include/string.h:494,
                 from dmparser.c:8:
In function ‘strncpy’,
    inlined from ‘merge_words’ at dmparser.c:41:2:
/usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’
                 specified bound depends on the length of the source argument
                 [-Wstringop-overflow=]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dmparser.c: In function ‘merge_words’:
dmparser.c:41:19: note: length computed here
   41 |  strncpy(p, word, strlen(word) + 1);
      |                   ^~~~~~~~~~~~

Signed-off-by: Martin Wilck <mwilck@suse.com>
libmultipath/dmparser.c

index 04f675c..a8b0b71 100644 (file)
@@ -21,24 +21,21 @@ static int
 merge_words(char **dst, char *word)
 {
        char * p = *dst;
-       int len;
+       int len, dstlen;
 
-       len = strlen(*dst) + strlen(word) + 1;
-       *dst = REALLOC(*dst, len + 1);
+       dstlen = strlen(*dst);
+       len = dstlen + strlen(word) + 2;
+       *dst = REALLOC(*dst, len);
 
        if (!*dst) {
                free(p);
                return 1;
        }
 
-       p = *dst;
-
-       while (*p != '\0')
-               p++;
-
+       p = *dst + dstlen;
        *p = ' ';
        ++p;
-       strncpy(p, word, strlen(word) + 1);
+       strncpy(p, word, len - dstlen - 1);
 
        return 0;
 }