From cbb98853d130a5b3284fe54c3ddec259031310bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Nogueira?= Date: Fri, 12 Aug 2022 05:51:20 -0300 Subject: [PATCH] Fix bug in entry reading. - deal with empty lines correctly: single letter options that don't get a newline were being missed - entirely empty lines (empty line without a newline) weren't being used correctly - fix UB from not setting line=NULL - use the delim parameter instead of hardcoding for newlines --- util.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/util.c b/util.c index b041103..dcc6768 100644 --- a/util.c +++ b/util.c @@ -9,12 +9,13 @@ void read_entries_from_stream(struct str_array *a, int delim, FILE *input) size_t tmp = 0; ssize_t n; while ((n = getdelim(&line, &tmp, delim, input)) >= 0) { - if (n == 1) { + if (n == 0 || n == 1 && line[0] == delim) { /* don't match empty line */ free(line); + line = NULL; continue; } - if (line[n-1] == '\n') line[n-1] = 0; + if (line[n-1] == delim) line[n-1] = '\0'; add_entry(a, line); line = NULL;