diff mbox series

[for-3.1?,1/3] sheepdog: fix stringop-truncation warning

Message ID 20181120152753.10463-2-marcandre.lureau@redhat.com
State New
Headers show
Series strcpy: fix stringop-truncation warnings | expand

Commit Message

Marc-André Lureau Nov. 20, 2018, 3:27 p.m. UTC
It seems adding an assert is enough to silence GCC.
(sd_parse_snapid_or_tag() g_strlcpy() ensures that we don't get in
that situation)

~/src/qemu/block/sheepdog.c: In function 'find_vdi_name':
~/src/qemu/block/sheepdog.c:1239:5: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
     strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 block/sheepdog.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Eric Blake Nov. 20, 2018, 4:56 p.m. UTC | #1
On 11/20/18 9:27 AM, Marc-André Lureau wrote:
> It seems adding an assert is enough to silence GCC.
> (sd_parse_snapid_or_tag() g_strlcpy() ensures that we don't get in
> that situation)
> 
> ~/src/qemu/block/sheepdog.c: In function 'find_vdi_name':
> ~/src/qemu/block/sheepdog.c:1239:5: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
>       strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
>       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> cc1: all warnings being treated as errors
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>   block/sheepdog.c | 1 +
>   1 file changed, 1 insertion(+)

Reviewed-by: Eric Blake <eblake@redhat.com>

and safe for 3.1 in my opinion
Philippe Mathieu-Daudé Nov. 20, 2018, 7:35 p.m. UTC | #2
On 20/11/18 16:27, Marc-André Lureau wrote:
> It seems adding an assert is enough to silence GCC.
> (sd_parse_snapid_or_tag() g_strlcpy() ensures that we don't get in
> that situation)
> 
> ~/src/qemu/block/sheepdog.c: In function 'find_vdi_name':
> ~/src/qemu/block/sheepdog.c:1239:5: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
>       strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
>       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> cc1: all warnings being treated as errors
> 

Fixes: https://bugs.launchpad.net/bugs/1803872

> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>   block/sheepdog.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/block/sheepdog.c b/block/sheepdog.c
> index 0125df9d49..f8877b611d 100644
> --- a/block/sheepdog.c
> +++ b/block/sheepdog.c
> @@ -1236,6 +1236,7 @@ static int find_vdi_name(BDRVSheepdogState *s, const char *filename,
>        * don't want the send_req to read uninitialized data.
>        */
>       strncpy(buf, filename, SD_MAX_VDI_LEN);
> +    assert(strlen(tag) < SD_MAX_VDI_TAG_LEN);
>       strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
>   
>       memset(&hdr, 0, sizeof(hdr));
> 

I tried to fix this warning this way:

-    char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
+    struct {
+        char vdi[SD_MAX_VDI_LEN];
+        char vdi_tag[SD_MAX_VDI_TAG_LEN];
+    } buf;

...

-    strncpy(buf, filename, SD_MAX_VDI_LEN);
-    strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
+    strncpy(buf.vdi, filename, SD_MAX_VDI_LEN);
+    strncpy(buf.vdi_tag, tag, SD_MAX_VDI_TAG_LEN);

but your patch is simpler.

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Thanks,

Phil.
diff mbox series

Patch

diff --git a/block/sheepdog.c b/block/sheepdog.c
index 0125df9d49..f8877b611d 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -1236,6 +1236,7 @@  static int find_vdi_name(BDRVSheepdogState *s, const char *filename,
      * don't want the send_req to read uninitialized data.
      */
     strncpy(buf, filename, SD_MAX_VDI_LEN);
+    assert(strlen(tag) < SD_MAX_VDI_TAG_LEN);
     strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
 
     memset(&hdr, 0, sizeof(hdr));