Skip to content

Commit 2bd97ec

Browse files
earlephilhowerhasenradball
authored andcommitted
Remove 400b stack allocation from AdvWeb example (esp8266#8793)
The AdvancedWebServer.ino example allocated a 400 byte char array on the stack which, in the case of the example, will work but in general is a dangerous thing to show new users to try. Instead, use a StreamString to generate the string on the heap.
1 parent 75002c3 commit 2bd97ec

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

libraries/ESP8266WebServer/examples/AdvancedWebServer/AdvancedWebServer.ino

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <WiFiClient.h>
3333
#include <ESP8266WebServer.h>
3434
#include <ESP8266mDNS.h>
35+
#include <StreamString.h>
3536

3637
#ifndef STASSID
3738
#define STASSID "your-ssid"
@@ -47,14 +48,14 @@ const int led = 13;
4748

4849
void handleRoot() {
4950
digitalWrite(led, 1);
50-
char temp[400];
5151
int sec = millis() / 1000;
5252
int min = sec / 60;
5353
int hr = min / 60;
5454

55-
snprintf(temp, 400,
56-
57-
"<html>\
55+
StreamString temp;
56+
temp.reserve(500); // Preallocate a large chunk to avoid memory fragmentation
57+
temp.printf("\
58+
<html>\
5859
<head>\
5960
<meta http-equiv='refresh' content='5'/>\
6061
<title>ESP8266 Demo</title>\
@@ -68,9 +69,8 @@ void handleRoot() {
6869
<img src=\"/test.svg\" />\
6970
</body>\
7071
</html>",
71-
72-
hr, min % 60, sec % 60);
73-
server.send(200, "text/html", temp);
72+
hr, min % 60, sec % 60);
73+
server.send(200, "text/html", temp.c_str());
7474
digitalWrite(led, 0);
7575
}
7676

0 commit comments

Comments
 (0)