Read an entire file into a malloc'd buffer. Returns NULL on failure. */
| 33 | |
| 34 | /* Read an entire file into a malloc'd buffer. Returns NULL on failure. */ |
| 35 | static char *pkgmap_read_file(const char *path, int *out_len) { |
| 36 | FILE *f = cbm_fopen(path, "rb"); |
| 37 | if (!f) { |
| 38 | return NULL; |
| 39 | } |
| 40 | (void)fseek(f, 0, SEEK_END); |
| 41 | long size = ftell(f); |
| 42 | (void)fseek(f, 0, SEEK_SET); |
| 43 | if (size <= 0 || size > (long)CBM_SZ_1K * CBM_SZ_1K) { /* 1MB cap for manifests */ |
| 44 | (void)fclose(f); |
| 45 | return NULL; |
| 46 | } |
| 47 | char *buf = (char *)malloc((size_t)size + SKIP_ONE); |
| 48 | if (!buf) { |
| 49 | (void)fclose(f); |
| 50 | return NULL; |
| 51 | } |
| 52 | size_t nread = fread(buf, SKIP_ONE, (size_t)size, f); |
| 53 | (void)fclose(f); |
| 54 | buf[nread] = '\0'; |
| 55 | *out_len = (int)nread; |
| 56 | return buf; |
| 57 | } |
| 58 | |
| 59 | /* ── Constants ─────────────────────────────────────────────────── */ |
| 60 |
no test coverage detected