From 2eaf7d3ef05047dd346dacafb268b9bc67f7ba56 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 23 Nov 2019 17:17:54 +0100 Subject: Fix: add missing null-terminator in getline implementation Signed-off-by: David Oberhollenzer --- lib/compat/getline.c | 10 ++++++++++ tests/fstree_from_file.c | 1 + 2 files changed, 11 insertions(+) diff --git a/lib/compat/getline.c b/lib/compat/getline.c index f330c6d..e63c50e 100644 --- a/lib/compat/getline.c +++ b/lib/compat/getline.c @@ -43,6 +43,16 @@ ssize_t getline(char **line, size_t *n, FILE *fp) buffer[len++] = c; } while (c != '\n'); + if (len == cap) { + new = realloc(buffer, cap ? cap * 2 : 32); + if (new == NULL) + return -1; + + buffer = new; + } + + buffer[len] = '\0'; + *line = buffer; *n = len; return len; diff --git a/tests/fstree_from_file.c b/tests/fstree_from_file.c index 4df0a4e..a0b6b2d 100644 --- a/tests/fstree_from_file.c +++ b/tests/fstree_from_file.c @@ -90,6 +90,7 @@ int main(void) assert(n->uid == 2); assert(n->gid == 3); assert(strcmp(n->name, "slink") == 0); + fprintf(stderr, "'%s'\n", n->data.slink_target); assert(strcmp(n->data.slink_target, "slinktarget") == 0); n = n->next; -- cgit v1.2.3