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).
This commit is contained in:
Eli Lipsitz 2022-12-04 15:23:16 -06:00 committed by GitHub
parent 4e16a700c6
commit d35e938c36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -192,13 +192,12 @@ File File::openNextFile() {
String File::readString() {
String ret;
ret.reserve(size() - position());
char temp[256 + 1];
int countRead = readBytes(temp, sizeof(temp) - 1);
while (countRead > 0) {
temp[countRead] = 0;
ret += temp;
countRead = readBytes(temp, sizeof(temp) - 1);
}
uint8_t temp[256];
int countRead;
do {
countRead = read(temp, sizeof(temp));
ret.concat(temp, countRead);
} while (countRead > 0);
return ret;
}