Skip to content

Commit

Permalink
Fix File::readString to work with binary data (#8742)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
elipsitz committed Dec 6, 2022
1 parent 3c62531 commit da48a52
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,15 @@ File File::openNextFile() {
return _fakeDir->openFile("r");
}

String File::readString()
{
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((const char*)temp, countRead);
} while (countRead > 0);
return ret;
}

Expand Down

0 comments on commit da48a52

Please sign in to comment.