* 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. */
| 166 | * Returns 0 on success, -1 on alloc failure. |
| 167 | */ |
| 168 | static int parse_extra_extensions(yyjson_val *root, cbm_userext_t **entries, int *count, |
| 169 | const char *source_label) { |
| 170 | if (!yyjson_is_obj(root)) { |
| 171 | cbm_log_warn("userconfig.bad_root", "file", source_label); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | yyjson_val *extra = yyjson_obj_get(root, "extra_extensions"); |
| 176 | if (!extra) { |
| 177 | return 0; /* key absent — fine */ |
| 178 | } |
| 179 | if (!yyjson_is_obj(extra)) { |
| 180 | cbm_log_warn("userconfig.bad_extra_extensions", "file", source_label); |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | yyjson_obj_iter iter; |
| 185 | yyjson_obj_iter_init(extra, &iter); |
| 186 | yyjson_val *key; |
| 187 | while ((key = yyjson_obj_iter_next(&iter)) != NULL) { |
| 188 | yyjson_val *val = yyjson_obj_iter_get_val(key); |
| 189 | |
| 190 | const char *ext_str = yyjson_get_str(key); |
| 191 | const char *lang_str = yyjson_get_str(val); |
| 192 | |
| 193 | if (!ext_str || !lang_str) { |
| 194 | cbm_log_warn("userconfig.skip_non_string", "file", source_label); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | /* Extension must start with '.' */ |
| 199 | if (ext_str[0] != '.') { |
| 200 | cbm_log_warn("userconfig.skip_bad_ext", "file", source_label, "ext", ext_str); |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | CBMLanguage lang = lang_from_string(lang_str); |
| 205 | if (lang == CBM_LANG_COUNT) { |
| 206 | cbm_log_warn("userconfig.unknown_lang", "file", source_label, "lang", lang_str); |
| 207 | continue; /* fail-open: skip unknown languages */ |
| 208 | } |
| 209 | |
| 210 | /* Grow the array */ |
| 211 | cbm_userext_t *tmp = realloc(*entries, (size_t)(*count + SKIP_ONE) * sizeof(cbm_userext_t)); |
| 212 | if (!tmp) { |
| 213 | return CBM_NOT_FOUND; |
| 214 | } |
| 215 | *entries = tmp; |
| 216 | |
| 217 | char *ext_copy = strdup(ext_str); |
| 218 | if (!ext_copy) { |
| 219 | return CBM_NOT_FOUND; |
| 220 | } |
| 221 | |
| 222 | (*entries)[*count].ext = ext_copy; |
| 223 | (*entries)[*count].lang = lang; |
| 224 | (*count)++; |
| 225 | } |
no test coverage detected