diff options
author | Karel Zak | 2016-10-25 13:41:10 +0200 |
---|---|---|
committer | Karel Zak | 2016-10-25 13:42:48 +0200 |
commit | 8430b9b88426eb3c273b02a2d9505d839913317c (patch) | |
tree | 71411e2e41f430af949fc5abe417395c8698266d /libfdisk | |
parent | tests: add sfdisk Type= and Id= test (diff) | |
download | kernel-qcow2-util-linux-8430b9b88426eb3c273b02a2d9505d839913317c.tar.gz kernel-qcow2-util-linux-8430b9b88426eb3c273b02a2d9505d839913317c.tar.xz kernel-qcow2-util-linux-8430b9b88426eb3c273b02a2d9505d839913317c.zip |
libfdisk: make script token parser more robust
* 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>
Diffstat (limited to 'libfdisk')
-rw-r--r-- | libfdisk/src/script.c | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/libfdisk/src/script.c b/libfdisk/src/script.c index 4c1f9c7b9..54621ae28 100644 --- a/libfdisk/src/script.c +++ b/libfdisk/src/script.c @@ -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; |