config: handle EOF properly In the main loop we peek to see what kind of line the next one is. If there are multiple newlines before the end of the file, the eof marker won't be set after we read the last line with data and we'll try to peek again. This peek will return LF (as it pretends that we have a newline at EOF so other function don't need any special handling). Fix cfg_getchar so it doesn't try to read past the last character in the file and config_parse so it considers LF as EOF on peek (as we're ignoring spaces) and sets the reader's EOF flag to exit the parsing loop.
diff --git a/src/config_file.c b/src/config_file.c
index 1358719..2374b38 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -499,7 +499,8 @@ static int cfg_getchar(diskfile_backend *cfg_file, int flags)
assert(cfg_file->reader.read_ptr);
do c = cfg_getchar_raw(cfg_file);
- while (skip_whitespace && isspace(c));
+ while (skip_whitespace && isspace(c) &&
+ !cfg_file->reader.eof);
if (skip_comments && (c == '#' || c == ';')) {
do c = cfg_getchar_raw(cfg_file);
@@ -844,7 +845,8 @@ static int config_parse(diskfile_backend *cfg_file)
c = cfg_peek(cfg_file, SKIP_WHITESPACE);
switch (c) {
- case '\0': /* We've arrived at the end of the file */
+ case '\n': /* EOF when peeking, set EOF in the reader to exit the loop */
+ cfg_file->reader.eof = 1;
break;
case '[': /* section header, new section begins */