]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: make script token parser more robust
authorKarel Zak <kzak@redhat.com>
Tue, 25 Oct 2016 11:41:10 +0000 (13:41 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 25 Oct 2016 11:42:48 +0000 (13:42 +0200)
* make sure token is terminated
* skip closing quotes
* allow extra space after quotes and before terminater
* skip extra space after terminater

Addresses: https://github.com/karelzak/util-linux/issues/367
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/script.c

index 4c1f9c7b968a6970a94e1ec5ee8da0fa85242920..54621ae2895cac3e4b8e4fec54cad76f8fed2b16 100644 (file)
@@ -736,7 +736,7 @@ static char *next_token(char **str)
             *tk_end = NULL,
             *end = NULL,
             *p;
-       int open_quote = 0;
+       int open_quote = 0, terminated = 0;
 
        for (p = *str; p && *p; p++) {
                if (!tk_begin) {
@@ -758,9 +758,35 @@ static char *next_token(char **str)
 
        if (!tk_end)
                return NULL;
-       end = isblank(*tk_end) ? (char *) skip_blank(tk_end) : tk_end;
-       if (*end == ',' || *end == ';')
+
+       end = tk_end;
+
+       /* skip closing quotes */
+       if (*end == '"')
+               end++;
+
+       /* token is terminated by blank (or blank is before "," or ";") */
+       if (isblank(*end)) {
+               end = (char *) skip_blank(end);
+               terminated++;
+       }
+
+       /* token is terminated by "," or ";" */
+       if (*end == ',' || *end == ';') {
                end++;
+               terminated++;
+
+       /* token is terminated by \0 */
+       } else if (!*end)
+               terminated++;
+
+       if (!terminated) {
+               DBG(SCRIPT, ul_debug("unterminated token '%s'", end));
+               return NULL;
+       }
+
+       /* skip extra space after terminator */
+       end = (char *) skip_blank(end);
 
        *tk_end = '\0';
        *str = end;