From 4e4d995df7d01a0f431c604396b24ffb6026e70c Mon Sep 17 00:00:00 2001 From: Gleb Chesnokov Date: Wed, 21 Oct 2026 14:25:38 +0300 Subject: [PATCH] iscsi-scstd: Keep portal tokenizer state local Use a separate strtok_r saveptr for each listen-address, internal-portal validation and portal-match operation. Preserve delimiters, limits and buffer ownership. No tokenizer race was reproduced; this change makes the parser state explicit and local to each operation. Cppcheck reports strtokCalled at these tokenization sites: Non reentrant function 'strtok' called. For threadsafe applications it is recommended to use the reentrant replacement function 'strtok_r'. --- iscsi-scst/usr/event.c | 6 +++--- iscsi-scst/usr/iscsi_scstd.c | 6 +++--- iscsi-scst/usr/iscsid.c | 5 +++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/iscsi-scst/usr/event.c b/iscsi-scst/usr/event.c index 743781cf4..5808635d8 100644 --- a/iscsi-scst/usr/event.c +++ b/iscsi-scst/usr/event.c @@ -1131,7 +1131,7 @@ static int handle_e_set_attr_value(int fd, const struct iscsi_kern_event *event) /* We have been provided with a string, check the contents. */ if (strchr(p, ' ')) { char *portals = strdup(p); - char *portal; + char *portal, *saveptr; if (!portals) { log_error("Memory error (internal_portal)"); @@ -1139,7 +1139,7 @@ static int handle_e_set_attr_value(int fd, const struct iscsi_kern_event *event) goto out_free; } - portal = strtok(portals, " "); + portal = strtok_r(portals, " ", &saveptr); while (portal != NULL) { if (!is_addr(portal)) { log_error("Invalid address supplied %s", portal); @@ -1147,7 +1147,7 @@ static int handle_e_set_attr_value(int fd, const struct iscsi_kern_event *event) res = -EINVAL; goto out_free; } - portal = strtok(NULL, " "); + portal = strtok_r(NULL, " ", &saveptr); } free(portals); } else { diff --git a/iscsi-scst/usr/iscsi_scstd.c b/iscsi-scst/usr/iscsi_scstd.c index bdb1382b9..d89e2a559 100644 --- a/iscsi-scst/usr/iscsi_scstd.c +++ b/iscsi-scst/usr/iscsi_scstd.c @@ -910,7 +910,7 @@ int main(int argc, char **argv) gid = strtoul(optarg, NULL, 0); break; case 'a': { - char *server_address, *token; + char *server_address, *token, *saveptr; int i = 0; server_address = strdup(optarg); @@ -919,14 +919,14 @@ int main(int argc, char **argv) exit(-1); } - token = strtok(server_address, " "); + token = strtok_r(server_address, " ", &saveptr); while ((i < ADDR_MAX) && token) { log_debug(0, "Address to listen: %s\n", token); server_addresses[i] = token; i++; - token = strtok(NULL, " "); + token = strtok_r(NULL, " ", &saveptr); } break; diff --git a/iscsi-scst/usr/iscsid.c b/iscsi-scst/usr/iscsid.c index a8dd41ea7..747509b0b 100644 --- a/iscsi-scst/usr/iscsid.c +++ b/iscsi-scst/usr/iscsid.c @@ -1029,14 +1029,15 @@ static bool on_internal_portal(struct connection *conn) char *portals = strdup(internal_portal); if (portals) { - char *portal = strtok(portals, " "); + char *saveptr; + char *portal = strtok_r(portals, " ", &saveptr); while (portal != NULL) { if (!strcmp(portal, conn->target_portal)) { free(portals); return true; } - portal = strtok(NULL, " "); + portal = strtok_r(NULL, " ", &saveptr); } free(portals); }