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
This commit is contained in:
Érico Nogueira 2022-08-12 05:51:20 -03:00
parent b734660c96
commit cbb98853d1
1 changed files with 3 additions and 2 deletions

5
util.c
View File

@ -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;