Fix #2
I love handling null terminated strings :)))) I have no idea how this didn't come up earlier
This commit is contained in:
parent
02c7492f74
commit
6c402c771f
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue