Skip to contentSkip to conversation

drivers: replace strncpy with strlcpy

PATCH Superseded Bruce Richardson 5 participants 51 messages Jun 23 – Sep 22

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover drivers: replace strncpy with strlcpy

Many uses of strncpy in DPDK drivers can be directly replaced by a call to strlcpy instead, which is safer in that it always null- terminates the string. This AI assisted patchset makes those simple replacements, adjusting lengths as appropriate.

After this set, there are still a number of drivers with strncpy calls in them, but those are not simple strncpy->strlcpy replacements, so left for later rework.

Bruce Richardson (17):
common/cnxk: replace strncpy with strlcpy
common/mlx5: replace strncpy with strlcpy
crypto/mvsam: replace strncpy with strlcpy
crypto/scheduler: replace strncpy with strlcpy
event/cnxk: replace strncpy with strlcpy
net/ark: replace strncpy with strlcpy
net/bnx2x: replace strncpy with strlcpy
net/cnxk: replace strncpy with strlcpy
net/dpaa: replace strncpy with strlcpy
net/ionic: replace strncpy with strlcpy
net/mlx4: replace strncpy with strlcpy
net/mlx5: replace strncpy with strlcpy
net/nfp: replace strncpy with strlcpy
net/qede: replace strncpy with strlcpy
net/softnic: replace strncpy with strlcpy
net/vhost: replace strncpy with strlcpy
net/virtio: replace strncpy with strlcpy

 drivers/common/cnxk/roc_model.c                   | 8 ++++----
 drivers/common/mlx5/linux/mlx5_common_os.c        | 2 +-
 drivers/crypto/mvsam/rte_mrvl_pmd.c               | 2 +-
 drivers/crypto/scheduler/scheduler_pmd.c          | 8 ++++----
 drivers/event/cnxk/cnxk_eventdev_stats.c          | 2 +-
 drivers/net/ark/ark_ethdev.c                      | 2 +-
 drivers/net/bnx2x/bnx2x.c                         | 2 +-
 drivers/net/bnx2x/bnx2x_vfpf.c                    | 2 +-
 drivers/net/cnxk/cnxk_eswitch.c                   | 4 ++--
 drivers/net/cnxk/cnxk_rep_msg.c                   | 2 +-
 drivers/net/dpaa/dpaa_ethdev.c                    | 2 +-
 drivers/net/ionic/ionic_main.c                    | 4 ++--
 drivers/net/mlx4/mlx4_ethdev.c                    | 2 +-
 drivers/net/mlx5/mlx5.c                           | 8 ++++----
 drivers/net/mlx5/windows/mlx5_ethdev_os.c         | 2 +-
 drivers/net/nfp/nfpcore/nfp_resource.c            | 2 +-
 drivers/net/qede/qede_ethdev.c                    | 2 +-
 drivers/net/softnic/conn.c                        | 4 ++--
 drivers/net/vhost/rte_eth_vhost.c                 | 2 +-
 drivers/net/virtio/virtio_user/vhost_kernel_tap.c | 2 +-
 20 files changed, 32 insertions(+), 32 deletions(-)

--
2.53.0

#1 lore raw

PATCH 1/17 common/cnxk: replace strncpy with strlcpy

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 014a9e222bac ("common/cnxk: add model init and IO handling API")
Fixes b315581c66dc ("common/cnxk: skip probing SoC environment for CN9K")
---
 drivers/common/cnxk/roc_model.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/common/cnxk/roc_model.c +4 -4
index f0312a5400..ddd7ee8634 100644
@@ -211,15 +211,15 @@ populate_model(struct roc_model *model, uint32_t midr)
 		if (model_db[i].impl == impl && model_db[i].part == part &&
 		    model_db[i].major == major && model_db[i].minor == minor) {
 			model->flag = model_db[i].flag;
-			strncpy(model->name, model_db[i].name,
-				ROC_MODEL_STR_LEN_MAX - 1);
+			strlcpy(model->name, model_db[i].name,
+				ROC_MODEL_STR_LEN_MAX);
 			found = true;
 			break;
 		}
 not_found:
 	if (!found) {
 		model->flag = 0;
-		strncpy(model->name, "unknown", ROC_MODEL_STR_LEN_MAX - 1);
+		strlcpy(model->name, "unknown", ROC_MODEL_STR_LEN_MAX);
 		plt_err("Invalid RoC model (impl=0x%x, part=0x%x, major=0x%x, minor=0x%x)",
 			impl, part, major, minor);
 	}
@@ -297,7 +297,7 @@ of_env_get(struct roc_model *model)
 	FILE *fp;
 
 	if (access(path, F_OK) != 0) {
-		strncpy(model->env, "HW_PLATFORM", ROC_MODEL_STR_LEN_MAX - 1);
+		strlcpy(model->env, "HW_PLATFORM", ROC_MODEL_STR_LEN_MAX);
 		model->flag |= ROC_ENV_HW;
 		return;
 	}
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/17 common/mlx5: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes aec086c9f1c8 ("common/mlx5: share kernel interface name getter")
---
 drivers/common/mlx5/linux/mlx5_common_os.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/common/mlx5/linux/mlx5_common_os.c +1 -1
index e3db6c4124..53bcc9b844 100644
@@ -231,7 +231,7 @@ mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
 		rte_errno = ENOENT;
 		return -rte_errno;
 	}
-	strncpy(ifname, match, IF_NAMESIZE);
+	strlcpy(ifname, match, IF_NAMESIZE);
 	return 0;
 }
 
Signature · 1 line
2.53.0
#4 lore raw

PATCH 3/17 crypto/mvsam: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 25b05a1c806b ("crypto/mvsam: parse max number of sessions")
---
 drivers/crypto/mvsam/rte_mrvl_pmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/mvsam/rte_mrvl_pmd.c +1 -1
index a824719fb0..65a63c9d62 100644
@@ -1146,7 +1146,7 @@ parse_name_arg(const char *key __rte_unused,
 		return -EINVAL;
 	}
 
-	strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
+	strlcpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
 
 	return 0;
 }
Signature · 1 line
2.53.0
#5 lore raw

PATCH 4/17 crypto/scheduler: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 85b00824aeef ("crypto/scheduler: rename slave to worker")
---
 drivers/crypto/scheduler/scheduler_pmd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/scheduler/scheduler_pmd.c +4 -4
index 95ce893f05..ceaefa329b 100644
@@ -229,10 +229,10 @@ cryptodev_scheduler_create(const char *name,
 			return -ENOMEM;
 		}
 
