| 2804 | } |
| 2805 | |
| 2806 | static char *sh_read(const char *path, size_t *len) { |
| 2807 | FILE *f = cbm_fopen(path, "rb"); |
| 2808 | if (!f) { |
| 2809 | return NULL; |
| 2810 | } |
| 2811 | (void)fseek(f, 0, SEEK_END); |
| 2812 | long size = ftell(f); |
| 2813 | (void)fseek(f, 0, SEEK_SET); |
| 2814 | if (size <= 0 || size > cbm_max_file_bytes()) { |
| 2815 | (void)fclose(f); |
| 2816 | return NULL; |
| 2817 | } |
| 2818 | enum { SH_TS_PAD = 16 }; /* tree-sitter's lexer reads past the end */ |
| 2819 | char *buf = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)size + SH_TS_PAD); |
| 2820 | if (!buf) { |
| 2821 | (void)fclose(f); |
| 2822 | return NULL; |
| 2823 | } |
| 2824 | size_t got = fread(buf, SKIP_ONE, (size_t)size, f); |
| 2825 | (void)fclose(f); |
| 2826 | memset(buf + got, 0, SH_TS_PAD); |
| 2827 | *len = got; |
| 2828 | return buf; |
| 2829 | } |
| 2830 | |
| 2831 | typedef struct { |
| 2832 | sh_index_t *ix; |
no test coverage detected