Retain the project input before parsing it; never reopen for declarations. */
| 338 | |
| 339 | /* Retain the project input before parsing it; never reopen for declarations. */ |
| 340 | static int load_project_source(const char *path, cbm_userconfig_t *cfg, cbm_userext_t **entries, |
| 341 | int *count) { |
| 342 | cbm_userconfig_source_t *source = cfg->project_source; |
| 343 | source->state = CBM_USERCONFIG_SOURCE_ERROR; |
| 344 | userconfig_source_digest("read-error", NULL, 0, cfg->project_source_sha256); |
| 345 | cbm_path_info_t info = {0}; |
| 346 | int status = cbm_path_info_utf8(path, &info); |
| 347 | if (status == CBM_PATH_INFO_ABSENT) { |
| 348 | source->state = CBM_USERCONFIG_SOURCE_ABSENT; |
| 349 | /* Preserve the existing digest for actual absence. */ |
| 350 | userconfig_source_digest("missing-or-unreadable", NULL, 0, cfg->project_source_sha256); |
| 351 | return 0; |
| 352 | } |
| 353 | if (status != CBM_PATH_INFO_OK || !info.is_regular || info.size > MAX_CONFIG_SIZE) |
| 354 | return 0; |
| 355 | char *bytes = cbm_arena_alloc(&source->arena, MAX_CONFIG_SIZE + 1U); |
| 356 | if (!bytes) |
| 357 | return CBM_NOT_FOUND; |
| 358 | FILE *file = cbm_fopen(path, "rb"); |
| 359 | if (!file) |
| 360 | return 0; |
| 361 | size_t len = fread(bytes, 1, MAX_CONFIG_SIZE + 1U, file); |
| 362 | bool valid = !ferror(file) && feof(file) && len <= MAX_CONFIG_SIZE; |
| 363 | if (fclose(file) != 0) |
| 364 | valid = false; |
| 365 | if (!valid) |
| 366 | return 0; |
| 367 | bytes[len] = '\0'; |
| 368 | source->state = CBM_USERCONFIG_SOURCE_PRESENT; |
| 369 | source->bytes = bytes; |
| 370 | source->len = len; |
| 371 | userconfig_source_digest(len ? "present" : "empty", bytes, len, cfg->project_source_sha256); |
| 372 | if (!len) |
| 373 | return 0; |
| 374 | yyjson_read_err error = {0}; |
| 375 | yyjson_doc *doc = yyjson_read_opts(bytes, len, 0, NULL, &error); |
| 376 | if (!doc) { |
| 377 | if (error.code == YYJSON_READ_ERROR_MEMORY_ALLOCATION) |
| 378 | return CBM_NOT_FOUND; |
| 379 | cbm_log_warn("userconfig.corrupt_json", "path", path); |
| 380 | return 0; |
| 381 | } |
| 382 | int rc = parse_extra_extensions(yyjson_doc_get_root(doc), entries, count, path); |
| 383 | yyjson_doc_free(doc); |
| 384 | return rc; |
| 385 | } |
| 386 | |
| 387 | /* ── Public API ──────────────────────────────────────────────────── */ |
| 388 |
no test coverage detected