diff options
author | Thijs Paelman <[email protected]> | 2023-12-28 09:01:45 +0100 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2023-12-31 16:48:02 +0100 |
commit | 9bae90546e50aa317cc6676a1968e1a6ccae5ab3 (patch) | |
tree | bfbbb2514f3fd7d7e52305769d5a3be2ada11d17 | |
parent | 863553891b296c5574d6b0893ad21fe16b97a6ea (diff) | |
download | ouroboros-9bae90546e50aa317cc6676a1968e1a6ccae5ab3.tar.gz ouroboros-9bae90546e50aa317cc6676a1968e1a6ccae5ab3.zip |
irmd: Fix parsing multiple args in configfile
New method retains the original string in parsing the args string into
an argv dynamic array. Previous method (`strtok`) didn't work,
because it is a destructive function, changing the supplied string. We
however needed to apply it twice to the same string. It is still done
twice in a loop, to make sure argc is exact. Other methods, like
counting the amount of spaces to determine argc, would be incorrect
for his particular way of tokenizing if arguments are separated by
e.g. two spaces.
Also fixes a wrong pointer dereference, which did go unnoticed before
due to the previous error.
Signed-off-by: Thijs Paelman <[email protected]>
Signed-off-by: Sander Vrijders <[email protected]>
-rw-r--r-- | src/irmd/configfile.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/irmd/configfile.c b/src/irmd/configfile.c index af2a37c8..61e828d8 100644 --- a/src/irmd/configfile.c +++ b/src/irmd/configfile.c @@ -579,9 +579,9 @@ static int args_to_argv(const char * args, str = (char *) args; - tok = strtok(str, " "); - while (tok != NULL) { - tok = strtok(NULL, " "); + tok = str; + while (*(tok += strspn(tok, " ")) != '\0') { + tok += strcspn(tok, " "); argc++; } @@ -590,14 +590,16 @@ static int args_to_argv(const char * args, goto fail_malloc; argc = 0; - tok = strtok(str, " "); - while (tok != NULL) { - (*argv)[argc] = malloc((strlen(tok) + 1) * sizeof(***argv)); - if (*argv[argc] == NULL) + tok = str; + while (*(tok += strspn(tok, " ")) != '\0') { + size_t toklen = strcspn(tok, " "); + (*argv)[argc] = malloc((toklen + 1) * sizeof(***argv)); + if ((*argv)[argc] == NULL) goto fail_malloc2; - strcpy((*argv)[argc++], tok); - tok = strtok(NULL, " "); + strncpy((*argv)[argc], tok, toklen); + (*argv)[argc++][toklen] = '\0'; + tok += toklen; } (*argv)[argc] = NULL; |