[v5,14/52] bus/fslmc: replace strerror with reentrant version

Message ID 20241104111037.3632161-15-huangdengdui@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series replace strerror |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

huangdengdui Nov. 4, 2024, 11:09 a.m. UTC
The function strerror() is insecure in a multi-thread environment.
This patch uses strerror_r() to replace it.

Signed-off-by: Dengdui Huang <huangdengdui@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Huisong Li <lihuisong@huawei.com>
---
 drivers/bus/fslmc/fslmc_vfio.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
  

Patch

diff --git a/drivers/bus/fslmc/fslmc_vfio.c b/drivers/bus/fslmc/fslmc_vfio.c
index 6981679a2d..b6647dc38d 100644
--- a/drivers/bus/fslmc/fslmc_vfio.c
+++ b/drivers/bus/fslmc/fslmc_vfio.c
@@ -448,6 +448,7 @@  fslmc_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 			.argsz = sizeof(group_status)
 	};
 	int vfio_group_fd, vfio_container_fd, iommu_group_no, ret;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	/* get group number */
 	ret = rte_vfio_get_group_num(sysfs_base, dev_addr, &iommu_group_no);
@@ -479,9 +480,11 @@  fslmc_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 	/* check if the group is viable */
 	ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
 	if (ret) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		DPAA2_BUS_ERR("  %s cannot get group status, "
 				"error %i (%s)", dev_addr,
-				errno, strerror(errno));
+				errno, errmsg);
 		close(vfio_group_fd);
 		rte_vfio_clear_group(vfio_group_fd);
 		return -1;
@@ -502,9 +505,11 @@  fslmc_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 		ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
 				&vfio_container_fd);
 		if (ret) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			DPAA2_BUS_ERR("  %s cannot add VFIO group to container, "
 					"error %i (%s)", dev_addr,
-					errno, strerror(errno));
+					errno, errmsg);
 			close(vfio_group_fd);
 			close(vfio_container_fd);
 			rte_vfio_clear_group(vfio_group_fd);
@@ -550,8 +555,10 @@  fslmc_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
 	/* test and setup the device */
 	ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
 	if (ret) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		DPAA2_BUS_ERR("  %s cannot get device info, error %i (%s)",
-				dev_addr, errno, strerror(errno));
+				dev_addr, errno, errmsg);
 		close(*vfio_dev_fd);
 		close(vfio_group_fd);
 		close(vfio_container_fd);
@@ -599,6 +606,7 @@  int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle, int index)
 	char irq_set_buf[IRQ_SET_BUF_LEN];
 	struct vfio_irq_set *irq_set;
 	int *fd_ptr, vfio_dev_fd;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	len = sizeof(irq_set_buf);
 
@@ -615,9 +623,10 @@  int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle, int index)
 	vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
 	ret = ioctl(vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
 	if (ret) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		DPAA2_BUS_ERR("Error:dpaa2 SET IRQs fd=%d, err = %d(%s)",
-			      rte_intr_fd_get(intr_handle), errno,
-			      strerror(errno));
+			      rte_intr_fd_get(intr_handle), errno, errmsg);
 		return ret;
 	}
 
@@ -655,6 +664,7 @@  rte_dpaa2_vfio_setup_intr(struct rte_intr_handle *intr_handle,
 			  int vfio_dev_fd,
 			  int num_irqs)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int i, ret;
 
 	/* start from MSI-X interrupt type */
@@ -666,8 +676,10 @@  rte_dpaa2_vfio_setup_intr(struct rte_intr_handle *intr_handle,
 
 		ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
 		if (ret < 0) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			DPAA2_BUS_ERR("Cannot get IRQ(%d) info, error %i (%s)",
-				      i, errno, strerror(errno));
+				      i, errno, errmsg);
 			return -1;
 		}
 
@@ -681,8 +693,10 @@  rte_dpaa2_vfio_setup_intr(struct rte_intr_handle *intr_handle,
 		/* set up an eventfd for interrupts */
 		fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
 		if (fd < 0) {
+			if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 			DPAA2_BUS_ERR("Cannot set up eventfd, error %i (%s)",
-				      errno, strerror(errno));
+				      errno, errmsg);
 			return -1;
 		}