Skip to content

Commit b121b7a

Browse files
authored
Merge pull request libgit2#4411 from pks-t/pks/config-parse-cleanups
Config parser cleanups
2 parents e212011 + e1e90dc commit b121b7a

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

src/config_file.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -876,10 +876,8 @@ static char *escape_value(const char *ptr)
876876
ptr++;
877877
}
878878

879-
if (git_buf_oom(&buf)) {
880-
git_buf_dispose(&buf);
879+
if (git_buf_oom(&buf))
881880
return NULL;
882-
}
883881

884882
return git_buf_detach(&buf);
885883
}
@@ -1022,33 +1020,33 @@ static int parse_conditional_include(git_config_parser *reader,
10221020
static int read_on_variable(
10231021
git_config_parser *reader,
10241022
const char *current_section,
1025-
char *var_name,
1026-
char *var_value,
1023+
const char *var_name,
1024+
const char *var_value,
10271025
const char *line,
10281026
size_t line_len,
10291027
void *data)
10301028
{
10311029
diskfile_parse_state *parse_data = (diskfile_parse_state *)data;
10321030
git_buf buf = GIT_BUF_INIT;
10331031
git_config_entry *entry;
1032+
const char *c;
10341033
int result = 0;
10351034

10361035
GIT_UNUSED(line);
10371036
GIT_UNUSED(line_len);
10381037

1039-
git__strtolower(var_name);
1040-
git_buf_printf(&buf, "%s.%s", current_section, var_name);
1041-
git__free(var_name);
1038+
git_buf_puts(&buf, current_section);
1039+
git_buf_putc(&buf, '.');
1040+
for (c = var_name; *c; c++)
1041+
git_buf_putc(&buf, git__tolower(*c));
10421042

1043-
if (git_buf_oom(&buf)) {
1044-
git__free(var_value);
1043+
if (git_buf_oom(&buf))
10451044
return -1;
1046-
}
10471045

10481046
entry = git__calloc(1, sizeof(git_config_entry));
10491047
GITERR_CHECK_ALLOC(entry);
10501048
entry->name = git_buf_detach(&buf);
1051-
entry->value = var_value;
1049+
entry->value = var_value ? git__strdup(var_value) : NULL;
10521050
entry->level = parse_data->level;
10531051
entry->include_depth = parse_data->depth;
10541052

@@ -1065,7 +1063,6 @@ static int read_on_variable(
10651063
result = parse_conditional_include(reader, parse_data,
10661064
entry->name, entry->value);
10671065

1068-
10691066
return result;
10701067
}
10711068

@@ -1249,8 +1246,8 @@ static int write_on_section(
12491246
static int write_on_variable(
12501247
git_config_parser *reader,
12511248
const char *current_section,
1252-
char *var_name,
1253-
char *var_value,
1249+
const char *var_name,
1250+
const char *var_value,
12541251
const char *line,
12551252
size_t line_len,
12561253
void *data)
@@ -1279,9 +1276,6 @@ static int write_on_variable(
12791276
if (has_matched && write_data->preg != NULL)
12801277
has_matched = (regexec(write_data->preg, var_value, 0, NULL, 0) == 0);
12811278

1282-
git__free(var_name);
1283-
git__free(var_value);
1284-
12851279
/* If this isn't the name/value we're looking for, simply dump the
12861280
* existing data back out and continue on.
12871281
*/

src/config_parse.c

Lines changed: 27 additions & 21 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(
@@ -463,7 +466,7 @@ int git_config_parse(
463466
void *data)
464467
{
465468
git_parse_ctx *ctx;
466-
char *current_section = NULL, *var_name, *var_value;
469+
char *current_section = NULL, *var_name = NULL, *var_value = NULL;
467470
int result = 0;
468471

469472
ctx = &parser->ctx;
@@ -508,7 +511,10 @@ int git_config_parse(
508511
default: /* assume variable declaration */
509512
if ((result = parse_variable(parser, &var_name, &var_value)) == 0 && on_variable) {
510513
result = on_variable(parser, current_section, var_name, var_value, line_start, line_len, data);
514+
git__free(var_name);
515+
git__free(var_value);
511516
}
517+
512518
break;
513519
}
514520

src/config_parse.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ typedef int (*git_config_parser_section_cb)(
3636
typedef int (*git_config_parser_variable_cb)(
3737
git_config_parser *parser,
3838
const char *current_section,
39-
char *var_name,
40-
char *var_value,
39+
const char *var_name,
40+
const char *var_value,
4141
const char *line,
4242
size_t line_len,
4343
void *data);

0 commit comments

Comments
 (0)