Read file into a malloc'd buffer (= mimalloc in production). * *out_size receives the on-disk size and *out_status the failure reason so the * caller can attribute a skip to the right phase (read vs oversized) instead of * a silent drop. Both out params may be NULL. */
| 231 | * caller can attribute a skip to the right phase (read vs oversized) instead of |
| 232 | * a silent drop. Both out params may be NULL. */ |
| 233 | static char *read_file(const char *path, int *out_len, long *out_size, |
| 234 | cbm_read_status_t *out_status) { |
| 235 | if (out_size) { |
| 236 | *out_size = 0; |
| 237 | } |
| 238 | if (out_status) { |
| 239 | *out_status = CBM_READ_OK; |
| 240 | } |
| 241 | FILE *f = cbm_fopen(path, "rb"); |
| 242 | if (!f) { |
| 243 | if (out_status) { |
| 244 | *out_status = CBM_READ_OPEN_FAIL; |
| 245 | } |
| 246 | return NULL; |
| 247 | } |
| 248 | (void)fseek(f, 0, SEEK_END); |
| 249 | long size = ftell(f); |
| 250 | (void)fseek(f, 0, SEEK_SET); |
| 251 | if (out_size) { |
| 252 | *out_size = size; |
| 253 | } |
| 254 | if (size <= 0) { |
| 255 | (void)fclose(f); |
| 256 | if (out_status) { |
| 257 | *out_status = CBM_READ_EMPTY; |
| 258 | } |
| 259 | return NULL; |
| 260 | } |
| 261 | if (size > cbm_max_file_bytes()) { /* generous, env-configurable cap (B4) */ |
| 262 | (void)fclose(f); |
| 263 | if (out_status) { |
| 264 | *out_status = CBM_READ_OVERSIZED; |
| 265 | } |
| 266 | return NULL; |
| 267 | } |
| 268 | char *buf = (char *)malloc((size_t)size + SKIP_ONE); |
| 269 | if (!buf) { |
| 270 | (void)fclose(f); |
| 271 | if (out_status) { |
| 272 | *out_status = CBM_READ_OOM; |
| 273 | } |
| 274 | return NULL; |
| 275 | } |
| 276 | size_t nread = fread(buf, SKIP_ONE, (size_t)size, f); |
| 277 | (void)fclose(f); |
| 278 | buf[nread] = '\0'; |
| 279 | *out_len = (int)nread; |
| 280 | return buf; |
| 281 | } |
| 282 | |
| 283 | /* ── Per-worker skip list (Stage 2 / Track B) ─────────────────────── |
| 284 | * Each extract worker appends read/extract/oversized skips into its OWN list |
no test coverage detected