From bbc3a15672ff677a569bfcd0597cc385ccdb4d59 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 2 Feb 2022 19:27:42 -0500 Subject: Bug fixes: * Allocate memory for the exact record size. BUFSIZ turned out to be insufficient. * Fix a strncmp length inconsistency for matching "## " in the header data --- record.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/record.c b/record.c index cab4db5..3e49cd5 100644 --- a/record.c +++ b/record.c @@ -28,7 +28,7 @@ struct Record *record_parse(const char *content) { char value[255] = {0}; sscanf(next, "## %9[^: ]:%254s[^\n]", key, value); - if (strncmp(next, "## ", 2) != 0) { + if (strncmp(next, "## ", 3) != 0) { break; } if (!strcmp(key, "date")) @@ -62,14 +62,9 @@ struct Record *record_read(FILE **fp) { ssize_t soh, sot, eot; size_t record_size; char task[2] = {0}; - char *buf = calloc(BUFSIZ, sizeof(char)); - - if (!buf) { - return NULL; - } + char *buf; if (!*fp) { - free(buf); return NULL; } @@ -105,10 +100,16 @@ struct Record *record_read(FILE **fp) { // Verify the record is not too small, and contained a start of text marker record_size = eot - soh; if (record_size < 1 && !sot) { - free(buf); return NULL; } + // Allocate enough space for the record + buf = calloc(record_size + 1, sizeof(char)); + if (!buf) { + perror("Unable to allocate record"); + exit(1); + } + // Go back to start of header fseek(*fp, soh, SEEK_SET); // Read the entire record -- cgit