tokenize: free content of argv on failure, and reset the argv pointer

This fixes a memory leak when we failed to parse a command line (for
example, because it contained an unclosed quote).
This commit is contained in:
Daniel Eklöf 2021-12-25 23:25:30 +01:00
parent ba851c963e
commit e225be1c50
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

View file

@ -49,9 +49,7 @@ tokenize_cmdline(const char *cmdline, char ***argv)
if (end == NULL) {
if (delim != ' ') {
LOG_ERR("unterminated %s quote", delim == '"' ? "double" : "single");
free(*argv);
*argv = NULL;
return false;
goto err;
}
if (!push_argv(argv, &argv_size, p, final_end - p, &idx) ||
@ -97,6 +95,9 @@ tokenize_cmdline(const char *cmdline, char ***argv)
return true;
err:
for (size_t i = 0; i < idx; i++)
free((*argv)[i]);
free(*argv);
*argv = NULL;
return false;
}