Siddhesh Poyarekar has submitted this change and it was merged. ( )

Change subject: dev: Leave last byte in strncpy for NULL

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-by: Gabe Black <>
Reviewed-by: Andreas Sandberg <>
Maintainer: Andreas Sandberg <>
M src/dev/net/
M src/dev/virtio/
2 files changed, 2 insertions(+), 2 deletions(-)

  Andreas Sandberg: Looks good to me, approved; Looks good to me, approved
  Gabe Black: Looks good to me, approved

diff --git a/src/dev/net/ b/src/dev/net/
index 4b5b9bd..ca19b48 100644
--- a/src/dev/net/
+++ b/src/dev/net/
@@ -404,7 +404,7 @@
     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/ b/src/dev/virtio/
index f2eb8ba..7857fea 100644
--- a/src/dev/virtio/
+++ b/src/dev/virtio/
@@ -371,7 +371,7 @@
     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");

To view, visit
To unsubscribe, or for help writing mail filters, visit

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: Iea65fa6327c8242bd8ddf4bf9a5a2b5164996495
Gerrit-Change-Number: 8561
Gerrit-PatchSet: 3
Gerrit-Owner: Siddhesh Poyarekar <>
Gerrit-Reviewer: Andreas Sandberg <>
Gerrit-Reviewer: Gabe Black <>
Gerrit-Reviewer: Siddhesh Poyarekar <>
Gerrit-MessageType: merged
gem5-dev mailing list

Reply via email to