[PATCH kvmtool 2/3] virtio: use strlcpy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



GCC 8.x complains about improper usage of strncpy in virtio/net.c and
virtio/scsi.c:
In function 'virtio_scsi_init_one',
    inlined from 'virtio_scsi_init' at virtio/scsi.c:285:7:
virtio/scsi.c:247:2: error: 'strncpy' specified bound 224 equals destination size [-Werror=stringop-truncation]
  strncpy((char *)&sdev->target.vhost_wwpn, disk->wwpn, sizeof(sdev->target.vhost_wwpn));
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this and the other occurences in virtio/ by using strlcpy instead
of strncpy.

Signed-off-by: Andre Przywara <andre.przywara@xxxxxxx>
---
 virtio/net.c  | 5 +++--
 virtio/scsi.c | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/virtio/net.c b/virtio/net.c
index 35ff2e9b..09140691 100644
--- a/virtio/net.c
+++ b/virtio/net.c
@@ -8,6 +8,7 @@
 #include "kvm/uip.h"
 #include "kvm/guest_compat.h"
 #include "kvm/iovec.h"
+#include "kvm/strbuf.h"
 
 #include <linux/vhost.h>
 #include <linux/virtio_net.h>
@@ -283,12 +284,12 @@ static int virtio_net_request_tap(struct net_dev *ndev, struct ifreq *ifr,
 	memset(ifr, 0, sizeof(*ifr));
 	ifr->ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
 	if (tapname)
-		strncpy(ifr->ifr_name, tapname, sizeof(ifr->ifr_name));
+		strlcpy(ifr->ifr_name, tapname, sizeof(ifr->ifr_name));
 
 	ret = ioctl(ndev->tap_fd, TUNSETIFF, ifr);
 
 	if (ret >= 0)
-		strncpy(ndev->tap_name, ifr->ifr_name, sizeof(ndev->tap_name));
+		strlcpy(ndev->tap_name, ifr->ifr_name, sizeof(ndev->tap_name));
 	return ret;
 }
 
diff --git a/virtio/scsi.c b/virtio/scsi.c
index c8400b64..a72bb2a9 100644
--- a/virtio/scsi.c
+++ b/virtio/scsi.c
@@ -8,6 +8,7 @@
 #include "kvm/guest_compat.h"
 #include "kvm/virtio-pci.h"
 #include "kvm/virtio.h"
+#include "kvm/strbuf.h"
 
 #include <linux/kernel.h>
 #include <linux/virtio_scsi.h>
@@ -255,7 +256,7 @@ static int virtio_scsi_init_one(struct kvm *kvm, struct disk_image *disk)
 		},
 		.kvm			= kvm,
 	};
-	strncpy((char *)&sdev->target.vhost_wwpn, disk->wwpn, sizeof(sdev->target.vhost_wwpn));
+	strlcpy((char *)&sdev->target.vhost_wwpn, disk->wwpn, sizeof(sdev->target.vhost_wwpn));
 	sdev->target.vhost_tpgt = strtol(disk->tpgt, NULL, 0);
 
 	virtio_init(kvm, sdev, &sdev->vdev, &scsi_dev_virtio_ops,
-- 
2.17.1




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux