From 9bae90546e50aa317cc6676a1968e1a6ccae5ab3 Mon Sep 17 00:00:00 2001 From: Thijs Paelman Date: Thu, 28 Dec 2023 09:01:45 +0100 Subject: 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 Signed-off-by: Sander Vrijders --- src/irmd/configfile.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src') 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; -- cgit v1.2.3