Skip to content

Commit

Permalink
app/graph: replace strtok with reentrant version
Browse files Browse the repository at this point in the history
The function strtok is not thread safe, better to use strtok_r.
This patch was found by running semgrep on the DPDK repository.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
  • Loading branch information
shemminger authored and tmonjalo committed Nov 19, 2024
1 parent 0787cdb commit 9236e5b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/graph/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ parser_usecases_read(char *usecases)
{
bool valid = false;
uint32_t i, j = 0;
char *token;
char *token, *saveptr = NULL;

token = strtok(usecases, ",");
token = strtok_r(usecases, ",", &saveptr);
while (token != NULL) {
for (i = 0; i < RTE_DIM(supported_usecases); i++) {
if (strcmp(supported_usecases[i], token) == 0) {
Expand All @@ -116,7 +116,7 @@ parser_usecases_read(char *usecases)
break;
}
}
token = strtok(NULL, ",");
token = strtok_r(NULL, ",", &saveptr);
}

return valid;
Expand Down
18 changes: 9 additions & 9 deletions app/graph/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ parser_ip4_read(uint32_t *value, char *p)
{
uint8_t shift = 24;
uint32_t ip = 0;
char *token;
char *token, *saveptr = NULL;

token = strtok(p, ".");
token = strtok_r(p, ".", &saveptr);
while (token != NULL) {
ip |= (((uint32_t)strtoul(token, NULL, 10)) << shift);
token = strtok(NULL, ".");
token = strtok_r(NULL, ".", &saveptr);
shift -= 8;
}

Expand All @@ -113,13 +113,13 @@ int
parser_ip6_read(uint8_t *value, char *p)
{
uint64_t val = 0;
char *token;
char *token, *saveptr = NULL;

token = strtok(p, ":");
token = strtok_r(p, ":", &saveptr);
while (token != NULL) {
hex_string_to_uint64(&val, token);
*value = val;
token = strtok(NULL, ":");
token = strtok_r(NULL, ":", &saveptr);
value++;
val = 0;
}
Expand All @@ -132,13 +132,13 @@ parser_mac_read(uint64_t *value, char *p)
{
uint64_t mac = 0, val = 0;
uint8_t shift = 40;
char *token;
char *token, *saveptr = NULL;

token = strtok(p, ":");
token = strtok_r(p, ":", &saveptr);
while (token != NULL) {
hex_string_to_uint64(&val, token);
mac |= val << shift;
token = strtok(NULL, ":");
token = strtok_r(NULL, ":", &saveptr);
shift -= 8;
val = 0;
}
Expand Down

0 comments on commit 9236e5b

Please sign in to comment.