[v5,11/52] bpf: replace strerror with reentrant version

Message ID 20241104111037.3632161-12-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>
---
 lib/bpf/bpf_load_elf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
  

Patch

diff --git a/lib/bpf/bpf_load_elf.c b/lib/bpf/bpf_load_elf.c
index e0abd3c856..569a9f4446 100644
--- a/lib/bpf/bpf_load_elf.c
+++ b/lib/bpf/bpf_load_elf.c
@@ -300,6 +300,7 @@  rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
 {
 	int32_t fd, rc;
 	struct rte_bpf *bpf;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (prm == NULL || fname == NULL || sname == NULL) {
 		rte_errno = EINVAL;
@@ -309,8 +310,10 @@  rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
 	fd = open(fname, O_RDONLY);
 	if (fd < 0) {
 		rc = errno;
+		if (strerror_r(rc, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", rc);
 		RTE_BPF_LOG_LINE(ERR, "%s(%s) error code: %d(%s)",
-			__func__, fname, rc, strerror(rc));
+			__func__, fname, rc, errmsg);
 		rte_errno = EINVAL;
 		return NULL;
 	}