mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-07 06:01:35 +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:
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user