From 6c402c771fd8bc2a0d826a1d395d8298399bf4c8 Mon Sep 17 00:00:00 2001 From: natjms Date: Fri, 20 Jan 2023 16:45:08 -0800 Subject: [PATCH] Fix #2 I love handling null terminated strings :)))) I have no idea how this didn't come up earlier --- calathea.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/calathea.c b/calathea.c index ccd1a7d..324a1f7 100644 --- a/calathea.c +++ b/calathea.c @@ -35,7 +35,7 @@ char * read_file(char *filename) { rewind(file); // Go back to the beginning of the file // Allocate enough space in our buffer to hold the entire file. - char *buffer = malloc(fileLength); + char *buffer = malloc(fileLength + 1); if (buffer == NULL) { printf("Warning: Failed to allocate enough memory for %s\n", filename); @@ -44,6 +44,7 @@ char * read_file(char *filename) { } fread(buffer, 1, fileLength, file); + buffer[fileLength] = 0; fclose(file); return buffer; @@ -335,9 +336,7 @@ int main(int argc, char *argv[]) { // Save the content string for later by mallocing it char *contentBuffer = endOfFirstLine; - currentPage->content = malloc( - sizeof(char) * (strlen(buffer) - titleLength + 1) - ); + currentPage->content = calloc(strlen(buffer), sizeof(char)); strcpy(currentPage->content, contentBuffer); // Copy the first line (title) into its respective field