[v5,22/52] dma/idxd: replace strerror with reentrant version

Message ID 20241104111037.3632161-23-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:10 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/dma/idxd/idxd_bus.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
  

Patch

diff --git a/drivers/dma/idxd/idxd_bus.c b/drivers/dma/idxd/idxd_bus.c
index ba8076715d..78137f3531 100644
--- a/drivers/dma/idxd/idxd_bus.c
+++ b/drivers/dma/idxd/idxd_bus.c
@@ -16,6 +16,7 @@ 
 #include <rte_log.h>
 #include <rte_dmadev_pmd.h>
 #include <rte_string_fns.h>
+#include <rte_errno.h>
 
 #include "idxd_internal.h"
 
@@ -136,6 +137,7 @@  static int
 read_wq_string(struct rte_dsa_device *dev, const char *filename,
 		char *value, size_t valuelen)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	char sysfs_node[PATH_MAX];
 	int len;
 	int fd;
@@ -144,16 +146,20 @@  read_wq_string(struct rte_dsa_device *dev, const char *filename,
 			dsa_get_sysfs_path(), dev->wq_name, filename);
 	fd = open(sysfs_node, O_RDONLY);
 	if (fd < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		IDXD_PMD_ERR("%s(): opening file '%s' failed: %s",
-				__func__, sysfs_node, strerror(errno));
+				__func__, sysfs_node, errmsg);
 		return -1;
 	}
 
 	len = read(fd, value, valuelen - 1);
 	close(fd);
 	if (len < 0) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		IDXD_PMD_ERR("%s(): error reading file '%s': %s",
-				__func__, sysfs_node, strerror(errno));
+				__func__, sysfs_node, errmsg);
 		return -1;
 	}
 	value[len] = '\0';
@@ -164,6 +170,7 @@  static int
 read_wq_int(struct rte_dsa_device *dev, const char *filename,
 		int *value)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	char sysfs_node[PATH_MAX];
 	FILE *f;
 	int ret = 0;
@@ -172,14 +179,18 @@  read_wq_int(struct rte_dsa_device *dev, const char *filename,
 			dsa_get_sysfs_path(), dev->wq_name, filename);
 	f = fopen(sysfs_node, "r");
 	if (f == NULL) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		IDXD_PMD_ERR("%s(): opening file '%s' failed: %s",
-				__func__, sysfs_node, strerror(errno));
+				__func__, sysfs_node, errmsg);
 		return -1;
 	}
 
 	if (fscanf(f, "%d", value) != 1) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		IDXD_PMD_ERR("%s(): error reading file '%s': %s",
-				__func__, sysfs_node, strerror(errno));
+				__func__, sysfs_node, errmsg);
 		ret = -1;
 	}
 
@@ -191,6 +202,7 @@  static int
 read_device_int(struct rte_dsa_device *dev, const char *filename,
 		int *value)
 {
+	char errmsg[RTE_STRERR_BUFSIZE];
 	char sysfs_node[PATH_MAX];
 	FILE *f;
 	int ret = 0;
@@ -199,14 +211,18 @@  read_device_int(struct rte_dsa_device *dev, const char *filename,
 			dsa_get_sysfs_path(), dev->addr.device_id, filename);
 	f = fopen(sysfs_node, "r");
 	if (f == NULL) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		IDXD_PMD_ERR("%s(): opening file '%s' failed: %s",
-				__func__, sysfs_node, strerror(errno));
+				__func__, sysfs_node, errmsg);
 		return -1;
 	}
 
 	if (fscanf(f, "%d", value) != 1) {
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		IDXD_PMD_ERR("%s(): error reading file '%s': %s",
-				__func__, sysfs_node, strerror(errno));
+				__func__, sysfs_node, errmsg);
 		ret = -1;
 	}
 
@@ -315,6 +331,7 @@  static int
 dsa_scan(void)
 {
 	const char *path = dsa_get_dev_path();
+	char errmsg[RTE_STRERR_BUFSIZE];
 	struct dirent *wq;
 	DIR *dev_dir;
 
@@ -322,8 +339,10 @@  dsa_scan(void)
 	if (dev_dir == NULL) {
 		if (errno == ENOENT)
 			return 0; /* no bus, return without error */
+		if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno);
 		IDXD_PMD_ERR("%s(): opendir '%s' failed: %s",
-				__func__, path, strerror(errno));
+				__func__, path, errmsg);
 		return -1;
 	}