Skip to content

Commit 0934410

Browse files
committed
Limit file manager string size to avoid stack explosion
1 parent b2fdcd5 commit 0934410

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Firmware/RTK_Everywhere/WebServer.ino

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void getFileList(String &returnText)
217217
returnText += "sdFreeSpace," + freeSpace + ",";
218218

219219
char fileName[50]; // Handle long file names
220-
220+
221221
// Attempt to gain access to the SD card
222222
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
223223
{
@@ -239,6 +239,18 @@ void getFileList(String &returnText)
239239
String fileSize;
240240
stringHumanReadableSize(fileSize, file.fileSize());
241241
returnText += "fmName," + String(fileName) + ",fmSize," + fileSize + ",";
242+
243+
const int maxFiles = 20; //40 is too much
244+
const int fileNameLength = 50;
245+
const int maxStringLength = maxFiles * fileNameLength;
246+
// It is not uncommon to have SD cards with 100+ files on them. String can get huge.
247+
// Here we arbitrarily limit it.
248+
// This could be larger but, left unchecked, it will absolutely explode the stack.
249+
if(returnText.length() > maxStringLength)
250+
{
251+
systemPrintf("Limiting file list to %d characters\r\n", maxStringLength);
252+
break;
253+
}
242254
}
243255
}
244256

0 commit comments

Comments
 (0)