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. */
| 214 | * caller can attribute a skip to the right phase (read vs oversized) instead of |
| 215 | * a silent drop. Both out params may be NULL. */ |
| 216 | static char *read_file(const char *path, int *out_len, long *out_size, |
| 217 | cbm_read_status_t *out_status) { |
| 218 | if (out_size) { |
| 219 | *out_size = 0; |
| 220 | } |
| 221 | if (out_status) { |
| 222 | *out_status = CBM_READ_OK; |
| 223 | } |
| 224 | FILE *f = cbm_fopen(path, "rb"); |
| 225 | if (!f) { |
| 226 | if (out_status) { |
| 227 | *out_status = CBM_READ_OPEN_FAIL; |
| 228 | } |
| 229 | return NULL; |
| 230 | } |
| 231 | (void)fseek(f, 0, SEEK_END); |
| 232 | long size = ftell(f); |
| 233 | (void)fseek(f, 0, SEEK_SET); |
| 234 | if (out_size) { |
| 235 | *out_size = size; |
| 236 | } |
| 237 | if (size <= 0) { |
| 238 | (void)fclose(f); |
| 239 | if (out_status) { |
| 240 | *out_status = CBM_READ_EMPTY; |
| 241 | } |
| 242 | return NULL; |
| 243 | } |
| 244 | if (size > cbm_max_file_bytes()) { /* generous, env-configurable cap (B4) */ |
| 245 | (void)fclose(f); |
| 246 | if (out_status) { |
| 247 | *out_status = CBM_READ_OVERSIZED; |
| 248 | } |
| 249 | return NULL; |
| 250 | } |
| 251 | char *buf = (char *)malloc((size_t)size + SKIP_ONE); |
| 252 | if (!buf) { |
| 253 | (void)fclose(f); |
| 254 | if (out_status) { |
| 255 | *out_status = CBM_READ_OOM; |
| 256 | } |
| 257 | return NULL; |
| 258 | } |
| 259 | size_t nread = fread(buf, SKIP_ONE, (size_t)size, f); |
| 260 | (void)fclose(f); |
| 261 | buf[nread] = '\0'; |
| 262 | *out_len = (int)nread; |
| 263 | return buf; |
| 264 | } |
| 265 | |
| 266 | /* ── Per-worker skip list (Stage 2 / Track B) ─────────────────────── |
| 267 | * Each extract worker appends read/extract/oversized skips into its OWN list |
no test coverage detected