* Parse extra_extensions from a yyjson object root. * Appends valid entries to *entries / *count (growing via realloc). * Project-level entries (from_project=true) are appended after global * entries so that a later dedup pass can prefer project values. * * Returns 0 on success, -1 on alloc failure. */
| 188 | * Returns 0 on success, -1 on alloc failure. |
| 189 | */ |
| 190 | static int parse_extra_extensions(yyjson_val *root, cbm_userext_t **entries, int *count, |
| 191 | const char *source_label) { |
| 192 | if (!yyjson_is_obj(root)) { |
| 193 | cbm_log_warn("userconfig.bad_root", "file", source_label); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | yyjson_val *extra = yyjson_obj_get(root, "extra_extensions"); |
| 198 | if (!extra) { |
| 199 | return 0; /* key absent — fine */ |
| 200 | } |
| 201 | if (!yyjson_is_obj(extra)) { |
| 202 | cbm_log_warn("userconfig.bad_extra_extensions", "file", source_label); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | yyjson_obj_iter iter; |
| 207 | yyjson_obj_iter_init(extra, &iter); |
| 208 | yyjson_val *key; |
| 209 | while ((key = yyjson_obj_iter_next(&iter)) != NULL) { |
| 210 | yyjson_val *val = yyjson_obj_iter_get_val(key); |
| 211 | |
| 212 | const char *ext_str = yyjson_get_str(key); |
| 213 | const char *lang_str = yyjson_get_str(val); |
| 214 | |
| 215 | if (!ext_str || !lang_str) { |
| 216 | cbm_log_warn("userconfig.skip_non_string", "file", source_label); |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | /* Extension must start with '.' */ |
| 221 | if (ext_str[0] != '.') { |
| 222 | cbm_log_warn("userconfig.skip_bad_ext", "file", source_label, "ext", ext_str); |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | CBMLanguage lang = lang_from_string(lang_str); |
| 227 | if (lang == CBM_LANG_COUNT) { |
| 228 | cbm_log_warn("userconfig.unknown_lang", "file", source_label, "lang", lang_str); |
| 229 | continue; /* fail-open: skip unknown languages */ |
| 230 | } |
| 231 | |
| 232 | /* Grow the array */ |
| 233 | cbm_userext_t *tmp = realloc(*entries, (size_t)(*count + SKIP_ONE) * sizeof(cbm_userext_t)); |
| 234 | if (!tmp) { |
| 235 | return CBM_NOT_FOUND; |
| 236 | } |
| 237 | *entries = tmp; |
| 238 | |
| 239 | char *ext_copy = strdup(ext_str); |
| 240 | if (!ext_copy) { |
| 241 | return CBM_NOT_FOUND; |
| 242 | } |
| 243 | |
| 244 | (*entries)[*count].ext = ext_copy; |
| 245 | (*entries)[*count].lang = lang; |
| 246 | (*count)++; |
| 247 | } |
no test coverage detected