Commit 9db04160 by Vicent Martí

Merge pull request #222 from carlosmn/config-bugfix

Config bugfix
parents 50b7334e fc0ee5bd
...@@ -261,7 +261,6 @@ static int config_open(git_config_file *cfg) ...@@ -261,7 +261,6 @@ static int config_open(git_config_file *cfg)
cleanup: cleanup:
cvar_list_free(&b->var_list); cvar_list_free(&b->var_list);
gitfo_free_buf(&b->reader.buffer); gitfo_free_buf(&b->reader.buffer);
free(cfg);
return git__rethrow(error, "Failed to open config"); return git__rethrow(error, "Failed to open config");
} }
...@@ -484,15 +483,6 @@ static int cfg_peek(diskfile_backend *cfg, int flags) ...@@ -484,15 +483,6 @@ static int cfg_peek(diskfile_backend *cfg, int flags)
return ret; return ret;
} }
static const char *LINEBREAK_UNIX = "\\\n";
static const char *LINEBREAK_WIN32 = "\\\r\n";
static int is_linebreak(const char *pos)
{
return memcmp(pos - 1, LINEBREAK_UNIX, sizeof(LINEBREAK_UNIX)) == 0 ||
memcmp(pos - 2, LINEBREAK_WIN32, sizeof(LINEBREAK_WIN32)) == 0;
}
/* /*
* Read and consume a line, returning it in newly-allocated memory. * Read and consume a line, returning it in newly-allocated memory.
*/ */
...@@ -503,38 +493,24 @@ static char *cfg_readline(diskfile_backend *cfg) ...@@ -503,38 +493,24 @@ static char *cfg_readline(diskfile_backend *cfg)
int line_len; int line_len;
line_src = cfg->reader.read_ptr; line_src = cfg->reader.read_ptr;
/* Skip empty empty lines */
while (isspace(*line_src))
++line_src;
line_end = strchr(line_src, '\n'); line_end = strchr(line_src, '\n');
/* no newline at EOF */ /* no newline at EOF */
if (line_end == NULL) if (line_end == NULL)
line_end = strchr(line_src, 0); line_end = strchr(line_src, 0);
else
while (is_linebreak(line_end))
line_end = strchr(line_end + 1, '\n');
while (line_src < line_end && isspace(*line_src)) line_len = line_end - line_src;
line_src++;
line = (char *)git__malloc((size_t)(line_end - line_src) + 1); line = git__malloc(line_len + 1);
if (line == NULL) if (line == NULL)
return NULL; return NULL;
line_len = 0; memcpy(line, line_src, line_len);
while (line_src < line_end) {
if (memcmp(line_src, LINEBREAK_UNIX, sizeof(LINEBREAK_UNIX)) == 0) {
line_src += sizeof(LINEBREAK_UNIX);
continue;
}
if (memcmp(line_src, LINEBREAK_WIN32, sizeof(LINEBREAK_WIN32)) == 0) {
line_src += sizeof(LINEBREAK_WIN32);
continue;
}
line[line_len++] = *line_src++;
}
line[line_len] = '\0'; line[line_len] = '\0';
...@@ -619,10 +595,15 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha ...@@ -619,10 +595,15 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
* added to the string. In case of error, jump to out * added to the string. In case of error, jump to out
*/ */
do { do {
if (quote_marks == 2) {
error = git__throw(GIT_EOBJCORRUPTED, "Falied to parse ext header. Text after closing quote");
goto out;
}
switch (c) { switch (c) {
case '"': case '"':
if (quote_marks++ >= 2) ++quote_marks;
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse ext header. Too many quotes");
break; break;
case '\\': case '\\':
c = line[rpos++]; c = line[rpos++];
...@@ -634,6 +615,7 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha ...@@ -634,6 +615,7 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse ext header. Unsupported escape char \\%c", c); error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse ext header. Unsupported escape char \\%c", c);
goto out; goto out;
} }
break;
default: default:
break; break;
} }
...@@ -820,6 +802,10 @@ static int config_parse(diskfile_backend *cfg_file) ...@@ -820,6 +802,10 @@ static int config_parse(diskfile_backend *cfg_file)
cfg_file->reader.read_ptr = cfg_file->reader.buffer.data; cfg_file->reader.read_ptr = cfg_file->reader.buffer.data;
cfg_file->reader.eof = 0; cfg_file->reader.eof = 0;
/* If the file is empty, there's nothing for us to do */
if (*cfg_file->reader.read_ptr == '\0')
return GIT_SUCCESS;
skip_bom(cfg_file); skip_bom(cfg_file);
while (error == GIT_SUCCESS && !cfg_file->reader.eof) { while (error == GIT_SUCCESS && !cfg_file->reader.eof) {
...@@ -832,6 +818,7 @@ static int config_parse(diskfile_backend *cfg_file) ...@@ -832,6 +818,7 @@ static int config_parse(diskfile_backend *cfg_file)
case '[': /* section header, new section begins */ case '[': /* section header, new section begins */
free(current_section); free(current_section);
current_section = NULL;
error = parse_section_header(cfg_file, &current_section); error = parse_section_header(cfg_file, &current_section);
break; break;
...@@ -871,7 +858,6 @@ static int config_parse(diskfile_backend *cfg_file) ...@@ -871,7 +858,6 @@ static int config_parse(diskfile_backend *cfg_file)
} }
} }
if (current_section)
free(current_section); free(current_section);
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse config"); return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse config");
......
[valid "subsection"]
something = true
[something "else"]
something = false
[valid "subsection"]
something = a
; we don't allow anything after closing "
[sec "subsec"x]
bleh = blah
...@@ -159,6 +159,35 @@ BEGIN_TEST(config5, "test number suffixes") ...@@ -159,6 +159,35 @@ BEGIN_TEST(config5, "test number suffixes")
git_config_free(cfg); git_config_free(cfg);
END_TEST END_TEST
BEGIN_TEST(config6, "test blank lines")
git_config *cfg;
int i;
must_pass(git_config_open_file(&cfg, CONFIG_BASE "/config6"));
must_pass(git_config_get_bool(cfg, "valid.subsection.something", &i));
must_be_true(i == 1);
must_pass(git_config_get_bool(cfg, "something.else.something", &i));
must_be_true(i == 0);
git_config_free(cfg);
END_TEST
BEGIN_TEST(config7, "test for invalid ext headers")
git_config *cfg;
must_fail(git_config_open_file(&cfg, CONFIG_BASE "/config7"));
END_TEST
BEGIN_TEST(config8, "don't fail on empty files")
git_config *cfg;
must_pass(git_config_open_file(&cfg, CONFIG_BASE "/config8"));
git_config_free(cfg);
END_TEST
BEGIN_SUITE(config) BEGIN_SUITE(config)
ADD_TEST(config0); ADD_TEST(config0);
...@@ -167,4 +196,7 @@ BEGIN_SUITE(config) ...@@ -167,4 +196,7 @@ BEGIN_SUITE(config)
ADD_TEST(config3); ADD_TEST(config3);
ADD_TEST(config4); ADD_TEST(config4);
ADD_TEST(config5); ADD_TEST(config5);
ADD_TEST(config6);
ADD_TEST(config7);
ADD_TEST(config8);
END_SUITE END_SUITE
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