From 4879891da3f5aad8ca525f97be56b780b7df7af9 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sun, 28 Nov 2021 13:23:38 +0000 Subject: x1000-installer: xf_stream_read_lines bugfixes - Only treat EOF as newline if the line is non-empty to suppress a useless empty line after the end of a file. - Deal with 0- and 1-byte line buffers safely. - Remove whitespace stripping and comment handling which was left over from refactoring. Move it to xf_map_parse() where it belongs. Change-Id: I9f05f4e18ca6dd011ca4cd231f0b5ea37c784778 --- lib/x1000-installer/src/xf_flashmap.c | 4 ++++ lib/x1000-installer/src/xf_stream.c | 19 ++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/x1000-installer/src/xf_flashmap.c b/lib/x1000-installer/src/xf_flashmap.c index 75cd3c5905..2cde0f1c20 100644 --- a/lib/x1000-installer/src/xf_flashmap.c +++ b/lib/x1000-installer/src/xf_flashmap.c @@ -193,6 +193,10 @@ int map_parse_line_cb(int n, char* buf, void* arg) struct map_parse_args* args = arg; + /* skip whitespace */ + while(*buf && isspace(*buf)) + ++buf; + /* ignore comments and blank lines */ if(*buf == '#' || *buf == '\0') return 0; diff --git a/lib/x1000-installer/src/xf_stream.c b/lib/x1000-installer/src/xf_stream.c index 5a0f86123c..b6391b2c8d 100644 --- a/lib/x1000-installer/src/xf_stream.c +++ b/lib/x1000-installer/src/xf_stream.c @@ -159,6 +159,9 @@ int xf_stream_read_lines(struct xf_stream* s, char* buf, size_t bufsz, size_t pos = 0; bool at_eof = false; + if(bufsz <= 1) + return XF_E_LINE_TOO_LONG; + while(!at_eof) { bytes_read = xf_stream_read(s, &buf[pos], bufsz - pos - 1); if(bytes_read < 0) @@ -183,20 +186,14 @@ int xf_stream_read_lines(struct xf_stream* s, char* buf, size_t bufsz, else break; /* read ahead to look for newline */ } else { - endp = &buf[pos]; /* treat EOF as a newline */ + if(startp == &buf[pos]) + break; /* nothing left to do */ + else + endp = &buf[pos]; /* last line missing a newline - + * treat EOF as newline */ } } - /* skip whitespace */ - while(*startp && isspace(*startp)) - ++startp; - - /* ignore blank lines and comment lines */ - if(*startp == '#' || *startp == '\0') { - startp = endp + 1; - continue; - } - rc = callback(n++, startp, arg); if(rc != 0) return rc; -- cgit v1.2.3