summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2021-11-28 13:23:38 +0000
committerAidan MacDonald <amachronic@protonmail.com>2021-11-28 13:55:48 +0000
commit4879891da3f5aad8ca525f97be56b780b7df7af9 (patch)
tree10f15837bcd6d2c7a2029c4962bd3f7aacc86721
parent420d545018dbb21956b4c441b42c7e4803d95a1d (diff)
downloadrockbox-4879891da3f5aad8ca525f97be56b780b7df7af9.tar.gz
rockbox-4879891da3f5aad8ca525f97be56b780b7df7af9.zip
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
-rw-r--r--lib/x1000-installer/src/xf_flashmap.c4
-rw-r--r--lib/x1000-installer/src/xf_stream.c19
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)
193 193
194 struct map_parse_args* args = arg; 194 struct map_parse_args* args = arg;
195 195
196 /* skip whitespace */
197 while(*buf && isspace(*buf))
198 ++buf;
199
196 /* ignore comments and blank lines */ 200 /* ignore comments and blank lines */
197 if(*buf == '#' || *buf == '\0') 201 if(*buf == '#' || *buf == '\0')
198 return 0; 202 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,
159 size_t pos = 0; 159 size_t pos = 0;
160 bool at_eof = false; 160 bool at_eof = false;
161 161
162 if(bufsz <= 1)
163 return XF_E_LINE_TOO_LONG;
164
162 while(!at_eof) { 165 while(!at_eof) {
163 bytes_read = xf_stream_read(s, &buf[pos], bufsz - pos - 1); 166 bytes_read = xf_stream_read(s, &buf[pos], bufsz - pos - 1);
164 if(bytes_read < 0) 167 if(bytes_read < 0)
@@ -183,20 +186,14 @@ int xf_stream_read_lines(struct xf_stream* s, char* buf, size_t bufsz,
183 else 186 else
184 break; /* read ahead to look for newline */ 187 break; /* read ahead to look for newline */
185 } else { 188 } else {
186 endp = &buf[pos]; /* treat EOF as a newline */ 189 if(startp == &buf[pos])
190 break; /* nothing left to do */
191 else
192 endp = &buf[pos]; /* last line missing a newline -
193 * treat EOF as newline */
187 } 194 }
188 } 195 }
189 196
190 /* skip whitespace */
191 while(*startp && isspace(*startp))
192 ++startp;
193
194 /* ignore blank lines and comment lines */
195 if(*startp == '#' || *startp == '\0') {
196 startp = endp + 1;
197 continue;
198 }
199
200 rc = callback(n++, startp, arg); 197 rc = callback(n++, startp, arg);
201 if(rc != 0) 198 if(rc != 0)
202 return rc; 199 return rc;