diff mbox series

[2/2] hw/rdma: avoid suspicious strncpy() use

Message ID 20200316160702.478964-3-stefanha@redhat.com
State New
Headers show
Series gcc 9.2 strncpy(3) warnings fixes | expand

Commit Message

Stefan Hajnoczi March 16, 2020, 4:07 p.m. UTC
gcc (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1) with sanitizers enabled
reports the following error:

  CC      x86_64-softmmu/hw/rdma/vmw/pvrdma_dev_ring.o
In file included from /usr/include/string.h:495,
                 from include/qemu/osdep.h:101,
                 from hw/rdma/vmw/pvrdma_dev_ring.c:16:
In function ‘strncpy’,
    inlined from ‘pvrdma_ring_init’ at hw/rdma/vmw/pvrdma_dev_ring.c:33:5:
/usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 32 equals destination size [-Werror=stringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use pstrcpy() instead of strncpy().  It is guaranteed to NUL-terminate
strings.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 hw/rdma/vmw/pvrdma_dev_ring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Juan Quintela March 16, 2020, 5:28 p.m. UTC | #1
Stefan Hajnoczi <stefanha@redhat.com> wrote:
> gcc (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1) with sanitizers enabled
> reports the following error:
>
>   CC      x86_64-softmmu/hw/rdma/vmw/pvrdma_dev_ring.o
> In file included from /usr/include/string.h:495,
>                  from include/qemu/osdep.h:101,
>                  from hw/rdma/vmw/pvrdma_dev_ring.c:16:
> In function ‘strncpy’,
>     inlined from ‘pvrdma_ring_init’ at hw/rdma/vmw/pvrdma_dev_ring.c:33:5:
> /usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 32 equals destination size [-Werror=stringop-truncation]
>   106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Use pstrcpy() instead of strncpy().  It is guaranteed to NUL-terminate
> strings.
>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>

Reviewed-by: Juan Quintela <quintela@redhat.com>
Yuval Shaia March 20, 2020, 11:55 a.m. UTC | #2
On Mon, 16 Mar 2020 at 18:07, Stefan Hajnoczi <stefanha@redhat.com> wrote:

> gcc (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1) with sanitizers enabled
> reports the following error:
>
>   CC      x86_64-softmmu/hw/rdma/vmw/pvrdma_dev_ring.o
> In file included from /usr/include/string.h:495,
>                  from include/qemu/osdep.h:101,
>                  from hw/rdma/vmw/pvrdma_dev_ring.c:16:
> In function ‘strncpy’,
>     inlined from ‘pvrdma_ring_init’ at hw/rdma/vmw/pvrdma_dev_ring.c:33:5:
> /usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’
> specified bound 32 equals destination size [-Werror=stringop-truncation]
>   106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos
> (__dest));
>       |
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Use pstrcpy() instead of strncpy().  It is guaranteed to NUL-terminate
> strings.
>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>  hw/rdma/vmw/pvrdma_dev_ring.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/hw/rdma/vmw/pvrdma_dev_ring.c b/hw/rdma/vmw/pvrdma_dev_ring.c
> index d7bc7f5ccc..74b8fa834c 100644
> --- a/hw/rdma/vmw/pvrdma_dev_ring.c
> +++ b/hw/rdma/vmw/pvrdma_dev_ring.c
> @@ -14,6 +14,7 @@
>   */
>
>  #include "qemu/osdep.h"
> +#include "qemu/cutils.h"
>  #include "hw/pci/pci.h"
>  #include "cpu.h"
>
> @@ -30,8 +31,7 @@ int pvrdma_ring_init(PvrdmaRing *ring, const char *name,
> PCIDevice *dev,
>      int i;
>      int rc = 0;
>
> -    strncpy(ring->name, name, MAX_RING_NAME_SZ);
> -    ring->name[MAX_RING_NAME_SZ - 1] = 0;
> +    pstrcpy(ring->name, MAX_RING_NAME_SZ, name);
>      ring->dev = dev;
>      ring->ring_state = ring_state;
>      ring->max_elems = max_elems;
> --
> 2.24.1
>
>
Thanks,

Reviewed-by: Yuval Shaia <yuval.shaia.ml.gmail.com>
Marcel Apfelbaum March 21, 2020, 5:25 p.m. UTC | #3
Hi Stefan,

On 3/20/20 1:55 PM, Yuval Shaia wrote:
>
>
> On Mon, 16 Mar 2020 at 18:07, Stefan Hajnoczi <stefanha@redhat.com 
> <mailto:stefanha@redhat.com>> wrote:
>
>     gcc (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1) with sanitizers enabled
>     reports the following error:
>
>       CC      x86_64-softmmu/hw/rdma/vmw/pvrdma_dev_ring.o
>     In file included from /usr/include/string.h:495,
>                      from include/qemu/osdep.h:101,
>                      from hw/rdma/vmw/pvrdma_dev_ring.c:16:
>     In function ‘strncpy’,
>         inlined from ‘pvrdma_ring_init’ at
>     hw/rdma/vmw/pvrdma_dev_ring.c:33:5:
>     /usr/include/bits/string_fortified.h:106:10: error:
>     ‘__builtin_strncpy’ specified bound 32 equals destination size
>     [-Werror=stringop-truncation]
>       106 |   return __builtin___strncpy_chk (__dest, __src, __len,
>     __bos (__dest));
>           | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>     Use pstrcpy() instead of strncpy().  It is guaranteed to NUL-terminate
>     strings.
>
>     Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com
>     <mailto:stefanha@redhat.com>>
>     ---
>      hw/rdma/vmw/pvrdma_dev_ring.c | 4 ++--
>      1 file changed, 2 insertions(+), 2 deletions(-)
>
>     diff --git a/hw/rdma/vmw/pvrdma_dev_ring.c
>     b/hw/rdma/vmw/pvrdma_dev_ring.c
>     index d7bc7f5ccc..74b8fa834c 100644
>     --- a/hw/rdma/vmw/pvrdma_dev_ring.c
>     +++ b/hw/rdma/vmw/pvrdma_dev_ring.c
>     @@ -14,6 +14,7 @@
>       */
>
>      #include "qemu/osdep.h"
>     +#include "qemu/cutils.h"
>      #include "hw/pci/pci.h"
>      #include "cpu.h"
>
>     @@ -30,8 +31,7 @@ int pvrdma_ring_init(PvrdmaRing *ring, const
>     char *name, PCIDevice *dev,
>          int i;
>          int rc = 0;
>
>     -    strncpy(ring->name, name, MAX_RING_NAME_SZ);
>     -    ring->name[MAX_RING_NAME_SZ - 1] = 0;
>     +    pstrcpy(ring->name, MAX_RING_NAME_SZ, name);
>          ring->dev = dev;
>          ring->ring_state = ring_state;
>          ring->max_elems = max_elems;
>     -- 
>     2.24.1
>
>
> Thanks,
>
> Reviewed-by: Yuval Shaia <yuval.shaia.ml.gmail.com 
> <http://yuval.shaia.ml.gmail.com>>

I'll add this patch to the pvrdma queue.

Thanks,
Marcel
diff mbox series

Patch

diff --git a/hw/rdma/vmw/pvrdma_dev_ring.c b/hw/rdma/vmw/pvrdma_dev_ring.c
index d7bc7f5ccc..74b8fa834c 100644
--- a/hw/rdma/vmw/pvrdma_dev_ring.c
+++ b/hw/rdma/vmw/pvrdma_dev_ring.c
@@ -14,6 +14,7 @@ 
  */
 
 #include "qemu/osdep.h"
+#include "qemu/cutils.h"
 #include "hw/pci/pci.h"
 #include "cpu.h"
 
@@ -30,8 +31,7 @@  int pvrdma_ring_init(PvrdmaRing *ring, const char *name, PCIDevice *dev,
     int i;
     int rc = 0;
 
-    strncpy(ring->name, name, MAX_RING_NAME_SZ);
-    ring->name[MAX_RING_NAME_SZ - 1] = 0;
+    pstrcpy(ring->name, MAX_RING_NAME_SZ, name);
     ring->dev = dev;
     ring->ring_state = ring_state;
     ring->max_elems = max_elems;