dev: Leave last byte in strncpy for NULL
authorSiddhesh Poyarekar <siddhesh.poyarekar@gmail.com>
Sun, 18 Feb 2018 18:58:12 +0000 (00:28 +0530)
committerSiddhesh Poyarekar <siddhesh.poyarekar@gmail.com>
Tue, 6 Mar 2018 19:45:34 +0000 (19:45 +0000)
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 <siddhesh.poyarekar@gmail.com>
Reviewed-on: https://gem5-review.googlesource.com/8561
Reviewed-by: Gabe Black <gabeblack@google.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>

src/dev/net/ethertap.cc
src/dev/virtio/fs9p.cc

index 4b5b9bdb0043ee6f12d352de8e748fadcbcdce3a..ca19b4884f1c4152c7ebbd63e3cbf2c19430a8c0 100644 (file)
@@ -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);
index f2eb8ba52aa2c127268e3f69fd3a974f34e00dbb..7857feafc373cd47a0da6b74ca7fb8413d3fee3e 100644 (file)
@@ -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");