Skip to content

Commit 360cdbd

Browse files
committed
Fix stack overflow
Implement settingsToDefaults(). Reduce large arrays on stack.
1 parent 1ddb2df commit 360cdbd

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

Firmware/RTK_Surveyor/Form.ino

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,19 @@ static void handleFirmwareFileUpload(AsyncWebServerRequest *request, String file
255255
//Events triggered by web sockets
256256
#ifdef COMPILE_WIFI
257257
#ifdef COMPILE_AP
258+
char *settingsCSV; //Push large array onto heap
259+
258260
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)
259261
{
260262
if (type == WS_EVT_CONNECT) {
261-
char settingsCSV[AP_CONFIG_SETTING_SIZE];
262-
memset(settingsCSV, 0, sizeof(settingsCSV));
263+
settingsCSV = (char*)malloc(AP_CONFIG_SETTING_SIZE);
264+
memset(settingsCSV, 0, AP_CONFIG_SETTING_SIZE); //Clear any garbage from settings array
265+
263266
createSettingsString(settingsCSV);
264267
log_d("Sending command: %s\n\r", settingsCSV);
265268
client->text(settingsCSV);
266269
wifiSetState(WIFI_CONNECTED);
270+
free(settingsCSV);
267271
}
268272
else if (type == WS_EVT_DISCONNECT) {
269273
log_d("Websocket client disconnected");
@@ -272,6 +276,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
272276
else if (type == WS_EVT_DATA) {
273277
for (int i = 0; i < len; i++) {
274278
incomingSettings[incomingSettingsSpot++] = data[i];
279+
incomingSettingsSpot %= AP_CONFIG_SETTING_SIZE;
275280
}
276281
timeSinceLastIncomingSetting = millis();
277282
}
@@ -675,9 +680,7 @@ void updateSettingWithValue(const char *settingName, const char* settingValueStr
675680
}
676681
else if (strcmp(settingName, "resetProfile") == 0)
677682
{
678-
//Overwrite our current settings with defaults
679-
Settings tempSettings;
680-
settings = tempSettings;
683+
settingsToDefaults(); //Overwrite our current settings with defaults
681684

682685
recordSystemSettings(); //Overwrite profile file and NVM with these settings
683686

Firmware/RTK_Surveyor/States.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ void updateSystemState()
606606
break;
607607
case (STATE_WIFI_CONFIG):
608608
{
609+
609610
if (incomingSettingsSpot > 0)
610611
{
611612
//Allow for 750ms before we parse buffer for all data to arrive

Firmware/RTK_Surveyor/System.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,9 @@ void createNMEASentence(customNmeaType_e textID, char *nmeaMessage, char *textMe
590590

591591
sprintf(nmeaMessage, "%s%02X", nmeaTxt, CRC);
592592
}
593+
594+
//Reset settings struct to default initializers
595+
void settingsToDefaults()
596+
{
597+
settings = Settings(); //https://stackoverflow.com/questions/15183429/c-completely-erase-or-reset-all-values-of-a-struct
598+
}

Firmware/RTK_Surveyor/WiFi.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void wifiPeriodicallyDisplayIpAddress()
115115
}
116116

117117
//Update the state of the WiFi state machine
118-
void wifiSetState (byte newState)
118+
void wifiSetState(byte newState)
119119
{
120120
if (wifiState == newState)
121121
Serial.print("*");

Firmware/RTK_Surveyor/menuMain.ino

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ void menuUserProfiles()
186186
byte bContinue = getByteChoice(menuTimeout);
187187
if (bContinue == 'y')
188188
{
189-
//Overwrite our current settings with defaults
190-
Settings tempSettings;
191-
settings = tempSettings;
189+
settingsToDefaults(); //Overwrite our current settings with defaults
192190

193191
recordSystemSettings(); //Overwrite profile file and NVM with these settings
194192

@@ -229,8 +227,7 @@ void menuUserProfiles()
229227
//If this is an empty/new profile slot, overwrite our current settings with defaults
230228
if (responseLFS == false && responseSD == false)
231229
{
232-
Settings tempSettings;
233-
settings = tempSettings;
230+
settingsToDefaults();
234231
}
235232

236233
//Get bitmask of active profiles
@@ -281,10 +278,7 @@ void changeProfileNumber(byte newProfileNumber)
281278
if (responseLFS == false && responseSD == false)
282279
{
283280
Serial.println("Default the settings");
284-
//Create a temporary settings struc on the heap (not the stack because it is ~4500 bytes)
285-
Settings *tempSettings = new Settings;
286-
settings = *tempSettings;
287-
delete tempSettings;
281+
settingsToDefaults();
288282
}
289283
}
290284

0 commit comments

Comments
 (0)