1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Remove 400b stack allocation from AdvWeb example (#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.
This commit is contained in:
Earle F. Philhower, III 2023-01-07 10:01:56 -08:00 committed by GitHub
parent a76852a991
commit c8dcdede34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,6 +32,7 @@
#include <WiFiClient.h> #include <WiFiClient.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include <StreamString.h>
#ifndef STASSID #ifndef STASSID
#define STASSID "your-ssid" #define STASSID "your-ssid"
@ -47,14 +48,14 @@ const int led = 13;
void handleRoot() { void handleRoot() {
digitalWrite(led, 1); digitalWrite(led, 1);
char temp[400];
int sec = millis() / 1000; int sec = millis() / 1000;
int min = sec / 60; int min = sec / 60;
int hr = min / 60; int hr = min / 60;
snprintf(temp, 400, StreamString temp;
temp.reserve(500); // Preallocate a large chunk to avoid memory fragmentation
"<html>\ temp.printf("\
<html>\
<head>\ <head>\
<meta http-equiv='refresh' content='5'/>\ <meta http-equiv='refresh' content='5'/>\
<title>ESP8266 Demo</title>\ <title>ESP8266 Demo</title>\
@ -68,9 +69,8 @@ void handleRoot() {
<img src=\"/test.svg\" />\ <img src=\"/test.svg\" />\
</body>\ </body>\
</html>", </html>",
hr, min % 60, sec % 60);
hr, min % 60, sec % 60); server.send(200, "text/html", temp.c_str());
server.send(200, "text/html", temp);
digitalWrite(led, 0); digitalWrite(led, 0);
} }