aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2022-02-02 19:27:42 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2022-02-02 19:27:42 -0500
commitbbc3a15672ff677a569bfcd0597cc385ccdb4d59 (patch)
treeba9ccedff77a52fb91dbd668e0336b9ee5069067
parent25460cecf4eaf231537e68d50d9eebb4e7035c11 (diff)
downloadweekly-bbc3a15672ff677a569bfcd0597cc385ccdb4d59.tar.gz
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
-rw-r--r--record.c17
1 files 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