Commit 83b5f161 by Patrick Steinhardt

config_parse: always sanitize out-parameters in `parse_variable`

The `parse_variable` function has two out parameters `var_name` and
`var_value`. Currently, those are not being sanitized to `NULL`. when.
any error happens inside of the `parse_variable` function. Fix that.
While at it, the coding style is improved to match our usual coding
practices more closely.
parent e51e29e8
...@@ -404,22 +404,21 @@ static int parse_name( ...@@ -404,22 +404,21 @@ static int parse_name(
static int parse_variable(git_config_parser *reader, char **var_name, char **var_value) static int parse_variable(git_config_parser *reader, char **var_name, char **var_value)
{ {
const char *value_start = NULL; const char *value_start = NULL;
char *line; char *line = NULL, *name = NULL, *value = NULL;
int quote_count; int quote_count, error;
bool multiline; bool multiline;
*var_name = NULL;
*var_value = NULL;
git_parse_advance_ws(&reader->ctx); git_parse_advance_ws(&reader->ctx);
line = git__strndup(reader->ctx.line, reader->ctx.line_len); line = git__strndup(reader->ctx.line, reader->ctx.line_len);
if (line == NULL) GITERR_CHECK_ALLOC(line);
return -1;
quote_count = strip_comments(line, 0); quote_count = strip_comments(line, 0);
/* If there is no value, boolean true is assumed */ if ((error = parse_name(&name, &value_start, reader, line)) < 0)
*var_value = NULL; goto out;
if (parse_name(var_name, &value_start, reader, line) < 0)
goto on_error;
/* /*
* Now, let's try to parse the value * Now, let's try to parse the value
...@@ -428,30 +427,34 @@ static int parse_variable(git_config_parser *reader, char **var_name, char **var ...@@ -428,30 +427,34 @@ static int parse_variable(git_config_parser *reader, char **var_name, char **var
while (git__isspace(value_start[0])) while (git__isspace(value_start[0]))
value_start++; value_start++;
if (unescape_line(var_value, &multiline, value_start, 0) < 0) if ((error = unescape_line(&value, &multiline, value_start, 0)) < 0)
goto on_error; goto out;
if (multiline) { if (multiline) {
git_buf multi_value = GIT_BUF_INIT; git_buf multi_value = GIT_BUF_INIT;
git_buf_attach(&multi_value, *var_value, 0); git_buf_attach(&multi_value, value, 0);
if (parse_multiline_variable(reader, &multi_value, quote_count) < 0 || if (parse_multiline_variable(reader, &multi_value, quote_count) < 0 ||
git_buf_oom(&multi_value)) { git_buf_oom(&multi_value)) {
error = -1;
git_buf_dispose(&multi_value); git_buf_dispose(&multi_value);
goto on_error; goto out;
} }
*var_value = git_buf_detach(&multi_value); value = git_buf_detach(&multi_value);
} }
} }
git__free(line); *var_name = name;
return 0; *var_value = value;
name = NULL;
value = NULL;
on_error: out:
git__free(*var_name); git__free(name);
git__free(value);
git__free(line); git__free(line);
return -1; return error;
} }
int git_config_parse( int git_config_parse(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment