diff options
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); |
