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>
This commit is contained in:
Karel Zak 2016-10-25 13:41:10 +02:00
parent 0dcfebf527
commit 8430b9b884
1 changed files with 29 additions and 3 deletions

View 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,10 +758,36 @@ 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;
return tk_begin;