Skip to content

Commit 83b5f16

Browse files
committed
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.
1 parent e51e29e commit 83b5f16

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/config_parse.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -404,22 +404,21 @@ static int parse_name(
404404
static int parse_variable(git_config_parser *reader, char **var_name, char **var_value)
405405
{
406406
const char *value_start = NULL;
407-
char *line;
408-
int quote_count;
407+
char *line = NULL, *name = NULL, *value = NULL;
408+
int quote_count, error;
409409
bool multiline;
410410

411+
*var_name = NULL;
412+
*var_value = NULL;
413+
411414
git_parse_advance_ws(&reader->ctx);
412415
line = git__strndup(reader->ctx.line, reader->ctx.line_len);
413-
if (line == NULL)
414-
return -1;
416+
GITERR_CHECK_ALLOC(line);
415417

416418
quote_count = strip_comments(line, 0);
417419

418-
/* If there is no value, boolean true is assumed */
419-
*var_value = NULL;
420-
421-
if (parse_name(var_name, &value_start, reader, line) < 0)
422-
goto on_error;
420+
if ((error = parse_name(&name, &value_start, reader, line)) < 0)
421+
goto out;
423422

424423
/*
425424
* 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
428427
while (git__isspace(value_start[0]))
429428
value_start++;
430429

431-
if (unescape_line(var_value, &multiline, value_start, 0) < 0)
432-
goto on_error;
430+
if ((error = unescape_line(&value, &multiline, value_start, 0)) < 0)
431+
goto out;
433432

434433
if (multiline) {
435434
git_buf multi_value = GIT_BUF_INIT;
436-
git_buf_attach(&multi_value, *var_value, 0);
435+
git_buf_attach(&multi_value, value, 0);
437436

438437
if (parse_multiline_variable(reader, &multi_value, quote_count) < 0 ||
439-
git_buf_oom(&multi_value)) {
438+
git_buf_oom(&multi_value)) {
439+
error = -1;
440440
git_buf_dispose(&multi_value);
441-
goto on_error;
441+
goto out;
442442
}
443443

444-
*var_value = git_buf_detach(&multi_value);
444+
value = git_buf_detach(&multi_value);
445445
}
446446
}
447447

448-
git__free(line);
449-
return 0;
448+
*var_name = name;
449+
*var_value = value;
450+
name = NULL;
451+
value = NULL;
450452

451-
on_error:
452-
git__free(*var_name);
453+
out:
454+
git__free(name);
455+
git__free(value);
453456
git__free(line);
454-
return -1;
457+
return error;
455458
}
456459

457460
int git_config_parse(

0 commit comments

Comments
 (0)