-		strncpy(sched_ctx->init_worker_names[
+		strlcpy(sched_ctx->init_worker_names[
 					sched_ctx->nb_init_workers],
 				init_params->worker_names[i],
-				RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+				RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
 		sched_ctx->nb_init_workers++;
 	}
@@ -443,8 +443,8 @@ parse_worker_arg(const char *key __rte_unused,
 		return -ENOMEM;
 	}
 
-	strncpy(param->worker_names[param->nb_workers++], value,
-			RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+	strlcpy(param->worker_names[param->nb_workers++], value,
+			RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
 	return 0;
 }
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH 04/17] crypto/scheduler: replace strncpy with strlcpy

Quoted 3 lines · Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 85b00824aeef ("crypto/scheduler: rename slave to worker")

Correction, this is not the introducer commit. Instead it's:

Fixes 50e14527b9d1 ("crypto/scheduler: improve parameters parsing")
Quoted 38 lines
---
 drivers/crypto/scheduler/scheduler_pmd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/scheduler/scheduler_pmd.c +4 -4
index 95ce893f05..ceaefa329b 100644
@@ -229,10 +229,10 @@ cryptodev_scheduler_create(const char *name,
 			return -ENOMEM;
 		}
 
-		strncpy(sched_ctx->init_worker_names[
+		strlcpy(sched_ctx->init_worker_names[
 					sched_ctx->nb_init_workers],
 				init_params->worker_names[i],
-				RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+				RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
 		sched_ctx->nb_init_workers++;
 	}
@@ -443,8 +443,8 @@ parse_worker_arg(const char *key __rte_unused,
 		return -ENOMEM;
 	}
 
-	strncpy(param->worker_names[param->nb_workers++], value,
-			RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+	strlcpy(param->worker_names[param->nb_workers++], value,
+			RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
 	return 0;
 }
Signature · 1 line
2.53.0
#7 lore raw

PATCH 5/17 event/cnxk: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes b5a52c9d97e2 ("event/cnxk: add event port and queue xstats")
---
 drivers/event/cnxk/cnxk_eventdev_stats.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev_stats.c +1 -1
index 6dea91aedf..288d34bd7e 100644
@@ -283,7 +283,7 @@ cnxk_sso_xstats_get_names(const struct rte_eventdev *event_dev,
 
 	for (i = 0; i < xstats_mode_count; i++) {
 		xidx = i + start_offset;
-		strncpy(xstats_names[i].name, xstats_names_copy[xidx].name,
+		strlcpy(xstats_names[i].name, xstats_names_copy[xidx].name,
 			sizeof(xstats_names[i].name));
 		ids[i] = xidx;
 	}
Signature · 1 line
2.53.0
#8 lore raw

PATCH 6/17 net/ark: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 1131cbf0fb2b ("net/ark: stub PMD for Atomic Rules Arkville")
---
 drivers/net/ark/ark_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ark/ark_ethdev.c +1 -1
index 9272e271e6..d6b1abf383 100644
@@ -929,7 +929,7 @@ process_file_args(const char *key, const char *value, void *extra_args)
 			return -1;
 		}
 		if (first) {
-			strncpy(args, line, ARK_MAX_ARG_LEN);
+			strlcpy(args, line, ARK_MAX_ARG_LEN);
 			first = 0;
 		} else {
 			strncat(args, line, ARK_MAX_ARG_LEN);
Signature · 1 line
2.53.0
#9 lore raw

PATCH 7/17 net/bnx2x: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 540a211084a7 ("bnx2x: driver core")
---
 drivers/net/bnx2x/bnx2x.c      | 2 +-
 drivers/net/bnx2x/bnx2x_vfpf.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/net/bnx2x/bnx2x.c +1 -1
index 8790c858d5..1a3956b224 100644
@@ -3596,7 +3596,7 @@ static void bnx2x_drv_info_ether_stat(struct bnx2x_softc *sc)
 {
 	struct eth_stats_info *ether_stat = &sc->sp->drv_info_to_mcp.ether_stat;
 
-	strncpy(ether_stat->version, BNX2X_DRIVER_VERSION,
+	strlcpy(ether_stat->version, BNX2X_DRIVER_VERSION,
 		ETH_STAT_INFO_VERSION_LEN);
 
 	sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
drivers/net/bnx2x/bnx2x_vfpf.c +1 -1
index 5411df3a38..23f576877f 100644
@@ -303,7 +303,7 @@ int bnx2x_vf_get_resources(struct bnx2x_softc *sc, uint8_t tx_count, uint8_t rx_
 
 	PMD_DRV_LOG(DEBUG, sc, "status block count = %d, base status block = %x",
 		sc->igu_sb_cnt, sc->igu_base_sb);
-	strncpy(sc->fw_ver, sc_resp.fw_ver, sizeof(sc->fw_ver));
+	strlcpy(sc->fw_ver, sc_resp.fw_ver, sizeof(sc->fw_ver));
 
 	if (rte_is_valid_assigned_ether_addr(&sc_resp.resc.current_mac_addr))
 		rte_ether_addr_copy(&sc_resp.resc.current_mac_addr,
Signature · 1 line
2.53.0
#10 lore raw

PATCH 8/17 net/cnxk: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 804c585658ea ("net/cnxk: add representor control plane")
---
 drivers/net/cnxk/cnxk_eswitch.c | 4 ++--
 drivers/net/cnxk/cnxk_rep_msg.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/net/cnxk/cnxk_eswitch.c +2 -2
index e45c7dfd07..50c3e54109 100644
@@ -124,8 +124,8 @@ cnxk_eswitch_dev_remove(struct rte_pci_device *pci_dev)
 				}
 				sun.sun_family = AF_UNIX;
 				sunlen = sizeof(struct sockaddr_un);
-				strncpy(sun.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH,
-					sizeof(sun.sun_path) - 1);
+				strlcpy(sun.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH,
+					sizeof(sun.sun_path));
 
 				if (connect(sock_fd, (struct sockaddr *)&sun, sunlen) < 0) {
 					plt_err("Failed to connect socket: %s, err %d",
drivers/net/cnxk/cnxk_rep_msg.c +1 -1
index a222e2b5cd..7b92e7a5f7 100644
@@ -138,7 +138,7 @@ open_socket_ctrl_channel(void)
 
 	memset(&un, 0, sizeof(struct sockaddr_un));
 	un.sun_family = AF_UNIX;
-	strncpy(un.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH, sizeof(un.sun_path) - 1);
+	strlcpy(un.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH, sizeof(un.sun_path));
 
 	if (bind(sock_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
 		plt_err("Failed to bind %s: %s", un.sun_path, strerror(errno));
Signature · 1 line
2.53.0
#11 lore raw

PATCH 9/17 net/dpaa: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes ee0fa7552a0a ("net/dpaa: forbid MTU configuration for shared interface")
---
 drivers/net/dpaa/dpaa_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/dpaa/dpaa_ethdev.c +1 -1
index 9f976d179b..8b1072f9b9 100644
@@ -247,7 +247,7 @@ dpaa_eth_dev_configure(struct rte_eth_dev *dev)
 			DPAA_PMD_ERR("Cannot open IF socket");
 			return -errno;
 		}
-		strncpy(ifr.ifr_name, dpaa_intf->name, IFNAMSIZ - 1);
+		strlcpy(ifr.ifr_name, dpaa_intf->name, IFNAMSIZ);
 
 		if (ioctl(socket_fd, SIOCGIFMTU, &ifr) < 0) {
 			DPAA_PMD_ERR("Cannot get interface mtu");
Signature · 1 line
2.53.0
#12 lore raw

PATCH 10/17 net/ionic: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 5ef518098ec6 ("net/ionic: register and initialize adapter")
---
 drivers/net/ionic/ionic_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ionic/ionic_main.c +2 -2
index 814bb3b8f4..d5545b1ecb 100644
@@ -393,8 +393,8 @@ ionic_identify(struct ionic_adapter *adapter)
 	ident->drv.kernel_ver = 0;
 	snprintf(ident->drv.kernel_ver_str,
 		sizeof(ident->drv.kernel_ver_str), "DPDK");
-	strncpy(ident->drv.driver_ver_str, IONIC_DRV_VERSION,
-		sizeof(ident->drv.driver_ver_str) - 1);
+	strlcpy(ident->drv.driver_ver_str, IONIC_DRV_VERSION,
+		sizeof(ident->drv.driver_ver_str));
 
 	nwords = RTE_MIN(drv_size, cmd_size);
 	for (i = 0; i < nwords; i++)
Signature · 1 line
2.53.0
#13 lore raw

PATCH 11/17 net/mlx4: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 61cbdd419478 ("net/mlx4: separate device control functions")
---
 drivers/net/mlx4/mlx4_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/mlx4/mlx4_ethdev.c +1 -1
index efc6ee4577..ff353af224 100644
@@ -127,7 +127,7 @@ mlx4_get_ifname(const struct mlx4_priv *priv, char (*ifname)[IF_NAMESIZE])
 		rte_errno = ENODEV;
 		return -rte_errno;
 	}
-	strncpy(*ifname, match, sizeof(*ifname));
+	strlcpy(*ifname, match, sizeof(*ifname));
 	return 0;
 }
 
Signature · 1 line
2.53.0
#15 lore raw

PATCH 12/17 net/mlx5: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes ca1418ce3910 ("common/mlx5: share device context object")
Fixes 9e58a50c059f ("net/mlx5: fix interface name parameter definition")
---
 drivers/net/mlx5/mlx5.c                   | 8 ++++----
 drivers/net/mlx5/windows/mlx5_ethdev_os.c | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/net/mlx5/mlx5.c +4 -4
index 61c26d1206..24fda18710 100644
@@ -1916,10 +1916,10 @@ mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
 	sh->txpp.skew = sh->config.tx_skew;
 	sh->refcnt = 1;
 	sh->max_port = spawn->max_port;
-	strncpy(sh->ibdev_name, mlx5_os_get_ctx_device_name(sh->cdev->ctx),
-		sizeof(sh->ibdev_name) - 1);
-	strncpy(sh->ibdev_path, mlx5_os_get_ctx_device_path(sh->cdev->ctx),
-		sizeof(sh->ibdev_path) - 1);
+	strlcpy(sh->ibdev_name, mlx5_os_get_ctx_device_name(sh->cdev->ctx),
+		sizeof(sh->ibdev_name));
+	strlcpy(sh->ibdev_path, mlx5_os_get_ctx_device_path(sh->cdev->ctx),
+		sizeof(sh->ibdev_path));
 	sh->phdev = mlx5_get_physical_device(sh->cdev);
 	if (!sh->phdev)
 		goto error;
drivers/net/mlx5/windows/mlx5_ethdev_os.c +1 -1
index e24ff367af..ecb8914a65 100644
@@ -67,7 +67,7 @@ mlx5_get_ifname(const struct rte_eth_dev *dev, char ifname[MLX5_NAMESIZE])
 	}
 	priv = dev->data->dev_private;
 	context_obj = (mlx5_context_st *)priv->sh->cdev->ctx;
-	strncpy(ifname, context_obj->mlx5_dev.name, MLX5_NAMESIZE);
+	strlcpy(ifname, context_obj->mlx5_dev.name, MLX5_NAMESIZE);
 	return 0;
 }
 
Signature · 1 line
2.53.0
#17 lore raw

PATCH 13/17 net/nfp: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes c7e9729da6b5 ("net/nfp: support CPP")
---
 drivers/net/nfp/nfpcore/nfp_resource.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/nfp/nfpcore/nfp_resource.c +1 -1
index 6437a78852..781350c46f 100644
@@ -168,7 +168,7 @@ nfp_resource_acquire(struct nfp_cpp *cpp,
 
 	memset(res, 0, sizeof(*res));
 
-	strncpy(res->name, name, NFP_RESOURCE_ENTRY_NAME_SZ);
+	strlcpy(res->name, name, sizeof(res->name));
 
 	dev_mutex = nfp_cpp_mutex_alloc(cpp, NFP_RESOURCE_TBL_TARGET,
 			NFP_RESOURCE_TBL_BASE, NFP_RESOURCE_TBL_KEY);
Signature · 1 line
2.53.0
#18 lore raw

PATCH 14/17 net/qede: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 7eca78cefde8 ("net/qede: fix driver version string")
---
 drivers/net/qede/qede_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/qede/qede_ethdev.c +1 -1
index 4efc2dd349..2f37f956cb 100644
@@ -2596,7 +2596,7 @@ static int qede_common_dev_init(struct rte_eth_dev *eth_dev, bool is_vf)
 	params.drv_minor = QEDE_PMD_VERSION_MINOR;
 	params.drv_rev = QEDE_PMD_VERSION_REVISION;
 	params.drv_eng = QEDE_PMD_VERSION_PATCH;
-	strncpy((char *)params.name, QEDE_PMD_VER_PREFIX,
+	strlcpy((char *)params.name, QEDE_PMD_VER_PREFIX,
 		QEDE_PMD_DRV_VER_STR_SIZE);
 
 	qede_assign_rxtx_handlers(eth_dev, true);
Signature · 1 line
2.53.0
#19 lore raw

Re: [PATCH 14/17] net/qede: replace strncpy with strlcpy

Quoted 3 lines · Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 7eca78cefde8 ("net/qede: fix driver version string")

Correction:

Fixes 2ea6f76aff40 ("qede: add core driver")
Quoted 23 lines
---
 drivers/net/qede/qede_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/qede/qede_ethdev.c +1 -1
index 4efc2dd349..2f37f956cb 100644
@@ -2596,7 +2596,7 @@ static int qede_common_dev_init(struct rte_eth_dev *eth_dev, bool is_vf)
 	params.drv_minor = QEDE_PMD_VERSION_MINOR;
 	params.drv_rev = QEDE_PMD_VERSION_REVISION;
 	params.drv_eng = QEDE_PMD_VERSION_PATCH;
-	strncpy((char *)params.name, QEDE_PMD_VER_PREFIX,
+	strlcpy((char *)params.name, QEDE_PMD_VER_PREFIX,
 		QEDE_PMD_DRV_VER_STR_SIZE);
 
 	qede_assign_rxtx_handlers(eth_dev, true);
Signature · 1 line
2.53.0
#20 lore raw

PATCH 15/17 net/softnic: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 7709a63bf178 ("net/softnic: add connection agent")
---
 drivers/net/softnic/conn.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/softnic/conn.c +2 -2
index 5b031358d5..d34030ce63 100644
@@ -116,8 +116,8 @@ softnic_conn_init(struct softnic_conn_params *p)
 	}
 
 	/* Fill in */
-	strncpy(conn->welcome, p->welcome, CONN_WELCOME_LEN_MAX);
-	strncpy(conn->prompt, p->prompt, CONN_PROMPT_LEN_MAX);
+	strlcpy(conn->welcome, p->welcome, CONN_WELCOME_LEN_MAX + 1);
+	strlcpy(conn->prompt, p->prompt, CONN_PROMPT_LEN_MAX + 1);
 	conn->buf_size = p->buf_size;
 	conn->msg_in_len_max = p->msg_in_len_max;
 	conn->msg_out_len_max = p->msg_out_len_max;
Signature · 1 line
2.53.0
#21 lore raw

PATCH 16/17 net/vhost: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 2e16597c4bda ("net/vhost: move to vhost library statistics API")
---
 drivers/net/vhost/rte_eth_vhost.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/vhost/rte_eth_vhost.c +1 -1
index 05940f2461..82e9fc929a 100644
@@ -209,7 +209,7 @@ vhost_dev_xstats_get_names(struct rte_eth_dev *dev,
 	}
 
 	for (i = 0; i < count; i++)
-		strncpy(xstats_names[i].name, name[i].name, RTE_ETH_XSTATS_NAME_SIZE);
+		strlcpy(xstats_names[i].name, name[i].name, RTE_ETH_XSTATS_NAME_SIZE);
 
 	free(name);
 
Signature · 1 line
2.53.0
#22 lore raw

PATCH 17/17 net/virtio: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes b72099be7f27 ("net/virtio-user: fix init when using existing tap")
---
 drivers/net/virtio/virtio_user/vhost_kernel_tap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio/virtio_user/vhost_kernel_tap.c +1 -1
index 2431d00c33..da55aba505 100644
@@ -60,7 +60,7 @@ vhost_tap_open(const char *ifname, unsigned int r_flags, bool multi_queue)
 
 retry_mono_q:
 	memset(&ifr, 0, sizeof(ifr));
-	strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
+	strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
 	ifr.ifr_flags = r_flags;
 	if (multi_queue)
 		ifr.ifr_flags |= IFF_MULTI_QUEUE;
Signature · 1 line
2.53.0
#23 lore raw
In reply to Bruce Richardson
Quoted 8 lines · On Tue, 23 Jun 2026 17:51:30 +0100 Bruce Richardson…

Many uses of strncpy in DPDK drivers can be directly replaced by a call to strlcpy instead, which is safer in that it always null- terminates the string. This AI assisted patchset makes those simple replacements, adjusting lengths as appropriate.

After this set, there are still a number of drivers with strncpy calls in them, but those are not simple strncpy->strlcpy replacements, so left for later rework.

Maybe a coccinelle script?

#24 lore raw
Quoted 8 lines · Stephen Hemminger
Quoted 5 lines · On Tue, 23 Jun 2026 17:51:30 +0100 Bruce Richardson…

Many uses of strncpy in DPDK drivers can be directly replaced by a call to strlcpy instead, which is safer in that it always null- terminates the string. This AI assisted patchset makes those simple replacements, adjusting lengths as appropriate.

Quoted 5 lines
Quoted 3 lines

After this set, there are still a number of drivers with strncpy calls in them, but those are not simple strncpy->strlcpy replacements, so left for later rework.

Maybe a coccinelle script?

Don't think it's worth it.

a) All trivial replacements are already done by this set and the other patches I've posted.
b) AI does a better job of handling this anyway, since it's able to properly analyse the context and spot cases of using a length parameter one short when calling strncpy, or when code is improved by using sizeof() as the length parameter.

What probably is needed in scripting though, is a checkpatch check for new strncpy additions. Are you still working on that python replacement?

/Bruce

#25 lore raw

v2 cover drivers: replace strncpy with strlcpy

In reply to Bruce Richardson

Many uses of strncpy in DPDK drivers can be directly replaced by a call to strlcpy instead, which is safer in that it always null- terminates the string. This AI assisted patchset makes those simple replacements, adjusting lengths as appropriate.

After this set, there are still a number of drivers with strncpy calls in them, but those are not simple strncpy->strlcpy replacements, so left for later rework.

v2:
* fix incorrect commit ids for fixlines.
* fix build errors due to missing rte_string_fns.h header include

Bruce Richardson (17):
common/cnxk: replace strncpy with strlcpy
common/mlx5: replace strncpy with strlcpy
crypto/mvsam: replace strncpy with strlcpy
crypto/scheduler: replace strncpy with strlcpy
event/cnxk: replace strncpy with strlcpy
net/ark: replace strncpy with strlcpy
net/bnx2x: replace strncpy with strlcpy
net/cnxk: replace strncpy with strlcpy
net/dpaa: replace strncpy with strlcpy
net/ionic: replace strncpy with strlcpy
net/mlx4: replace strncpy with strlcpy
net/mlx5: replace strncpy with strlcpy
net/nfp: replace strncpy with strlcpy
net/qede: replace strncpy with strlcpy
net/softnic: replace strncpy with strlcpy
net/vhost: replace strncpy with strlcpy
net/virtio: replace strncpy with strlcpy

 drivers/common/cnxk/roc_model.c                   | 8 ++++----
 drivers/common/mlx5/linux/mlx5_common_os.c        | 2 +-
 drivers/crypto/mvsam/rte_mrvl_pmd.c               | 2 +-
 drivers/crypto/scheduler/scheduler_pmd.c          | 8 ++++----
 drivers/event/cnxk/cnxk_eventdev_stats.c          | 2 +-
 drivers/net/ark/ark_ethdev.c                      | 2 +-
 drivers/net/bnx2x/bnx2x.c                         | 2 +-
 drivers/net/bnx2x/bnx2x_vfpf.c                    | 2 +-
 drivers/net/cnxk/cnxk_eswitch.c                   | 4 ++--
 drivers/net/cnxk/cnxk_rep_msg.c                   | 2 +-
 drivers/net/dpaa/dpaa_ethdev.c                    | 2 +-
 drivers/net/ionic/ionic_main.c                    | 4 ++--
 drivers/net/mlx4/mlx4_ethdev.c                    | 2 +-
 drivers/net/mlx5/mlx5.c                           | 8 ++++----
 drivers/net/mlx5/windows/mlx5_ethdev_os.c         | 2 +-
 drivers/net/nfp/nfpcore/nfp_resource.c            | 2 +-
 drivers/net/qede/qede_ethdev.c                    | 2 +-
 drivers/net/softnic/conn.c                        | 6 ++++--
 drivers/net/vhost/rte_eth_vhost.c                 | 2 +-
 drivers/net/virtio/virtio_user/vhost_kernel_tap.c | 2 +-
 20 files changed, 34 insertions(+), 32 deletions(-)

--
2.53.0

#26 lore raw

PATCH v2 1/17 common/cnxk: replace strncpy with strlcpy

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 014a9e222bac ("common/cnxk: add model init and IO handling API")
Fixes b315581c66dc ("common/cnxk: skip probing SoC environment for CN9K")
---
 drivers/common/cnxk/roc_model.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/common/cnxk/roc_model.c +4 -4
index f0312a5400..ddd7ee8634 100644
@@ -211,15 +211,15 @@ populate_model(struct roc_model *model, uint32_t midr)
 		if (model_db[i].impl == impl && model_db[i].part == part &&
 		    model_db[i].major == major && model_db[i].minor == minor) {
 			model->flag = model_db[i].flag;
-			strncpy(model->name, model_db[i].name,
-				ROC_MODEL_STR_LEN_MAX - 1);
+			strlcpy(model->name, model_db[i].name,
+				ROC_MODEL_STR_LEN_MAX);
 			found = true;
 			break;
 		}
 not_found:
 	if (!found) {
 		model->flag = 0;
-		strncpy(model->name, "unknown", ROC_MODEL_STR_LEN_MAX - 1);
+		strlcpy(model->name, "unknown", ROC_MODEL_STR_LEN_MAX);
 		plt_err("Invalid RoC model (impl=0x%x, part=0x%x, major=0x%x, minor=0x%x)",
 			impl, part, major, minor);
 	}
@@ -297,7 +297,7 @@ of_env_get(struct roc_model *model)
 	FILE *fp;
 
 	if (access(path, F_OK) != 0) {
-		strncpy(model->env, "HW_PLATFORM", ROC_MODEL_STR_LEN_MAX - 1);
+		strlcpy(model->env, "HW_PLATFORM", ROC_MODEL_STR_LEN_MAX);
 		model->flag |= ROC_ENV_HW;
 		return;
 	}
Signature · 1 line
2.53.0
#27 lore raw

PATCH v2 2/17 common/mlx5: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes aec086c9f1c8 ("common/mlx5: share kernel interface name getter")
---
 drivers/common/mlx5/linux/mlx5_common_os.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/common/mlx5/linux/mlx5_common_os.c +1 -1
index e3db6c4124..53bcc9b844 100644
@@ -231,7 +231,7 @@ mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
 		rte_errno = ENOENT;
 		return -rte_errno;
 	}
-	strncpy(ifname, match, IF_NAMESIZE);
+	strlcpy(ifname, match, IF_NAMESIZE);
 	return 0;
 }
 
Signature · 1 line
2.53.0
#29 lore raw

PATCH v2 3/17 crypto/mvsam: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 25b05a1c806b ("crypto/mvsam: parse max number of sessions")
---
 drivers/crypto/mvsam/rte_mrvl_pmd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/mvsam/rte_mrvl_pmd.c +1 -1
index a824719fb0..65a63c9d62 100644
@@ -1146,7 +1146,7 @@ parse_name_arg(const char *key __rte_unused,
 		return -EINVAL;
 	}
 
-	strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
+	strlcpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
 
 	return 0;
 }
Signature · 1 line
2.53.0
#30 lore raw

PATCH v2 4/17 crypto/scheduler: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 50e14527b9d1 ("crypto/scheduler: improve parameters parsing")
---
 drivers/crypto/scheduler/scheduler_pmd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/scheduler/scheduler_pmd.c +4 -4
index 95ce893f05..ceaefa329b 100644
@@ -229,10 +229,10 @@ cryptodev_scheduler_create(const char *name,
 			return -ENOMEM;
 		}
 
-		strncpy(sched_ctx->init_worker_names[
+		strlcpy(sched_ctx->init_worker_names[
 					sched_ctx->nb_init_workers],
 				init_params->worker_names[i],
-				RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+				RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
 		sched_ctx->nb_init_workers++;
 	}
@@ -443,8 +443,8 @@ parse_worker_arg(const char *key __rte_unused,
 		return -ENOMEM;
 	}
 
-	strncpy(param->worker_names[param->nb_workers++], value,
-			RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+	strlcpy(param->worker_names[param->nb_workers++], value,
+			RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
 	return 0;
 }
Signature · 1 line
2.53.0
Acked-by on v2 4/17
#31 lore raw

Re: [PATCH v2 04/17] crypto/scheduler: replace strncpy with strlcpy

________________________________
From: Richardson, Bruce <[email protected]>
Sent: 24 June 2026 18:36
To: [email protected] <[email protected]>
Cc: Richardson, Bruce <[email protected]>; [email protected] <[email protected]>; Ji, Kai <[email protected]>; Fan Zhang <[email protected]>; De Lara Guarch, Pablo <[email protected]>
Subject: [PATCH v2 04/17] crypto/scheduler: replace strncpy with strlcpy

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 50e14527b9d1 ("crypto/scheduler: improve parameters parsing")
---
 drivers/crypto/scheduler/scheduler_pmd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/scheduler/scheduler_pmd.c +4 -4
index 95ce893f05..ceaefa329b 100644
@@ -229,10 +229,10 @@ cryptodev_scheduler_create(const char *name,
                         return -ENOMEM;
                 }
 
-               strncpy(sched_ctx->init_worker_names[
+               strlcpy(sched_ctx->init_worker_names[
                                         sched_ctx->nb_init_workers],
                                 init_params->worker_names[i],
-                               RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+                               RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
                 sched_ctx->nb_init_workers++;
         }
@@ -443,8 +443,8 @@ parse_worker_arg(const char *key __rte_unused,
                 return -ENOMEM;
         }
 
-       strncpy(param->worker_names[param->nb_workers++], value,
-                       RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+       strlcpy(param->worker_names[param->nb_workers++], value,
+                       RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
         return 0;
 }

--
2.53.0

Acked-by on v2 4/17
#32 lore raw

Re: [PATCH v2 04/17] crypto/scheduler: replace strncpy with strlcpy

In reply to Bruce Richardson

________________________________
From: Richardson, Bruce <[email protected]>
Sent: 24 June 2026 18:36
To: [email protected] <[email protected]>
Cc: Richardson, Bruce <[email protected]>; [email protected] <[email protected]>; Ji, Kai <[email protected]>; Fan Zhang <[email protected]>; De Lara Guarch, Pablo <[email protected]>
Subject: [PATCH v2 04/17] crypto/scheduler: replace strncpy with strlcpy

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 50e14527b9d1 ("crypto/scheduler: improve parameters parsing")
---
 drivers/crypto/scheduler/scheduler_pmd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/crypto/scheduler/scheduler_pmd.c +4 -4
index 95ce893f05..ceaefa329b 100644
@@ -229,10 +229,10 @@ cryptodev_scheduler_create(const char *name,
                         return -ENOMEM;
                 }
 
-               strncpy(sched_ctx->init_worker_names[
+               strlcpy(sched_ctx->init_worker_names[
                                         sched_ctx->nb_init_workers],
                                 init_params->worker_names[i],
-                               RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+                               RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
                 sched_ctx->nb_init_workers++;
         }
@@ -443,8 +443,8 @@ parse_worker_arg(const char *key __rte_unused,
                 return -ENOMEM;
         }
 
-       strncpy(param->worker_names[param->nb_workers++], value,
-                       RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
+       strlcpy(param->worker_names[param->nb_workers++], value,
+                       RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
 
         return 0;
 }

--
2.53.0

#33 lore raw

PATCH v2 5/17 event/cnxk: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes b5a52c9d97e2 ("event/cnxk: add event port and queue xstats")
---
 drivers/event/cnxk/cnxk_eventdev_stats.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/event/cnxk/cnxk_eventdev_stats.c +1 -1
index 6dea91aedf..288d34bd7e 100644
@@ -283,7 +283,7 @@ cnxk_sso_xstats_get_names(const struct rte_eventdev *event_dev,
 
 	for (i = 0; i < xstats_mode_count; i++) {
 		xidx = i + start_offset;
-		strncpy(xstats_names[i].name, xstats_names_copy[xidx].name,
+		strlcpy(xstats_names[i].name, xstats_names_copy[xidx].name,
 			sizeof(xstats_names[i].name));
 		ids[i] = xidx;
 	}
Signature · 1 line
2.53.0
#34 lore raw

PATCH v2 6/17 net/ark: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 1131cbf0fb2b ("net/ark: stub PMD for Atomic Rules Arkville")
---
 drivers/net/ark/ark_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ark/ark_ethdev.c +1 -1
index 9272e271e6..d6b1abf383 100644
@@ -929,7 +929,7 @@ process_file_args(const char *key, const char *value, void *extra_args)
 			return -1;
 		}
 		if (first) {
-			strncpy(args, line, ARK_MAX_ARG_LEN);
+			strlcpy(args, line, ARK_MAX_ARG_LEN);
 			first = 0;
 		} else {
 			strncat(args, line, ARK_MAX_ARG_LEN);
Signature · 1 line
2.53.0
#35 lore raw

PATCH v2 7/17 net/bnx2x: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 540a211084a7 ("bnx2x: driver core")
---
 drivers/net/bnx2x/bnx2x.c      | 2 +-
 drivers/net/bnx2x/bnx2x_vfpf.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/net/bnx2x/bnx2x.c +1 -1
index 8790c858d5..1a3956b224 100644
@@ -3596,7 +3596,7 @@ static void bnx2x_drv_info_ether_stat(struct bnx2x_softc *sc)
 {
 	struct eth_stats_info *ether_stat = &sc->sp->drv_info_to_mcp.ether_stat;
 
-	strncpy(ether_stat->version, BNX2X_DRIVER_VERSION,
+	strlcpy(ether_stat->version, BNX2X_DRIVER_VERSION,
 		ETH_STAT_INFO_VERSION_LEN);
 
 	sc->sp_objs[0].mac_obj.get_n_elements(sc, &sc->sp_objs[0].mac_obj,
drivers/net/bnx2x/bnx2x_vfpf.c +1 -1
index 5411df3a38..23f576877f 100644
@@ -303,7 +303,7 @@ int bnx2x_vf_get_resources(struct bnx2x_softc *sc, uint8_t tx_count, uint8_t rx_
 
 	PMD_DRV_LOG(DEBUG, sc, "status block count = %d, base status block = %x",
 		sc->igu_sb_cnt, sc->igu_base_sb);
-	strncpy(sc->fw_ver, sc_resp.fw_ver, sizeof(sc->fw_ver));
+	strlcpy(sc->fw_ver, sc_resp.fw_ver, sizeof(sc->fw_ver));
 
 	if (rte_is_valid_assigned_ether_addr(&sc_resp.resc.current_mac_addr))
 		rte_ether_addr_copy(&sc_resp.resc.current_mac_addr,
Signature · 1 line
2.53.0
#36 lore raw

PATCH v2 8/17 net/cnxk: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 804c585658ea ("net/cnxk: add representor control plane")
---
 drivers/net/cnxk/cnxk_eswitch.c | 4 ++--
 drivers/net/cnxk/cnxk_rep_msg.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/net/cnxk/cnxk_eswitch.c +2 -2
index e45c7dfd07..50c3e54109 100644
@@ -124,8 +124,8 @@ cnxk_eswitch_dev_remove(struct rte_pci_device *pci_dev)
 				}
 				sun.sun_family = AF_UNIX;
 				sunlen = sizeof(struct sockaddr_un);
-				strncpy(sun.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH,
-					sizeof(sun.sun_path) - 1);
+				strlcpy(sun.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH,
+					sizeof(sun.sun_path));
 
 				if (connect(sock_fd, (struct sockaddr *)&sun, sunlen) < 0) {
 					plt_err("Failed to connect socket: %s, err %d",
drivers/net/cnxk/cnxk_rep_msg.c +1 -1
index a222e2b5cd..7b92e7a5f7 100644
@@ -138,7 +138,7 @@ open_socket_ctrl_channel(void)
 
 	memset(&un, 0, sizeof(struct sockaddr_un));
 	un.sun_family = AF_UNIX;
-	strncpy(un.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH, sizeof(un.sun_path) - 1);
+	strlcpy(un.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH, sizeof(un.sun_path));
 
 	if (bind(sock_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
 		plt_err("Failed to bind %s: %s", un.sun_path, strerror(errno));
Signature · 1 line
2.53.0
#37 lore raw

PATCH v2 9/17 net/dpaa: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes ee0fa7552a0a ("net/dpaa: forbid MTU configuration for shared interface")
---
 drivers/net/dpaa/dpaa_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/dpaa/dpaa_ethdev.c +1 -1
index 9f976d179b..8b1072f9b9 100644
@@ -247,7 +247,7 @@ dpaa_eth_dev_configure(struct rte_eth_dev *dev)
 			DPAA_PMD_ERR("Cannot open IF socket");
 			return -errno;
 		}
-		strncpy(ifr.ifr_name, dpaa_intf->name, IFNAMSIZ - 1);
+		strlcpy(ifr.ifr_name, dpaa_intf->name, IFNAMSIZ);
 
 		if (ioctl(socket_fd, SIOCGIFMTU, &ifr) < 0) {
 			DPAA_PMD_ERR("Cannot get interface mtu");
Signature · 1 line
2.53.0
#38 lore raw

PATCH v2 10/17 net/ionic: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 5ef518098ec6 ("net/ionic: register and initialize adapter")
---
 drivers/net/ionic/ionic_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ionic/ionic_main.c +2 -2
index 814bb3b8f4..d5545b1ecb 100644
@@ -393,8 +393,8 @@ ionic_identify(struct ionic_adapter *adapter)
 	ident->drv.kernel_ver = 0;
 	snprintf(ident->drv.kernel_ver_str,
 		sizeof(ident->drv.kernel_ver_str), "DPDK");
-	strncpy(ident->drv.driver_ver_str, IONIC_DRV_VERSION,
-		sizeof(ident->drv.driver_ver_str) - 1);
+	strlcpy(ident->drv.driver_ver_str, IONIC_DRV_VERSION,
+		sizeof(ident->drv.driver_ver_str));
 
 	nwords = RTE_MIN(drv_size, cmd_size);
 	for (i = 0; i < nwords; i++)
Signature · 1 line
2.53.0
#39 lore raw

PATCH v2 11/17 net/mlx4: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 61cbdd419478 ("net/mlx4: separate device control functions")
---
 drivers/net/mlx4/mlx4_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/mlx4/mlx4_ethdev.c +1 -1
index efc6ee4577..ff353af224 100644
@@ -127,7 +127,7 @@ mlx4_get_ifname(const struct mlx4_priv *priv, char (*ifname)[IF_NAMESIZE])
 		rte_errno = ENODEV;
 		return -rte_errno;
 	}
-	strncpy(*ifname, match, sizeof(*ifname));
+	strlcpy(*ifname, match, sizeof(*ifname));
 	return 0;
 }
 
Signature · 1 line
2.53.0
#41 lore raw

PATCH v2 12/17 net/mlx5: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes ca1418ce3910 ("common/mlx5: share device context object")
Fixes 9e58a50c059f ("net/mlx5: fix interface name parameter definition")
---
 drivers/net/mlx5/mlx5.c                   | 8 ++++----
 drivers/net/mlx5/windows/mlx5_ethdev_os.c | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/net/mlx5/mlx5.c +4 -4
index 61c26d1206..24fda18710 100644
@@ -1916,10 +1916,10 @@ mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
 	sh->txpp.skew = sh->config.tx_skew;
 	sh->refcnt = 1;
 	sh->max_port = spawn->max_port;
-	strncpy(sh->ibdev_name, mlx5_os_get_ctx_device_name(sh->cdev->ctx),
-		sizeof(sh->ibdev_name) - 1);
-	strncpy(sh->ibdev_path, mlx5_os_get_ctx_device_path(sh->cdev->ctx),
-		sizeof(sh->ibdev_path) - 1);
+	strlcpy(sh->ibdev_name, mlx5_os_get_ctx_device_name(sh->cdev->ctx),
+		sizeof(sh->ibdev_name));
+	strlcpy(sh->ibdev_path, mlx5_os_get_ctx_device_path(sh->cdev->ctx),
+		sizeof(sh->ibdev_path));
 	sh->phdev = mlx5_get_physical_device(sh->cdev);
 	if (!sh->phdev)
 		goto error;
drivers/net/mlx5/windows/mlx5_ethdev_os.c +1 -1
index e24ff367af..ecb8914a65 100644
@@ -67,7 +67,7 @@ mlx5_get_ifname(const struct rte_eth_dev *dev, char ifname[MLX5_NAMESIZE])
 	}
 	priv = dev->data->dev_private;
 	context_obj = (mlx5_context_st *)priv->sh->cdev->ctx;
-	strncpy(ifname, context_obj->mlx5_dev.name, MLX5_NAMESIZE);
+	strlcpy(ifname, context_obj->mlx5_dev.name, MLX5_NAMESIZE);
 	return 0;
 }
 
Signature · 1 line
2.53.0
Acked-by on v2 12/17
#42 lore raw

Re: [PATCH v2 12/17] net/mlx5: replace strncpy with strlcpy

Quoted 7 lines · Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes ca1418ce3910 ("common/mlx5: share device context object")
Fixes 9e58a50c059f ("net/mlx5: fix interface name parameter definition")

I sent the previous ack on v1, sorry about that.

#43 lore raw

PATCH v2 13/17 net/nfp: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes c7e9729da6b5 ("net/nfp: support CPP")
---
 drivers/net/nfp/nfpcore/nfp_resource.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/nfp/nfpcore/nfp_resource.c +1 -1
index 6437a78852..781350c46f 100644
@@ -168,7 +168,7 @@ nfp_resource_acquire(struct nfp_cpp *cpp,
 
 	memset(res, 0, sizeof(*res));
 
-	strncpy(res->name, name, NFP_RESOURCE_ENTRY_NAME_SZ);
+	strlcpy(res->name, name, sizeof(res->name));
 
 	dev_mutex = nfp_cpp_mutex_alloc(cpp, NFP_RESOURCE_TBL_TARGET,
 			NFP_RESOURCE_TBL_BASE, NFP_RESOURCE_TBL_KEY);
Signature · 1 line
2.53.0
#44 lore raw

PATCH v2 14/17 net/qede: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 2ea6f76aff40 ("qede: add core driver")
---
 drivers/net/qede/qede_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/qede/qede_ethdev.c +1 -1
index 4efc2dd349..2f37f956cb 100644
@@ -2596,7 +2596,7 @@ static int qede_common_dev_init(struct rte_eth_dev *eth_dev, bool is_vf)
 	params.drv_minor = QEDE_PMD_VERSION_MINOR;
 	params.drv_rev = QEDE_PMD_VERSION_REVISION;
 	params.drv_eng = QEDE_PMD_VERSION_PATCH;
-	strncpy((char *)params.name, QEDE_PMD_VER_PREFIX,
+	strlcpy((char *)params.name, QEDE_PMD_VER_PREFIX,
 		QEDE_PMD_DRV_VER_STR_SIZE);
 
 	qede_assign_rxtx_handlers(eth_dev, true);
Signature · 1 line
2.53.0
#45 lore raw

PATCH v2 15/17 net/softnic: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 7709a63bf178 ("net/softnic: add connection agent")
---
 drivers/net/softnic/conn.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/softnic/conn.c +4 -2
index 5b031358d5..398f864d16 100644
@@ -15,6 +15,8 @@
 #include <arpa/inet.h>
 #include <errno.h>
 
+#include <rte_string_fns.h>
+
 #include "conn.h"
 
 #define MSG_CMD_TOO_LONG "Command too long."
@@ -116,8 +118,8 @@ softnic_conn_init(struct softnic_conn_params *p)
 	}
 
 	/* Fill in */
-	strncpy(conn->welcome, p->welcome, CONN_WELCOME_LEN_MAX);
-	strncpy(conn->prompt, p->prompt, CONN_PROMPT_LEN_MAX);
+	strlcpy(conn->welcome, p->welcome, CONN_WELCOME_LEN_MAX + 1);
+	strlcpy(conn->prompt, p->prompt, CONN_PROMPT_LEN_MAX + 1);
 	conn->buf_size = p->buf_size;
 	conn->msg_in_len_max = p->msg_in_len_max;
 	conn->msg_out_len_max = p->msg_out_len_max;
Signature · 1 line
2.53.0
#46 lore raw

PATCH v2 16/17 net/vhost: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes 2e16597c4bda ("net/vhost: move to vhost library statistics API")
---
 drivers/net/vhost/rte_eth_vhost.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/vhost/rte_eth_vhost.c +1 -1
index 05940f2461..82e9fc929a 100644
@@ -209,7 +209,7 @@ vhost_dev_xstats_get_names(struct rte_eth_dev *dev,
 	}
 
 	for (i = 0; i < count; i++)
-		strncpy(xstats_names[i].name, name[i].name, RTE_ETH_XSTATS_NAME_SIZE);
+		strlcpy(xstats_names[i].name, name[i].name, RTE_ETH_XSTATS_NAME_SIZE);
 
 	free(name);
 
Signature · 1 line
2.53.0
#47 lore raw

PATCH v2 17/17 net/virtio: replace strncpy with strlcpy

In reply to Bruce Richardson

Replace strncpy() with safer strlcpy() which always null-terminates.

Fixes b72099be7f27 ("net/virtio-user: fix init when using existing tap")
---
 drivers/net/virtio/virtio_user/vhost_kernel_tap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/virtio/virtio_user/vhost_kernel_tap.c +1 -1
index 2431d00c33..da55aba505 100644
@@ -60,7 +60,7 @@ vhost_tap_open(const char *ifname, unsigned int r_flags, bool multi_queue)
 
 retry_mono_q:
 	memset(&ifr, 0, sizeof(ifr));
-	strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
+	strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
 	ifr.ifr_flags = r_flags;
 	if (multi_queue)
 		ifr.ifr_flags |= IFF_MULTI_QUEUE;
Signature · 1 line
2.53.0
#48 lore raw
In reply to Bruce Richardson

Driver maintainers, ping for review please!

Thanks,
/Bruce

Quoted 57 lines · Bruce Richardson

Many uses of strncpy in DPDK drivers can be directly replaced by a call to strlcpy instead, which is safer in that it always null- terminates the string. This AI assisted patchset makes those simple replacements, adjusting lengths as appropriate.

After this set, there are still a number of drivers with strncpy calls in them, but those are not simple strncpy->strlcpy replacements, so left for later rework.

v2:
* fix incorrect commit ids for fixlines.
* fix build errors due to missing rte_string_fns.h header include

Bruce Richardson (17):
common/cnxk: replace strncpy with strlcpy
common/mlx5: replace strncpy with strlcpy
crypto/mvsam: replace strncpy with strlcpy
crypto/scheduler: replace strncpy with strlcpy
event/cnxk: replace strncpy with strlcpy
net/ark: replace strncpy with strlcpy
net/bnx2x: replace strncpy with strlcpy
net/cnxk: replace strncpy with strlcpy
net/dpaa: replace strncpy with strlcpy
net/ionic: replace strncpy with strlcpy
net/mlx4: replace strncpy with strlcpy
net/mlx5: replace strncpy with strlcpy
net/nfp: replace strncpy with strlcpy
net/qede: replace strncpy with strlcpy
net/softnic: replace strncpy with strlcpy
net/vhost: replace strncpy with strlcpy
net/virtio: replace strncpy with strlcpy

 drivers/common/cnxk/roc_model.c                   | 8 ++++----
 drivers/common/mlx5/linux/mlx5_common_os.c        | 2 +-
 drivers/crypto/mvsam/rte_mrvl_pmd.c               | 2 +-
 drivers/crypto/scheduler/scheduler_pmd.c          | 8 ++++----
 drivers/event/cnxk/cnxk_eventdev_stats.c          | 2 +-
 drivers/net/ark/ark_ethdev.c                      | 2 +-
 drivers/net/bnx2x/bnx2x.c                         | 2 +-
 drivers/net/bnx2x/bnx2x_vfpf.c                    | 2 +-
 drivers/net/cnxk/cnxk_eswitch.c                   | 4 ++--
 drivers/net/cnxk/cnxk_rep_msg.c                   | 2 +-
 drivers/net/dpaa/dpaa_ethdev.c                    | 2 +-
 drivers/net/ionic/ionic_main.c                    | 4 ++--
 drivers/net/mlx4/mlx4_ethdev.c                    | 2 +-
 drivers/net/mlx5/mlx5.c                           | 8 ++++----
 drivers/net/mlx5/windows/mlx5_ethdev_os.c         | 2 +-
 drivers/net/nfp/nfpcore/nfp_resource.c            | 2 +-
 drivers/net/qede/qede_ethdev.c                    | 2 +-
 drivers/net/softnic/conn.c                        | 6 ++++--
 drivers/net/vhost/rte_eth_vhost.c                 | 2 +-
 drivers/net/virtio/virtio_user/vhost_kernel_tap.c | 2 +-
 20 files changed, 34 insertions(+), 32 deletions(-)

--
2.53.0

#49 lore raw
In reply to Bruce Richardson
Quoted 46 lines · Bruce Richardson

Many uses of strncpy in DPDK drivers can be directly replaced by a call to strlcpy instead, which is safer in that it always null- terminates the string. This AI assisted patchset makes those simple replacements, adjusting lengths as appropriate.

After this set, there are still a number of drivers with strncpy calls in them, but those are not simple strncpy->strlcpy replacements, so left for later rework.

v2:
* fix incorrect commit ids for fixlines.
* fix build errors due to missing rte_string_fns.h header include

Bruce Richardson (17):
common/cnxk: replace strncpy with strlcpy
common/mlx5: replace strncpy with strlcpy
crypto/mvsam: replace strncpy with strlcpy
crypto/scheduler: replace strncpy with strlcpy
event/cnxk: replace strncpy with strlcpy
net/ark: replace strncpy with strlcpy
net/bnx2x: replace strncpy with strlcpy
net/cnxk: replace strncpy with strlcpy
net/dpaa: replace strncpy with strlcpy
net/ionic: replace strncpy with strlcpy
net/mlx4: replace strncpy with strlcpy
net/mlx5: replace strncpy with strlcpy
net/nfp: replace strncpy with strlcpy
net/qede: replace strncpy with strlcpy
net/softnic: replace strncpy with strlcpy
net/vhost: replace strncpy with strlcpy
net/virtio: replace strncpy with strlcpy

 drivers/common/cnxk/roc_model.c                   | 8 ++++----
 drivers/common/mlx5/linux/mlx5_common_os.c        | 2 +-
 drivers/crypto/mvsam/rte_mrvl_pmd.c               | 2 +-
 drivers/crypto/scheduler/scheduler_pmd.c          | 8 ++++----
 drivers/event/cnxk/cnxk_eventdev_stats.c          | 2 +-
 drivers/net/ark/ark_ethdev.c                      | 2 +-
 drivers/net/bnx2x/bnx2x.c                         | 2 +-
 drivers/net/bnx2x/bnx2x_vfpf.c                    | 2 +-
 drivers/net/cnxk/cnxk_eswitch.c                   | 4 ++--
 drivers/net/cnxk/cnxk_rep_msg.c                   | 2 +-
 drivers/net/dpaa/dpaa_ethdev.c                    | 2 +-
 drivers/net/ionic/ionic_main.c                    | 4 ++--
 drivers/net/mlx4/mlx4_ethdev.c                    | 2 +-
Quoted 8 lines
 drivers/net/mlx5/mlx5.c                           | 8 ++++----
 drivers/net/mlx5/windows/mlx5_ethdev_os.c         | 2 +-
 drivers/net/nfp/nfpcore/nfp_resource.c            | 2 +-
 drivers/net/qede/qede_ethdev.c                    | 2 +-
 drivers/net/softnic/conn.c                        | 6 ++++--
 drivers/net/vhost/rte_eth_vhost.c                 | 2 +-
 drivers/net/virtio/virtio_user/vhost_kernel_tap.c | 2 +-
 20 files changed, 34 insertions(+), 32 deletions(-)

Series applied.

Thanks for the cleanup Bruce.

Even if the transition is not finished, it is probably worth updating checkpatches.sh to avoid new strncpy additions now.

Signature · 1 line
David Marchand
#50 lore raw
Quoted 57 lines · David Marchand
Quoted 46 lines · Bruce Richardson

Many uses of strncpy in DPDK drivers can be directly replaced by a call to strlcpy instead, which is safer in that it always null- terminates the string. This AI assisted patchset makes those simple replacements, adjusting lengths as appropriate.

After this set, there are still a number of drivers with strncpy calls in them, but those are not simple strncpy->strlcpy replacements, so left for later rework.

v2:
* fix incorrect commit ids for fixlines.
* fix build errors due to missing rte_string_fns.h header include

Bruce Richardson (17):
common/cnxk: replace strncpy with strlcpy
common/mlx5: replace strncpy with strlcpy
crypto/mvsam: replace strncpy with strlcpy
crypto/scheduler: replace strncpy with strlcpy
event/cnxk: replace strncpy with strlcpy
net/ark: replace strncpy with strlcpy
net/bnx2x: replace strncpy with strlcpy
net/cnxk: replace strncpy with strlcpy
net/dpaa: replace strncpy with strlcpy
net/ionic: replace strncpy with strlcpy
net/mlx4: replace strncpy with strlcpy
net/mlx5: replace strncpy with strlcpy
net/nfp: replace strncpy with strlcpy
net/qede: replace strncpy with strlcpy
net/softnic: replace strncpy with strlcpy
net/vhost: replace strncpy with strlcpy
net/virtio: replace strncpy with strlcpy

 drivers/common/cnxk/roc_model.c                   | 8 ++++----
 drivers/common/mlx5/linux/mlx5_common_os.c        | 2 +-
 drivers/crypto/mvsam/rte_mrvl_pmd.c               | 2 +-
 drivers/crypto/scheduler/scheduler_pmd.c          | 8 ++++----
 drivers/event/cnxk/cnxk_eventdev_stats.c          | 2 +-
 drivers/net/ark/ark_ethdev.c                      | 2 +-
 drivers/net/bnx2x/bnx2x.c                         | 2 +-
 drivers/net/bnx2x/bnx2x_vfpf.c                    | 2 +-
 drivers/net/cnxk/cnxk_eswitch.c                   | 4 ++--
 drivers/net/cnxk/cnxk_rep_msg.c                   | 2 +-
 drivers/net/dpaa/dpaa_ethdev.c                    | 2 +-
 drivers/net/ionic/ionic_main.c                    | 4 ++--
 drivers/net/mlx4/mlx4_ethdev.c                    | 2 +-
Quoted 8 lines
 drivers/net/mlx5/mlx5.c                           | 8 ++++----
 drivers/net/mlx5/windows/mlx5_ethdev_os.c         | 2 +-
 drivers/net/nfp/nfpcore/nfp_resource.c            | 2 +-
 drivers/net/qede/qede_ethdev.c                    | 2 +-
 drivers/net/softnic/conn.c                        | 6 ++++--
 drivers/net/vhost/rte_eth_vhost.c                 | 2 +-
 drivers/net/virtio/virtio_user/vhost_kernel_tap.c | 2 +-
 20 files changed, 34 insertions(+), 32 deletions(-)
Quoted 7 lines

Series applied.

Thanks for the cleanup Bruce.

Even if the transition is not finished, it is probably worth updating checkpatches.sh to avoid new strncpy additions now.

This should be already covered by kernel checkpatch.pl script we wrap, so I don't believe we need to add anything:

cat /tmp/test_strncpy.patch | devtools/checkpatches.sh -q 2>&1

### [PATCH] test: check strncpy detection

WARNING:COMMIT_MESSAGE: Missing commit description - Add an appropriate one

WARNING:STRNCPY: Prefer strscpy, strscpy_pad, or __nonstring over strncpy - see: https://github.com/KSPP/linux/issues/90
#20: FILE: ...

If we replace the checkpatches.sh script with Stephen's python version, we can probably get a better error message more relevant for userspace.

/Bruce