[PATCH 14/21] lib/telemetry: replace strtok with strtok_r

Jie Hai haijie1 at huawei.com
Mon Nov 13 11:45:43 CET 2023


Multiple threads calling the same function may cause condition
race issues, which often leads to abnormal behavior and can cause
more serious vulnerabilities such as abnormal termination, denial
of service, and compromised data integrity.

The strtok() is non-reentrant, it is better to replace it with a
reentrant function.

Signed-off-by: Jie Hai <haijie1 at huawei.com>
---
 lib/telemetry/telemetry.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c
index 92982842a860..bb028d9381f7 100644
--- a/lib/telemetry/telemetry.c
+++ b/lib/telemetry/telemetry.c
@@ -371,6 +371,7 @@ static void *
 client_handler(void *sock_id)
 {
 	int s = (int)(uintptr_t)sock_id;
+	char *sp = NULL;
 	char buffer[1024];
 	char info_str[1024];
 	snprintf(info_str, sizeof(info_str),
@@ -385,8 +386,8 @@ client_handler(void *sock_id)
 	int bytes = read(s, buffer, sizeof(buffer) - 1);
 	while (bytes > 0) {
 		buffer[bytes] = 0;
-		const char *cmd = strtok(buffer, ",");
-		const char *param = strtok(NULL, "\0");
+		const char *cmd = strtok_r(buffer, ",", &sp);
+		const char *param = strtok_r(NULL, "\0", &sp);
 		telemetry_cb fn = unknown_command;
 		int i;
 
-- 
2.30.0



More information about the dev mailing list