| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "config_parse.h" |
| 9 |
|
| |
| 10 |
|
| #include <ctype.h> |
| 11 |
|
| |
| 12 |
|
| const char *git_config_escapes = "ntb\"\\"; |
| 13 |
|
| const char *git_config_escaped = "\n\t\b\"\\"; |
| 14 |
|
| |
| 15 |
0 |
| static void set_parse_error(git_config_parser *reader, int col, const char *error_str) |
| 16 |
|
| { |
| 17 |
0 |
▶ | if (col) |
Branch: 1 branch
| Branch | True | False | Total |
| if (col) |
0 |
0 |
0 |
|
| 18 |
0 |
| git_error_set(GIT_ERROR_CONFIG, |
| 19 |
|
| "failed to parse config file: %s (in %s:%"PRIuZ", column %d)", |
| 20 |
|
| error_str, reader->path, reader->ctx.line_num, col); |
| 21 |
|
| else |
| 22 |
0 |
| git_error_set(GIT_ERROR_CONFIG, |
| 23 |
|
| "failed to parse config file: %s (in %s:%"PRIuZ")", |
| 24 |
|
| error_str, reader->path, reader->ctx.line_num); |
| 25 |
0 |
| } |
| 26 |
|
| |
| 27 |
|
| |
| 28 |
4427 |
| GIT_INLINE(int) config_keychar(int c) |
| 29 |
|
| { |
| 30 |
4427 |
▶ | return isalnum(c) || c == '-'; |
Branch: 2 branches
| Branch | True | False | Total |
| return isalnum(c) || c == '-'; |
0 |
4427 |
4427 |
| return isalnum(c) || c == '-'; |
0 |
0 |
0 |
|
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
1962 |
| static int strip_comments(char *line, int in_quotes) |
| 34 |
|
| { |
| 35 |
1962 |
| int quote_count = in_quotes, backslash_count = 0; |
| 36 |
|
| char *ptr; |
| 37 |
|
| |
| 38 |
50177 |
▶ | for (ptr = line; *ptr; ++ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ptr = line; *ptr; ++ptr) { |
48215 |
1962 |
50177 |
|
| 39 |
48215 |
▶ | if (ptr[0] == '"' && ((ptr > line && ptr[-1] != '\\') || ptr == line)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (ptr[0] == '"' && ((ptr > line && ptr[-1] != '\\') || ptr == line)) |
0 |
48215 |
48215 |
| if (ptr[0] == '"' && ((ptr > line && ptr[-1] != '\\') || ptr == line)) |
0 |
0 |
0 |
| if (ptr[0] == '"' && ((ptr > line && ptr[-1] != '\\') || ptr == line)) |
0 |
0 |
0 |
| if (ptr[0] == '"' && ((ptr > line && ptr[-1] != '\\') || ptr == line)) |
0 |
0 |
0 |
|
| 40 |
0 |
| quote_count++; |
| 41 |
|
| |
| 42 |
48215 |
▶ | if ((ptr[0] == ';' || ptr[0] == '#') && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((ptr[0] == ';' || ptr[0] == '#') && |
48215 |
0 |
48215 |
| if ((ptr[0] == ';' || ptr[0] == '#') && |
0 |
48215 |
48215 |
|
| 43 |
0 |
▶ | (quote_count % 2) == 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| (quote_count % 2) == 0 && |
0 |
0 |
0 |
|
| 44 |
0 |
▶ | (backslash_count % 2) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| (backslash_count % 2) == 0) { |
0 |
0 |
0 |
|
| 45 |
0 |
| ptr[0] = '\0'; |
| 46 |
0 |
| break; |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
48215 |
▶ | if (ptr[0] == '\\') |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr[0] == '\\') |
0 |
48215 |
48215 |
|
| 50 |
0 |
| backslash_count++; |
| 51 |
|
| else |
| 52 |
48215 |
| backslash_count = 0; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
|
| /* skip any space at the end */ |
| 56 |
3924 |
▶ | while (ptr > line && git__isspace(ptr[-1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (ptr > line && git__isspace(ptr[-1])) { |
3924 |
0 |
3924 |
| while (ptr > line && git__isspace(ptr[-1])) { |
1962 |
1962 |
3924 |
|
| 57 |
1962 |
| ptr--; |
| 58 |
|
| } |
| 59 |
1962 |
| ptr[0] = '\0'; |
| 60 |
|
| |
| 61 |
1962 |
| return quote_count; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| |
| 65 |
273 |
| static int parse_subsection_header(git_config_parser *reader, const char *line, size_t pos, const char *base_name, char **section_name) |
| 66 |
|
| { |
| 67 |
|
| int c, rpos; |
| 68 |
|
| const char *first_quote, *last_quote; |
| 69 |
273 |
| const char *line_start = line; |
| 70 |
273 |
| git_str buf = GIT_STR_INIT; |
| 71 |
273 |
| size_t quoted_len, alloc_len, base_name_len = strlen(base_name); |
| 72 |
|
| |
| 73 |
|
| /* Skip any additional whitespace before our section name */ |
| 74 |
273 |
▶ | while (git__isspace(line[pos])) |
Branch: 1 branch
| Branch | True | False | Total |
| while (git__isspace(line[pos])) |
0 |
273 |
273 |
|
| 75 |
0 |
| pos++; |
| 76 |
|
| |
| 77 |
|
| /* We should be at the first quotation mark. */ |
| 78 |
273 |
▶ | if (line[pos] != '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (line[pos] != '"') { |
0 |
273 |
273 |
|
| 79 |
0 |
| set_parse_error(reader, 0, "missing quotation marks in section header"); |
| 80 |
0 |
| goto end_error; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
273 |
| first_quote = &line[pos]; |
| 84 |
273 |
| last_quote = strrchr(line, '"'); |
| 85 |
273 |
| quoted_len = last_quote - first_quote; |
| 86 |
|
| |
| 87 |
273 |
▶ | if ((last_quote - line) > INT_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((last_quote - line) > INT_MAX) { |
0 |
273 |
273 |
|
| 88 |
0 |
| set_parse_error(reader, 0, "invalid section header, line too long"); |
| 89 |
0 |
| goto end_error; |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
273 |
▶ | if (quoted_len == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (quoted_len == 0) { |
0 |
273 |
273 |
|
| 93 |
0 |
| set_parse_error(reader, 0, "missing closing quotation mark in section header"); |
| 94 |
0 |
| goto end_error; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
273 |
▶ | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len); |
0 |
273 |
273 |
| GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len); |
0 |
273 |
273 |
|
| 98 |
273 |
▶ | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2); |
0 |
273 |
273 |
| GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2); |
0 |
273 |
273 |
|
| 99 |
|
| |
| 100 |
546 |
| if (git_str_grow(&buf, alloc_len) < 0 || |
| 101 |
273 |
| git_str_printf(&buf, "%s.", base_name) < 0) |
| 102 |
0 |
| goto end_error; |
| 103 |
|
| |
| 104 |
273 |
| rpos = 0; |
| 105 |
|
| |
| 106 |
273 |
| line = first_quote; |
| 107 |
273 |
| c = line[++rpos]; |
| 108 |
|
| |
| 109 |
|
| /* |
| 110 |
|
| * At the end of each iteration, whatever is stored in c will be |
| 111 |
|
| * added to the string. In case of error, jump to out |
| 112 |
|
| */ |
| 113 |
|
| do { |
| 114 |
|
| |
| 115 |
2371 |
| switch (c) { |
| 116 |
0 |
| case 0: |
| 117 |
0 |
| set_parse_error(reader, 0, "unexpected end-of-line in section header"); |
| 118 |
0 |
| goto end_error; |
| 119 |
|
| |
| 120 |
0 |
| case '"': |
| 121 |
0 |
| goto end_parse; |
| 122 |
|
| |
| 123 |
0 |
| case '\\': |
| 124 |
0 |
| c = line[++rpos]; |
| 125 |
|
| |
| 126 |
0 |
▶ | if (c == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == 0) { |
0 |
0 |
0 |
|
| 127 |
0 |
| set_parse_error(reader, rpos, "unexpected end-of-line in section header"); |
| 128 |
0 |
| goto end_error; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| default: |
| 132 |
2371 |
| break; |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
2371 |
| git_str_putc(&buf, (char)c); |
| 136 |
2371 |
| c = line[++rpos]; |
| 137 |
2371 |
▶ | } while (line + rpos < last_quote); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (line + rpos < last_quote); |
2098 |
273 |
2371 |
|
| 138 |
|
| |
| 139 |
273 |
| end_parse: |
| 140 |
273 |
▶ | if (git_str_oom(&buf)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_str_oom(&buf)) |
0 |
273 |
273 |
|
| 141 |
0 |
| goto end_error; |
| 142 |
|
| |
| 143 |
273 |
▶ | if (line[rpos] != '"' || line[rpos + 1] != ']') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (line[rpos] != '"' || line[rpos + 1] != ']') { |
273 |
0 |
273 |
| if (line[rpos] != '"' || line[rpos + 1] != ']') { |
0 |
273 |
273 |
|
| 144 |
0 |
| set_parse_error(reader, rpos, "unexpected text after closing quotes"); |
| 145 |
0 |
| git_str_dispose(&buf); |
| 146 |
0 |
| return -1; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
273 |
| *section_name = git_str_detach(&buf); |
| 150 |
273 |
| return (int)(&line[rpos + 2] - line_start); /* rpos is at the closing quote */ |
| 151 |
|
| |
| 152 |
0 |
| end_error: |
| 153 |
0 |
| git_str_dispose(&buf); |
| 154 |
|
| |
| 155 |
0 |
| return -1; |
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
942 |
| static int parse_section_header(git_config_parser *reader, char **section_out) |
| 159 |
|
| { |
| 160 |
|
| char *name, *name_end; |
| 161 |
|
| int name_length, c, pos; |
| 162 |
|
| int result; |
| 163 |
|
| char *line; |
| 164 |
|
| size_t line_len; |
| 165 |
|
| |
| 166 |
942 |
| git_parse_advance_ws(&reader->ctx); |
| 167 |
942 |
| line = git__strndup(reader->ctx.line, reader->ctx.line_len); |
| 168 |
942 |
▶ | if (line == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (line == NULL) |
0 |
942 |
942 |
|
| 169 |
0 |
| return -1; |
| 170 |
|
| |
| 171 |
|
| /* find the end of the variable's name */ |
| 172 |
942 |
| name_end = strrchr(line, ']'); |
| 173 |
942 |
▶ | if (name_end == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (name_end == NULL) { |
0 |
942 |
942 |
|
| 174 |
0 |
| git__free(line); |
| 175 |
0 |
| set_parse_error(reader, 0, "missing ']' in section header"); |
| 176 |
0 |
| return -1; |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
942 |
▶ | GIT_ERROR_CHECK_ALLOC_ADD(&line_len, (size_t)(name_end - line), 1); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC_ADD(&line_len, (size_t)(name_end - line), 1); |
0 |
942 |
942 |
| GIT_ERROR_CHECK_ALLOC_ADD(&line_len, (size_t)(name_end - line), 1); |
0 |
942 |
942 |
|
| 180 |
942 |
| name = git__malloc(line_len); |
| 181 |
942 |
▶ | GIT_ERROR_CHECK_ALLOC(name); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(name); |
0 |
942 |
942 |
|
| 182 |
|
| |
| 183 |
942 |
| name_length = 0; |
| 184 |
942 |
| pos = 0; |
| 185 |
|
| |
| 186 |
|
| /* Make sure we were given a section header */ |
| 187 |
942 |
| c = line[pos++]; |
| 188 |
942 |
▶ | GIT_ASSERT(c == '['); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT(c == '['); |
0 |
942 |
942 |
|
| 189 |
|
| |
| 190 |
942 |
| c = line[pos++]; |
| 191 |
|
| |
| 192 |
|
| do { |
| 193 |
4700 |
▶ | if (git__isspace(c)){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (git__isspace(c)){ |
273 |
4427 |
4700 |
|
| 194 |
273 |
| name[name_length] = '\0'; |
| 195 |
273 |
| result = parse_subsection_header(reader, line, pos, name, section_out); |
| 196 |
273 |
| git__free(line); |
| 197 |
273 |
| git__free(name); |
| 198 |
273 |
| return result; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
4427 |
▶ | if (!config_keychar(c) && c != '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!config_keychar(c) && c != '.') { |
0 |
4427 |
4427 |
| if (!config_keychar(c) && c != '.') { |
0 |
0 |
0 |
|
| 202 |
0 |
| set_parse_error(reader, pos, "unexpected character in header"); |
| 203 |
0 |
| goto fail_parse; |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
4427 |
| name[name_length++] = (char)git__tolower(c); |
| 207 |
|
| |
| 208 |
4427 |
▶ | } while ((c = line[pos++]) != ']'); |
Branch: 1 branch
| Branch | True | False | Total |
| } while ((c = line[pos++]) != ']'); |
3758 |
669 |
4427 |
|
| 209 |
|
| |
| 210 |
669 |
▶ | if (line[pos - 1] != ']') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (line[pos - 1] != ']') { |
0 |
669 |
669 |
|
| 211 |
0 |
| set_parse_error(reader, pos, "unexpected end of file"); |
| 212 |
0 |
| goto fail_parse; |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
669 |
| git__free(line); |
| 216 |
|
| |
| 217 |
669 |
| name[name_length] = 0; |
| 218 |
669 |
| *section_out = name; |
| 219 |
|
| |
| 220 |
669 |
| return pos; |
| 221 |
|
| |
| 222 |
0 |
| fail_parse: |
| 223 |
0 |
| git__free(line); |
| 224 |
0 |
| git__free(name); |
| 225 |
0 |
| return -1; |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
251 |
| static int skip_bom(git_parse_ctx *parser) |
| 229 |
|
| { |
| 230 |
251 |
| git_str buf = GIT_STR_INIT_CONST(parser->content, parser->content_len); |
| 231 |
|
| git_str_bom_t bom; |
| 232 |
251 |
| int bom_offset = git_str_detect_bom(&bom, &buf); |
| 233 |
|
| |
| 234 |
251 |
▶ | if (bom == GIT_STR_BOM_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (bom == GIT_STR_BOM_UTF8) |
0 |
251 |
251 |
|
| 235 |
0 |
| git_parse_advance_chars(parser, bom_offset); |
| 236 |
|
| |
| 237 |
|
| /* TODO: reference implementation is pretty stupid with BoM */ |
| 238 |
|
| |
| 239 |
251 |
| return 0; |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
|
| /* |
| 243 |
|
| (* basic types *) |
| 244 |
|
| digit = "0".."9" |
| 245 |
|
| integer = digit { digit } |
| 246 |
|
| alphabet = "a".."z" + "A" .. "Z" |
| 247 |
|
| |
| 248 |
|
| section_char = alphabet | "." | "-" |
| 249 |
|
| extension_char = (* any character except newline *) |
| 250 |
|
| any_char = (* any character *) |
| 251 |
|
| variable_char = "alphabet" | "-" |
| 252 |
|
| |
| 253 |
|
| |
| 254 |
|
| (* actual grammar *) |
| 255 |
|
| config = { section } |
| 256 |
|
| |
| 257 |
|
| section = header { definition } |
| 258 |
|
| |
| 259 |
|
| header = "[" section [subsection | subsection_ext] "]" |
| 260 |
|
| |
| 261 |
|
| subsection = "." section |
| 262 |
|
| subsection_ext = "\"" extension "\"" |
| 263 |
|
| |
| 264 |
|
| section = section_char { section_char } |
| 265 |
|
| extension = extension_char { extension_char } |
| 266 |
|
| |
| 267 |
|
| definition = variable_name ["=" variable_value] "\n" |
| 268 |
|
| |
| 269 |
|
| variable_name = variable_char { variable_char } |
| 270 |
|
| variable_value = string | boolean | integer |
| 271 |
|
| |
| 272 |
|
| string = quoted_string | plain_string |
| 273 |
|
| quoted_string = "\"" plain_string "\"" |
| 274 |
|
| plain_string = { any_char } |
| 275 |
|
| |
| 276 |
|
| boolean = boolean_true | boolean_false |
| 277 |
|
| boolean_true = "yes" | "1" | "true" | "on" |
| 278 |
|
| boolean_false = "no" | "0" | "false" | "off" |
| 279 |
|
| */ |
| 280 |
|
| |
| 281 |
|
| /* '\"' -> '"' etc */ |
| 282 |
1962 |
| static int unescape_line( |
| 283 |
|
| char **out, bool *is_multi, const char *ptr, int quote_count) |
| 284 |
|
| { |
| 285 |
|
| char *str, *fixed, *esc; |
| 286 |
1962 |
| size_t ptr_len = strlen(ptr), alloc_len; |
| 287 |
|
| |
| 288 |
1962 |
| *is_multi = false; |
| 289 |
|
| |
| 290 |
3924 |
▶ | if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, ptr_len, 1) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, ptr_len, 1) || |
0 |
1962 |
1962 |
| if (GIT_ADD_SIZET_OVERFLOW(&alloc_len, ptr_len, 1) || |
0 |
1962 |
1962 |
|
| 291 |
1962 |
| (str = git__malloc(alloc_len)) == NULL) { |
| 292 |
0 |
| return -1; |
| 293 |
|
| } |
| 294 |
|
| |
| 295 |
1962 |
| fixed = str; |
| 296 |
|
| |
| 297 |
27591 |
▶ | while (*ptr != '\0') { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*ptr != '\0') { |
25629 |
1962 |
27591 |
|
| 298 |
25629 |
▶ | if (*ptr == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ptr == '"') { |
0 |
25629 |
25629 |
|
| 299 |
0 |
| quote_count++; |
| 300 |
25629 |
▶ | } else if (*ptr != '\\') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*ptr != '\\') { |
25629 |
0 |
25629 |
|
| 301 |
25629 |
| *fixed++ = *ptr; |
| 302 |
|
| } else { |
| 303 |
|
| /* backslash, check the next char */ |
| 304 |
0 |
| ptr++; |
| 305 |
|
| /* if we're at the end, it's a multiline, so keep the backslash */ |
| 306 |
0 |
▶ | if (*ptr == '\0') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*ptr == '\0') { |
0 |
0 |
0 |
|
| 307 |
0 |
| *is_multi = true; |
| 308 |
0 |
| goto done; |
| 309 |
|
| } |
| 310 |
0 |
▶ | if ((esc = strchr(git_config_escapes, *ptr)) != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((esc = strchr(git_config_escapes, *ptr)) != NULL) { |
0 |
0 |
0 |
|
| 311 |
0 |
| *fixed++ = git_config_escaped[esc - git_config_escapes]; |
| 312 |
|
| } else { |
| 313 |
0 |
| git__free(str); |
| 314 |
0 |
| git_error_set(GIT_ERROR_CONFIG, "invalid escape at %s", ptr); |
| 315 |
0 |
| return -1; |
| 316 |
|
| } |
| 317 |
|
| } |
| 318 |
25629 |
| ptr++; |
| 319 |
|
| } |
| 320 |
|
| |
| 321 |
1962 |
| done: |
| 322 |
1962 |
| *fixed = '\0'; |
| 323 |
1962 |
| *out = str; |
| 324 |
|
| |
| 325 |
1962 |
| return 0; |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
0 |
| static int parse_multiline_variable(git_config_parser *reader, git_str *value, int in_quotes, size_t *line_len) |
| 329 |
|
| { |
| 330 |
|
| int quote_count; |
| 331 |
0 |
| bool multiline = true; |
| 332 |
|
| |
| 333 |
0 |
▶ | while (multiline) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (multiline) { |
0 |
0 |
0 |
|
| 334 |
0 |
| char *line = NULL, *proc_line = NULL; |
| 335 |
|
| int error; |
| 336 |
|
| |
| 337 |
|
| /* Check that the next line exists */ |
| 338 |
0 |
| git_parse_advance_line(&reader->ctx); |
| 339 |
0 |
| line = git__strndup(reader->ctx.line, reader->ctx.line_len); |
| 340 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(line); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(line); |
0 |
0 |
0 |
|
| 341 |
0 |
▶ | if (GIT_ADD_SIZET_OVERFLOW(line_len, *line_len, reader->ctx.line_len)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIT_ADD_SIZET_OVERFLOW(line_len, *line_len, reader->ctx.line_len)) { |
0 |
0 |
0 |
| if (GIT_ADD_SIZET_OVERFLOW(line_len, *line_len, reader->ctx.line_len)) { |
0 |
0 |
0 |
|
| 342 |
0 |
| error = -1; |
| 343 |
0 |
| goto out; |
| 344 |
|
| } |
| 345 |
|
| |
| 346 |
|
| /* |
| 347 |
|
| * We've reached the end of the file, there is no continuation. |
| 348 |
|
| * (this is not an error). |
| 349 |
|
| */ |
| 350 |
0 |
▶ | if (line[0] == '\0') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (line[0] == '\0') { |
0 |
0 |
0 |
|
| 351 |
0 |
| error = 0; |
| 352 |
0 |
| goto out; |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
|
| /* If it was just a comment, pretend it didn't exist */ |
| 356 |
0 |
| quote_count = strip_comments(line, in_quotes); |
| 357 |
0 |
▶ | if (line[0] == '\0') |
Branch: 1 branch
| Branch | True | False | Total |
| if (line[0] == '\0') |
0 |
0 |
0 |
|
| 358 |
0 |
| goto next; |
| 359 |
|
| |
| 360 |
0 |
▶ | if ((error = unescape_line(&proc_line, &multiline, |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = unescape_line(&proc_line, &multiline, |
0 |
0 |
0 |
|
| 361 |
|
| line, in_quotes)) < 0) |
| 362 |
0 |
| goto out; |
| 363 |
|
| |
| 364 |
|
| /* Add this line to the multiline var */ |
| 365 |
0 |
▶ | if ((error = git_str_puts(value, proc_line)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_str_puts(value, proc_line)) < 0) |
0 |
0 |
0 |
|
| 366 |
0 |
| goto out; |
| 367 |
|
| |
| 368 |
0 |
| next: |
| 369 |
0 |
| git__free(line); |
| 370 |
0 |
| git__free(proc_line); |
| 371 |
0 |
| in_quotes = quote_count; |
| 372 |
0 |
| continue; |
| 373 |
|
| |
| 374 |
0 |
| out: |
| 375 |
0 |
| git__free(line); |
| 376 |
0 |
| git__free(proc_line); |
| 377 |
0 |
| return error; |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
0 |
| return 0; |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
16700 |
| GIT_INLINE(bool) is_namechar(char c) |
| 384 |
|
| { |
| 385 |
16700 |
▶ | return isalnum(c) || c == '-'; |
Branch: 2 branches
| Branch | True | False | Total |
| return isalnum(c) || c == '-'; |
1962 |
14738 |
16700 |
| return isalnum(c) || c == '-'; |
0 |
1962 |
1962 |
|
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
1962 |
| static int parse_name( |
| 389 |
|
| char **name, const char **value, git_config_parser *reader, const char *line) |
| 390 |
|
| { |
| 391 |
1962 |
| const char *name_end = line, *value_start; |
| 392 |
|
| |
| 393 |
1962 |
| *name = NULL; |
| 394 |
1962 |
| *value = NULL; |
| 395 |
|
| |
| 396 |
16700 |
▶ | while (*name_end && is_namechar(*name_end)) |
Branch: 2 branches
| Branch | True | False | Total |
| while (*name_end && is_namechar(*name_end)) |
16700 |
0 |
16700 |
| while (*name_end && is_namechar(*name_end)) |
14738 |
1962 |
16700 |
|
| 397 |
14738 |
| name_end++; |
| 398 |
|
| |
| 399 |
1962 |
▶ | if (line == name_end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (line == name_end) { |
0 |
1962 |
1962 |
|
| 400 |
0 |
| set_parse_error(reader, 0, "invalid configuration key"); |
| 401 |
0 |
| return -1; |
| 402 |
|
| } |
| 403 |
|
| |
| 404 |
1962 |
| value_start = name_end; |
| 405 |
|
| |
| 406 |
3924 |
▶ | while (*value_start && git__isspace(*value_start)) |
Branch: 2 branches
| Branch | True | False | Total |
| while (*value_start && git__isspace(*value_start)) |
3924 |
0 |
3924 |
| while (*value_start && git__isspace(*value_start)) |
1962 |
1962 |
3924 |
|
| 407 |
1962 |
| value_start++; |
| 408 |
|
| |
| 409 |
1962 |
▶ | if (*value_start == '=') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*value_start == '=') { |
1962 |
0 |
1962 |
|
| 410 |
1962 |
| *value = value_start + 1; |
| 411 |
0 |
▶ | } else if (*value_start) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*value_start) { |
0 |
0 |
0 |
|
| 412 |
0 |
| set_parse_error(reader, 0, "invalid configuration key"); |
| 413 |
0 |
| return -1; |
| 414 |
|
| } |
| 415 |
|
| |
| 416 |
1962 |
▶ | if ((*name = git__strndup(line, name_end - line)) == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((*name = git__strndup(line, name_end - line)) == NULL) |
0 |
1962 |
1962 |
|
| 417 |
0 |
| return -1; |
| 418 |
|
| |
| 419 |
1962 |
| return 0; |
| 420 |
|
| } |
| 421 |
|
| |
| 422 |
1962 |
| static int parse_variable(git_config_parser *reader, char **var_name, char **var_value, size_t *line_len) |
| 423 |
|
| { |
| 424 |
1962 |
| const char *value_start = NULL; |
| 425 |
1962 |
| char *line = NULL, *name = NULL, *value = NULL; |
| 426 |
|
| int quote_count, error; |
| 427 |
|
| bool multiline; |
| 428 |
|
| |
| 429 |
1962 |
| *var_name = NULL; |
| 430 |
1962 |
| *var_value = NULL; |
| 431 |
|
| |
| 432 |
1962 |
| git_parse_advance_ws(&reader->ctx); |
| 433 |
1962 |
| line = git__strndup(reader->ctx.line, reader->ctx.line_len); |
| 434 |
1962 |
▶ | GIT_ERROR_CHECK_ALLOC(line); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(line); |
0 |
1962 |
1962 |
|
| 435 |
|
| |
| 436 |
1962 |
| quote_count = strip_comments(line, 0); |
| 437 |
|
| |
| 438 |
1962 |
▶ | if ((error = parse_name(&name, &value_start, reader, line)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = parse_name(&name, &value_start, reader, line)) < 0) |
0 |
1962 |
1962 |
|
| 439 |
0 |
| goto out; |
| 440 |
|
| |
| 441 |
|
| /* |
| 442 |
|
| * Now, let's try to parse the value |
| 443 |
|
| */ |
| 444 |
1962 |
▶ | if (value_start != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value_start != NULL) { |
1962 |
0 |
1962 |
|
| 445 |
3924 |
▶ | while (git__isspace(value_start[0])) |
Branch: 1 branch
| Branch | True | False | Total |
| while (git__isspace(value_start[0])) |
1962 |
1962 |
3924 |
|
| 446 |
1962 |
| value_start++; |
| 447 |
|
| |
| 448 |
1962 |
▶ | if ((error = unescape_line(&value, &multiline, value_start, 0)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = unescape_line(&value, &multiline, value_start, 0)) < 0) |
0 |
1962 |
1962 |
|
| 449 |
0 |
| goto out; |
| 450 |
|
| |
| 451 |
1962 |
▶ | if (multiline) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (multiline) { |
0 |
1962 |
1962 |
|
| 452 |
0 |
| git_str multi_value = GIT_STR_INIT; |
| 453 |
0 |
| git_str_attach(&multi_value, value, 0); |
| 454 |
0 |
| value = NULL; |
| 455 |
|
| |
| 456 |
0 |
| if (parse_multiline_variable(reader, &multi_value, quote_count % 2, line_len) < 0 || |
| 457 |
0 |
| git_str_oom(&multi_value)) { |
| 458 |
0 |
| error = -1; |
| 459 |
0 |
| git_str_dispose(&multi_value); |
| 460 |
0 |
| goto out; |
| 461 |
|
| } |
| 462 |
|
| |
| 463 |
0 |
| value = git_str_detach(&multi_value); |
| 464 |
|
| } |
| 465 |
|
| } |
| 466 |
|
| |
| 467 |
1962 |
| *var_name = name; |
| 468 |
1962 |
| *var_value = value; |
| 469 |
1962 |
| name = NULL; |
| 470 |
1962 |
| value = NULL; |
| 471 |
|
| |
| 472 |
1962 |
| out: |
| 473 |
1962 |
| git__free(name); |
| 474 |
1962 |
| git__free(value); |
| 475 |
1962 |
| git__free(line); |
| 476 |
1962 |
| return error; |
| 477 |
|
| } |
| 478 |
|
| |
| 479 |
50 |
| int git_config_parser_init(git_config_parser *out, const char *path, const char *data, size_t datalen) |
| 480 |
|
| { |
| 481 |
50 |
| out->path = path; |
| 482 |
50 |
| return git_parse_ctx_init(&out->ctx, data, datalen); |
| 483 |
|
| } |
| 484 |
|
| |
| 485 |
50 |
| void git_config_parser_dispose(git_config_parser *parser) |
| 486 |
|
| { |
| 487 |
50 |
| git_parse_ctx_clear(&parser->ctx); |
| 488 |
50 |
| } |
| 489 |
|
| |
| 490 |
251 |
| int git_config_parse( |
| 491 |
|
| git_config_parser *parser, |
| 492 |
|
| git_config_parser_section_cb on_section, |
| 493 |
|
| git_config_parser_variable_cb on_variable, |
| 494 |
|
| git_config_parser_comment_cb on_comment, |
| 495 |
|
| git_config_parser_eof_cb on_eof, |
| 496 |
|
| void *payload) |
| 497 |
|
| { |
| 498 |
|
| git_parse_ctx *ctx; |
| 499 |
251 |
| char *current_section = NULL, *var_name = NULL, *var_value = NULL; |
| 500 |
251 |
| int result = 0; |
| 501 |
|
| |
| 502 |
251 |
| ctx = &parser->ctx; |
| 503 |
|
| |
| 504 |
251 |
| skip_bom(ctx); |
| 505 |
|
| |
| 506 |
3155 |
▶ | for (; ctx->remain_len > 0; git_parse_advance_line(ctx)) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; ctx->remain_len > 0; git_parse_advance_line(ctx)) { |
2904 |
251 |
3155 |
|
| 507 |
|
| const char *line_start; |
| 508 |
|
| size_t line_len; |
| 509 |
|
| char c; |
| 510 |
|
| |
| 511 |
2904 |
| restart: |
| 512 |
2904 |
| line_start = ctx->line; |
| 513 |
2904 |
| line_len = ctx->line_len; |
| 514 |
|
| |
| 515 |
|
| /* |
| 516 |
|
| * Get either first non-whitespace character or, if that does |
| 517 |
|
| * not exist, the first whitespace character. This is required |
| 518 |
|
| * to preserve whitespaces when writing back the file. |
| 519 |
|
| */ |
| 520 |
2904 |
| if (git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE) < 0 && |
| 521 |
0 |
| git_parse_peek(&c, ctx, 0) < 0) |
| 522 |
0 |
| continue; |
| 523 |
|
| |
| 524 |
2904 |
| switch (c) { |
| 525 |
942 |
| case '[': /* section header, new section begins */ |
| 526 |
942 |
| git__free(current_section); |
| 527 |
942 |
| current_section = NULL; |
| 528 |
|
| |
| 529 |
942 |
| result = parse_section_header(parser, ¤t_section); |
| 530 |
942 |
▶ | if (result < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (result < 0) |
0 |
942 |
942 |
|
| 531 |
0 |
| break; |
| 532 |
|
| |
| 533 |
942 |
| git_parse_advance_chars(ctx, result); |
| 534 |
|
| |
| 535 |
942 |
▶ | if (on_section) |
Branch: 1 branch
| Branch | True | False | Total |
| if (on_section) |
178 |
764 |
942 |
|
| 536 |
178 |
| result = on_section(parser, current_section, line_start, line_len, payload); |
| 537 |
|
| /* |
| 538 |
|
| * After we've parsed the section header we may not be |
| 539 |
|
| * done with the line. If there's still data in there, |
| 540 |
|
| * run the next loop with the rest of the current line |
| 541 |
|
| * instead of moving forward. |
| 542 |
|
| */ |
| 543 |
|
| |
| 544 |
942 |
▶ | if (!git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!git_parse_peek(&c, ctx, GIT_PARSE_PEEK_SKIP_WHITESPACE)) |
0 |
942 |
942 |
|
| 545 |
0 |
| goto restart; |
| 546 |
|
| |
| 547 |
942 |
| break; |
| 548 |
|
| |
| 549 |
0 |
| case '\n': /* comment or whitespace-only */ |
| 550 |
|
| case '\r': |
| 551 |
|
| case ' ': |
| 552 |
|
| case '\t': |
| 553 |
|
| case ';': |
| 554 |
|
| case '#': |
| 555 |
0 |
▶ | if (on_comment) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (on_comment) { |
0 |
0 |
0 |
|
| 556 |
0 |
| result = on_comment(parser, line_start, line_len, payload); |
| 557 |
|
| } |
| 558 |
0 |
| break; |
| 559 |
|
| |
| 560 |
1962 |
| default: /* assume variable declaration */ |
| 561 |
1962 |
▶ | if ((result = parse_variable(parser, &var_name, &var_value, &line_len)) == 0 && on_variable) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((result = parse_variable(parser, &var_name, &var_value, &line_len)) == 0 && on_variable) { |
1962 |
0 |
1962 |
| if ((result = parse_variable(parser, &var_name, &var_value, &line_len)) == 0 && on_variable) { |
1962 |
0 |
1962 |
|
| 562 |
1962 |
| result = on_variable(parser, current_section, var_name, var_value, line_start, line_len, payload); |
| 563 |
1962 |
| git__free(var_name); |
| 564 |
1962 |
| git__free(var_value); |
| 565 |
|
| } |
| 566 |
|
| |
| 567 |
1962 |
| break; |
| 568 |
|
| } |
| 569 |
|
| |
| 570 |
2904 |
▶ | if (result < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (result < 0) |
0 |
2904 |
2904 |
|
| 571 |
0 |
| goto out; |
| 572 |
|
| } |
| 573 |
|
| |
| 574 |
251 |
▶ | if (on_eof) |
Branch: 1 branch
| Branch | True | False | Total |
| if (on_eof) |
201 |
50 |
251 |
|
| 575 |
50 |
| result = on_eof(parser, current_section, payload); |
| 576 |
|
| |
| 577 |
201 |
| out: |
| 578 |
251 |
| git__free(current_section); |
| 579 |
251 |
| return result; |
| 580 |
|
| } |