diff options
| author | frosty <gabriel@bwaaa.monster> | 2026-04-10 17:09:41 -0400 |
|---|---|---|
| committer | frosty <gabriel@bwaaa.monster> | 2026-04-10 17:09:41 -0400 |
| commit | 8016cbdb7c190e6664a50560e3c8ea58f00fb31b (patch) | |
| tree | debaf00a4d04372a881513b4f9e3afaf4ef35c85 /src/template.c | |
| parent | 3fab89ecf8f4c664477a82add660d28db87357b4 (diff) | |
| parent | 80caa6b317fdde4f3223648da709d184438a5a08 (diff) | |
| download | beaker-8016cbdb7c190e6664a50560e3c8ea58f00fb31b.tar.gz | |
Merge branch 'indev'
Diffstat (limited to 'src/template.c')
| -rw-r--r-- | src/template.c | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/template.c b/src/template.c index 6255d1b..0937b66 100644 --- a/src/template.c +++ b/src/template.c @@ -1442,9 +1442,23 @@ char *render_template(const char *template_file, TemplateContext *ctx) { fseek(fp, 0, SEEK_END); long file_size = ftell(fp); + if (file_size < 0) { + perror("Error getting template file size"); + fprintf(stderr, + "[ERROR] render_template: Failed to get size of '%s'.\n", + full_path); + fclose(fp); + return NULL; + } fseek(fp, 0, SEEK_SET); - char *template_content = (char *)malloc(file_size + 1); + if (file_size == 0) { + char *empty_result = render_template_segment("", ctx); + fclose(fp); + return empty_result; + } + + char *template_content = (char *)malloc((size_t)file_size + 1); if (template_content == NULL) { perror("Error allocating memory for template content"); fprintf(stderr, @@ -1454,7 +1468,17 @@ char *render_template(const char *template_file, TemplateContext *ctx) { fclose(fp); return NULL; } - fread(template_content, 1, file_size, fp); + + size_t bytes_read = fread(template_content, 1, (size_t)file_size, fp); + if (bytes_read != (size_t)file_size) { + perror("Error reading template file"); + fprintf(stderr, + "[ERROR] render_template: Failed to read complete template '%s'.\n", + full_path); + free(template_content); + fclose(fp); + return NULL; + } template_content[file_size] = '\0'; fclose(fp); |
