parse_section_header: save the name where it belongs Save the location of the name in section_out instead of returning it as an int. Use the return code to signal success or failure. Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
diff --git a/src/config.c b/src/config.c
index e615b03..a3d7f8e 100644
--- a/src/config.c
+++ b/src/config.c
@@ -229,43 +229,68 @@ static char *parse_section_header_ext(char *base_name, git_config *cfg)
return base_name;
}
-static int parse_section_header(char **section_out, const char *line)
+static int parse_section_header(git_config *cfg, char **section_out, const char *line)
{
- char *name, *name_start, *name_end;
+ char *name, *name_end;
int name_length, c;
+ int error = GIT_SUCCESS;
/* find the end of the variable's name */
- name_end = strchr(name_start, ']');
+ name_end = strchr(line, ']');
if (name_end == NULL)
- return NULL;
+ return GIT_EOBJCORRUPTED;
- name = (char *)git__malloc((size_t)(name_end - name_start) + 1);
+ name = (char *)git__malloc((size_t)(name_end - line) + 1);
if (name == NULL)
- return NULL;
+ return GIT_EOBJCORRUPTED;
name_length = 0;
+
+ /* Make sure we were given a section header */
+ c = cfg_getchar(cfg, SKIP_WHITESPACE | SKIP_COMMENTS);
+ if(c != '['){
+ error = GIT_EOBJCORRUPTED;
+ goto error;
+ }
+
c = cfg_getchar(cfg, SKIP_WHITESPACE | SKIP_COMMENTS);
do {
- if (cfg->reader.eof)
+ if (cfg->reader.eof){
+ error = GIT_EOBJCORRUPTED;
goto error;
+ }
- if (isspace(c))
- return parse_section_name_ext(name, cfg);
+ if (isspace(c)){
+ *section_out = parse_section_header_ext(name, cfg);
+ return GIT_SUCCESS;
+ }
- if (!config_keychar(c) && c != '.')
+ if (!config_keychar(c) && c != '.'){
+ error = GIT_EOBJCORRUPTED;
goto error;
+ }
name[name_length++] = tolower(c);
} while ((c = cfg_getchar(cfg, SKIP_COMMENTS)) != ']');
+ /*
+ * Here, we enforce that a section name needs to be on its own
+ * line
+ */
+ if(cfg_getchar(cfg, SKIP_COMMENTS) != '\n'){
+ error = GIT_EOBJCORRUPTED;
+ goto error;
+ }
+
name[name_length] = 0;
- return name;
+ *section_out = name;
+ return GIT_SUCCESS;
error:
free(name);
- return NULL;
+ return error;
}
static int skip_bom(git_config *cfg)