Move config to the new error methods Take this opportunity to fix an instance of returning GIT_EOBJCORRUPTED when malloc failed. 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
diff --git a/src/config.c b/src/config.c
index d839e38..234c530 100644
--- a/src/config.c
+++ b/src/config.c
@@ -65,7 +65,9 @@ int git_config_open_bare(git_config **out, const char *path)
if (error < GIT_SUCCESS)
goto error;
- git_config_add_backend(cfg, backend, 1);
+ error = git_config_add_backend(cfg, backend, 1);
+ if (error < GIT_SUCCESS)
+ goto error;
error = backend->open(backend);
if (error < GIT_SUCCESS)
diff --git a/src/config_file.c b/src/config_file.c
index 05a57cd..d66dd20 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -316,18 +316,17 @@ static int config_set(git_config_backend *cfg, const char *name, const char *val
* Otherwise, create it and stick it at the end of the queue.
*/
+ last_dot = strrchr(name, '.');
+ if (last_dot == NULL) {
+ return git__throw(GIT_EINVALIDTYPE, "Variables without section aren't allowed");
+ }
+
var = git__malloc(sizeof(git_cvar));
if (var == NULL)
return GIT_ENOMEM;
memset(var, 0x0, sizeof(git_cvar));
- last_dot = strrchr(name, '.');
- if (last_dot == NULL) {
- error = GIT_ERROR;
- goto out;
- }
-
var->section = git__strndup(name, last_dot - name);
if (var->section == NULL) {
error = GIT_ENOMEM;
@@ -367,7 +366,7 @@ static int config_get(git_config_backend *cfg, const char *name, const char **ou
var = cvar_list_find(&b->var_list, name);
if (var == NULL)
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "Variable '%s' not found", name);
*out = var->value;
@@ -588,7 +587,7 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
last_quote = strrchr(line, '"');
if (last_quote - first_quote == 0)
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to parse ext header. There is no final quotation mark");
buf_len = last_quote - first_quote + 2;
@@ -611,7 +610,7 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
switch (c) {
case '"':
if (quote_marks++ >= 2)
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to parse ext header. Too many quotes");
break;
case '\\':
c = line[rpos++];
@@ -620,7 +619,7 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
case '\\':
break;
default:
- error = GIT_EOBJCORRUPTED;
+ error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse ext header. Unsupported escape char \\%c", c);
goto out;
}
default:
@@ -642,10 +641,10 @@ static int parse_section_header_ext(const char *line, const char *base_name, cha
ret = snprintf(*section_name, total_len, "%s %s", base_name, subsection);
if (ret >= total_len) {
/* If this fails, we've checked the length wrong */
- error = GIT_ERROR;
+ error = git__thow(GIT_ERROR, "Failed to parse ext header. Wrong total lenght calculation");
goto out;
} else if (ret < 0) {
- error = GIT_EOSERR;
+ error = git__throw(GIT_EOSERR, "Failed to parse ext header. OS error: %s", strerror(errno));
goto out;
}
@@ -671,11 +670,11 @@ static int parse_section_header(file_backend *cfg, char **section_out)
/* find the end of the variable's name */
name_end = strchr(line, ']');
if (name_end == NULL)
- return GIT_EOBJCORRUPTED;
+ return git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Can't find header name end");
name = (char *)git__malloc((size_t)(name_end - line) + 1);
if (name == NULL)
- return GIT_EOBJCORRUPTED;
+ return GIT_ENOMEM;
name_length = 0;
pos = 0;
@@ -683,7 +682,7 @@ static int parse_section_header(file_backend *cfg, char **section_out)
/* Make sure we were given a section header */
c = line[pos++];
if (c != '[') {
- error = GIT_EOBJCORRUPTED;
+ error = git__throw(GIT_ERROR, "Failed to parse header. Didn't get section header. This is a bug");
goto error;
}
@@ -691,7 +690,7 @@ static int parse_section_header(file_backend *cfg, char **section_out)
do {
if (cfg->reader.eof){
- error = GIT_EOBJCORRUPTED;
+ error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Config file ended unexpectedly");
goto error;
}
@@ -704,7 +703,7 @@ static int parse_section_header(file_backend *cfg, char **section_out)
}
if (!config_keychar(c) && c != '.') {
- error = GIT_EOBJCORRUPTED;
+ error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Wrong format on header");
goto error;
}
@@ -889,7 +888,7 @@ static int parse_multiline_variable(file_backend *cfg, const char *first, char *
/* We've reached the end of the file, there is input missing */
if (line[0] == '\0') {
- error = GIT_EOBJCORRUPTED;
+ error = git__throw(GIT_EOBJCORRUPTED, "Failed to parse multiline var. File ended unexpectedly");
goto out;
}
@@ -917,7 +916,7 @@ static int parse_multiline_variable(file_backend *cfg, const char *first, char *
ret = snprintf(buf, len, "%s %s", first, line);
if (ret < 0) {
- error = GIT_EOSERR;
+ error = git__throw(GIT_EOSERR, "Failed to parse multiline var. Failed to put together two lines. OS err: %s", strerror(errno));
free(buf);
goto out;
}