1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-08 17:02:26 +03:00

Fix File::readString to work with binary data (#8742)

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-06 13:56:49 -06:00
committed by GitHub
parent 3c6253109d
commit da48a52d7a

View File

@ -173,18 +173,15 @@ File File::openNextFile() {
return _fakeDir->openFile("r"); return _fakeDir->openFile("r");
} }
String File::readString() String File::readString() {
{
String ret; String ret;
ret.reserve(size() - position()); ret.reserve(size() - position());
char temp[256+1]; uint8_t temp[256];
int countRead = readBytes(temp, sizeof(temp)-1); int countRead;
while (countRead > 0) do {
{ countRead = read(temp, sizeof(temp));
temp[countRead] = 0; ret.concat((const char*)temp, countRead);
ret += temp; } while (countRead > 0);
countRead = readBytes(temp, sizeof(temp)-1);
}
return ret; return ret;
} }