diff mbox series

[v2,04/30] libmultipath: fix -Wstringop-overflow warning in merge_words()

Message ID 20190624092756.7769-5-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 24, 2019, 9:27 a.m. UTC
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 | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

Comments

Surachai Saiwong June 24, 2019, 11:29 a.m. UTC | #1
2562-06-24 16:27 GMT+07:00, Martin Wilck <mwilck@suse.com>:
> 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 | 15 ++++++---------
>  1 file changed, 6 insertions(+), 9 deletions(-)
>
> diff --git a/libmultipath/dmparser.c b/libmultipath/dmparser.c
> index 04f675c1..a8b0b71a 100644
> --- a/libmultipath/dmparser.c
> +++ b/libmultipath/dmparser.c
> @@ -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;
>  }
> --
> 2.21.0
>
> --
> dm-devel mailing list
> dm-devel@redhat.com
> https://www.redhat.com/mailman/listinfo/dm-devel

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

Patch

diff --git a/libmultipath/dmparser.c b/libmultipath/dmparser.c
index 04f675c1..a8b0b71a 100644
--- a/libmultipath/dmparser.c
+++ b/libmultipath/dmparser.c
@@ -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;
 }