From 10796633e84fc513ea820020ec17271edba82c4f Mon Sep 17 00:00:00 2001 From: Siddhesh Poyarekar Date: Mon, 19 Feb 2018 00:28:12 +0530 Subject: [PATCH] dev: Leave last byte in strncpy for NULL The length of the strncpy should be one less than the destination to ensure that there is space for the last NULL byte in case the source is longer than the destination. Change-Id: Iea65fa6327c8242bd8ddf4bf9a5a2b5164996495 Signed-off-by: Siddhesh Poyarekar Reviewed-on: https://gem5-review.googlesource.com/8561 Reviewed-by: Gabe Black Reviewed-by: Andreas Sandberg Maintainer: Andreas Sandberg --- src/dev/net/ethertap.cc | 2 +- src/dev/virtio/fs9p.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dev/net/ethertap.cc b/src/dev/net/ethertap.cc index 4b5b9bdb0..ca19b4884 100644 --- a/src/dev/net/ethertap.cc +++ b/src/dev/net/ethertap.cc @@ -404,7 +404,7 @@ EtherTap::EtherTap(const Params *p) : EtherTapBase(p) struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; - strncpy(ifr.ifr_name, p->tap_device_name.c_str(), IFNAMSIZ); + strncpy(ifr.ifr_name, p->tap_device_name.c_str(), IFNAMSIZ - 1); if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) panic("Failed to access tap device %s.\n", ifr.ifr_name); diff --git a/src/dev/virtio/fs9p.cc b/src/dev/virtio/fs9p.cc index f2eb8ba52..7857feafc 100644 --- a/src/dev/virtio/fs9p.cc +++ b/src/dev/virtio/fs9p.cc @@ -371,7 +371,7 @@ VirtIO9PDiod::startDiod() fatal_if(sizeof(socket_address.sun_path) <= socket_path.length(), "Incorrect length of socket path"); strncpy(socket_address.sun_path, socket_path.c_str(), - sizeof(socket_address.sun_path)); + sizeof(socket_address.sun_path) - 1); if (bind(socket_id, (struct sockaddr*) &socket_address, sizeof(struct sockaddr_un)) == -1){ perror("Socket binding"); -- 2.30.2