Skip to content

Commit d35e938

Browse files
authored
Fix File::readString to work with binary data (#1030)
Previously, File::readString used a C-style string as an intermediate buffer via the String += operator. This treats a NUL byte as a terminator, making this function work incorrectly if the File contains binary data. This commit switches the function to use String::concat, which doesn't treat NUL bytes any differently (and is a bit faster, because it doesn't need to use strlen).
1 parent 4e16a70 commit d35e938

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

cores/rp2040/FS.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,12 @@ File File::openNextFile() {
192192
String File::readString() {
193193
String ret;
194194
ret.reserve(size() - position());
195-
char temp[256 + 1];
196-
int countRead = readBytes(temp, sizeof(temp) - 1);
197-
while (countRead > 0) {
198-
temp[countRead] = 0;
199-
ret += temp;
200-
countRead = readBytes(temp, sizeof(temp) - 1);
201-
}
195+
uint8_t temp[256];
196+
int countRead;
197+
do {
198+
countRead = read(temp, sizeof(temp));
199+
ret.concat(temp, countRead);
200+
} while (countRead > 0);
202201
return ret;
203202
}
204203

0 commit comments

Comments
 (0)