@@ -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;
}
@@ -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,
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@arm.com> --- virtio/net.c | 5 +++-- virtio/scsi.c | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-)