Skip to content

Commit da48a52

Browse files
authored
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).
1 parent 3c62531 commit da48a52

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

cores/esp8266/FS.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,15 @@ File File::openNextFile() {
173173
return _fakeDir->openFile("r");
174174
}
175175

176-
String File::readString()
177-
{
176+
String File::readString() {
178177
String ret;
179178
ret.reserve(size() - position());
180-
char temp[256+1];
181-
int countRead = readBytes(temp, sizeof(temp)-1);
182-
while (countRead > 0)
183-
{
184-
temp[countRead] = 0;
185-
ret += temp;
186-
countRead = readBytes(temp, sizeof(temp)-1);
187-
}
179+
uint8_t temp[256];
180+
int countRead;
181+
do {
182+
countRead = read(temp, sizeof(temp));
183+
ret.concat((const char*)temp, countRead);
184+
} while (countRead > 0);
188185
return ret;
189186
}
190187

0 commit comments

Comments
 (0)