Read entire file into malloc'd buffer. Returns NULL on error. */
| 3780 | |
| 3781 | /* Read entire file into malloc'd buffer. Returns NULL on error. */ |
| 3782 | static char *read_file_str(const char *path, size_t *out_len) { |
| 3783 | FILE *f = fopen(path, "r"); |
| 3784 | if (!f) { |
| 3785 | if (out_len) { |
| 3786 | *out_len = 0; |
| 3787 | } |
| 3788 | return NULL; |
| 3789 | } |
| 3790 | (void)fseek(f, 0, SEEK_END); |
| 3791 | long size = ftell(f); |
| 3792 | (void)fseek(f, 0, SEEK_SET); |
| 3793 | if (size < 0 || size > (long)CLI_MB_10 * CLI_MB_FACTOR) { /* cap at 10 MB */ |
| 3794 | (void)fclose(f); |
| 3795 | return NULL; |
| 3796 | } |
| 3797 | |
| 3798 | char *buf = malloc((size_t)size + CLI_SKIP_ONE); |
| 3799 | if (!buf) { |
| 3800 | (void)fclose(f); |
| 3801 | return NULL; |
| 3802 | } |
| 3803 | size_t nread = fread(buf, CLI_ELEM_SIZE, (size_t)size, f); |
| 3804 | (void)fclose(f); |
| 3805 | if (nread > (size_t)size) { |
| 3806 | nread = (size_t)size; |
| 3807 | } |
| 3808 | buf[nread] = '\0'; |
| 3809 | if (out_len) { |
| 3810 | *out_len = nread; |
| 3811 | } |
| 3812 | return buf; |
| 3813 | } |
| 3814 | |
| 3815 | static int ensure_parent_dir(const char *path) { |
| 3816 | if (!path || !path[0]) { |
no test coverage detected