From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: Bruce Richardson <bruce.richardson@intel.com>,
stable@dpdk.org, Maxime Coquelin <maxime.coquelin@redhat.com>,
Chenbo Xia <chenbox@nvidia.com>,
Yuanhan Liu <yliu@fridaylinux.org>,
David Marchand <david.marchand@redhat.com>,
Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH 3/3] vhost: remove use of strncpy
Date: Tue, 23 Jun 2026 15:19:29 +0100 [thread overview]
Message-ID: <20260623141930.704771-4-bruce.richardson@intel.com> (raw)
In-Reply-To: <20260623141930.704771-1-bruce.richardson@intel.com>
The strlcpy is preferred over use of strncpy, which removes the need to
try and explicitly null-terminate some string buffers. We can also
simplify some name length handling as a result of this, as we no longer
need to use strnlen to clamp the length before calling the set_ifname
function.
Fixes: a277c7159876 ("vhost: refactor code structure")
Fixes: 0adb8eccc6a6 ("vhost: add VDUSE device creation and destruction")
Fixes: c171a2d5ff17 ("vhost: use strlcpy instead of strncpy")
Cc: stable@dpdk.org
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
lib/vhost/socket.c | 4 +---
lib/vhost/vduse.c | 2 +-
lib/vhost/vhost.c | 12 +++---------
lib/vhost/vhost.h | 2 +-
4 files changed, 6 insertions(+), 14 deletions(-)
diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index 70e582a18d..0943b3e9bb 100644
--- a/lib/vhost/socket.c
+++ b/lib/vhost/socket.c
@@ -207,7 +207,6 @@ static void
vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
{
int vid;
- size_t size;
struct vhost_user_connection *conn;
int ret;
struct virtio_net *dev;
@@ -226,8 +225,7 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
goto err;
}
- size = strnlen(vsocket->path, PATH_MAX);
- vhost_set_ifname(vid, vsocket->path, size);
+ vhost_set_ifname(vid, vsocket->path);
vhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,
vsocket->net_compliant_ol_flags, vsocket->stats_enabled,
diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c
index 0b5d158fee..f8a4a8edcb 100644
--- a/lib/vhost/vduse.c
+++ b/lib/vhost/vduse.c
@@ -796,7 +796,7 @@ vduse_device_create(const char *path, bool compliant_ol_flags, bool extbuf, bool
goto out_dev_destroy;
}
- strncpy(dev->ifname, path, IF_NAME_SZ - 1);
+ strlcpy(dev->ifname, path, sizeof(dev->ifname));
dev->vduse_ctrl_fd = control_fd;
dev->vduse_dev_fd = dev_fd;
diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c
index 7e68b2c3be..fde8acb00c 100644
--- a/lib/vhost/vhost.c
+++ b/lib/vhost/vhost.c
@@ -776,20 +776,15 @@ vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
}
void
-vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
+vhost_set_ifname(int vid, const char *if_name)
{
struct virtio_net *dev;
- unsigned int len;
dev = get_device(vid);
if (dev == NULL)
return;
- len = if_len > sizeof(dev->ifname) ?
- sizeof(dev->ifname) : if_len;
-
- strncpy(dev->ifname, if_name, len);
- dev->ifname[sizeof(dev->ifname) - 1] = '\0';
+ strlcpy(dev->ifname, if_name, sizeof(dev->ifname));
}
void
@@ -915,8 +910,7 @@ rte_vhost_get_ifname(int vid, char *buf, size_t len)
len = RTE_MIN(len, sizeof(dev->ifname));
- strncpy(buf, dev->ifname, len);
- buf[len - 1] = '\0';
+ strlcpy(buf, dev->ifname, len);
return 0;
}
diff --git a/lib/vhost/vhost.h b/lib/vhost/vhost.h
index ee61f7415e..1c957d2929 100644
--- a/lib/vhost/vhost.h
+++ b/lib/vhost/vhost.h
@@ -877,7 +877,7 @@ int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
-void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
+void vhost_set_ifname(int, const char *if_name);
void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags, bool stats_enabled,
bool support_iommu);
void vhost_enable_extbuf(int vid);
--
2.53.0
next prev parent reply other threads:[~2026-06-23 14:19 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-23 14:19 [PATCH 0/3] lib: remove use of strncpy Bruce Richardson
2026-06-23 14:19 ` [PATCH 1/3] ethdev: " Bruce Richardson
2026-06-23 14:19 ` [PATCH 2/3] eventdev: improve bounds checks for names in adapter create Bruce Richardson
2026-06-23 14:19 ` Bruce Richardson [this message]
2026-06-24 1:53 ` [PATCH 0/3] lib: remove use of strncpy fengchengwen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260623141930.704771-4-bruce.richardson@intel.com \
--to=bruce.richardson@intel.com \
--cc=chenbox@nvidia.com \
--cc=david.marchand@redhat.com \
--cc=dev@dpdk.org \
--cc=maxime.coquelin@redhat.com \
--cc=stable@dpdk.org \
--cc=stephen@networkplumber.org \
--cc=yliu@fridaylinux.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox