Read entire file into malloc'd buffer. Returns NULL on error. */
| 3207 | |
| 3208 | /* Read entire file into malloc'd buffer. Returns NULL on error. */ |
| 3209 | static char *read_file_str(const char *path, size_t *out_len) { |
| 3210 | FILE *f = fopen(path, "r"); |
| 3211 | if (!f) { |
| 3212 | if (out_len) { |
| 3213 | *out_len = 0; |
| 3214 | } |
| 3215 | return NULL; |
| 3216 | } |
| 3217 | (void)fseek(f, 0, SEEK_END); |
| 3218 | long size = ftell(f); |
| 3219 | (void)fseek(f, 0, SEEK_SET); |
| 3220 | if (size < 0 || size > (long)CLI_MB_10 * CLI_MB_FACTOR) { /* cap at 10 MB */ |
| 3221 | (void)fclose(f); |
| 3222 | return NULL; |
| 3223 | } |
| 3224 | |
| 3225 | char *buf = malloc((size_t)size + CLI_SKIP_ONE); |
| 3226 | if (!buf) { |
| 3227 | (void)fclose(f); |
| 3228 | return NULL; |
| 3229 | } |
| 3230 | size_t nread = fread(buf, CLI_ELEM_SIZE, (size_t)size, f); |
| 3231 | (void)fclose(f); |
| 3232 | if (nread > (size_t)size) { |
| 3233 | nread = (size_t)size; |
| 3234 | } |
| 3235 | buf[nread] = '\0'; |
| 3236 | if (out_len) { |
| 3237 | *out_len = nread; |
| 3238 | } |
| 3239 | return buf; |
| 3240 | } |
| 3241 | |
| 3242 | static int ensure_parent_dir(const char *path) { |
| 3243 | if (!path || !path[0]) { |
no outgoing calls
no test coverage detected