From 16e19181b94656c9ccc4b22de27d1e4d7eba7d4a Mon Sep 17 00:00:00 2001 From: Clemens Kirchgatterer Date: Fri, 9 Feb 2024 15:28:14 +0100 Subject: [PATCH 1/2] ESP8266WebServer - fix possible memory leak in request argument handling (#9076) * fix possible leak of _postArgs array in case of returning early from _parseForm(). * don't use _postArgs member, but instead use a new local variable postArgs instead. * same for _postArgsLen member vs.local postArgsLen. * remove useless NULL pointer check before delete(). * Remove _postArgs member from ESP8266WebServer.h * Remove searching through always empty _postArgs array in ESP8266WebServer-impl.h --- .../src/ESP8266WebServer-impl.h | 8 ------ .../ESP8266WebServer/src/ESP8266WebServer.h | 4 +-- libraries/ESP8266WebServer/src/Parsing-impl.h | 26 +++++++------------ 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h b/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h index 817d323cbf..ddbdbe5f41 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h @@ -590,10 +590,6 @@ const String& ESP8266WebServerTemplate::pathArg(unsigned int i) cons template const String& ESP8266WebServerTemplate::arg(const String& name) const { - for (int j = 0; j < _postArgsLen; ++j) { - if ( _postArgs[j].key == name ) - return _postArgs[j].value; - } for (int i = 0; i < _currentArgCount + _currentArgsHavePlain; ++i) { if ( _currentArgs[i].key == name ) return _currentArgs[i].value; @@ -622,10 +618,6 @@ int ESP8266WebServerTemplate::args() const { template bool ESP8266WebServerTemplate::hasArg(const String& name) const { - for (int j = 0; j < _postArgsLen; ++j) { - if (_postArgs[j].key == name) - return true; - } for (int i = 0; i < _currentArgCount + _currentArgsHavePlain; ++i) { if (_currentArgs[i].key == name) return true; diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index e0dba27a90..397132f161 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -323,8 +323,6 @@ class ESP8266WebServerTemplate RequestArgument* _currentArgs = nullptr; int _currentArgsHavePlain = 0; std::unique_ptr _currentUpload; - int _postArgsLen = 0; - RequestArgument* _postArgs = nullptr; int _headerKeysCount = 0; RequestArgument* _currentHeaders = nullptr; @@ -352,4 +350,4 @@ class ESP8266WebServerTemplate using ESP8266WebServer = esp8266webserver::ESP8266WebServerTemplate; using RequestHandler = esp8266webserver::RequestHandler; -#endif //ESP8266WEBSERVER_H \ No newline at end of file +#endif //ESP8266WEBSERVER_H diff --git a/libraries/ESP8266WebServer/src/Parsing-impl.h b/libraries/ESP8266WebServer/src/Parsing-impl.h index 83762a03c2..238b7b72d3 100644 --- a/libraries/ESP8266WebServer/src/Parsing-impl.h +++ b/libraries/ESP8266WebServer/src/Parsing-impl.h @@ -358,9 +358,8 @@ bool ESP8266WebServerTemplate::_parseForm(ClientType& client, const client.readStringUntil('\n'); //start reading the form if (line == ("--"+boundary)){ - if(_postArgs) delete[] _postArgs; - _postArgs = new RequestArgument[WEBSERVER_MAX_POST_ARGS]; - _postArgsLen = 0; + std::unique_ptr postArgs(new RequestArgument[WEBSERVER_MAX_POST_ARGS]); + int postArgsLen = 0; while(1){ String argName; String argValue; @@ -408,7 +407,7 @@ bool ESP8266WebServerTemplate::_parseForm(ClientType& client, const } DBGWS("PostArg Value: %s\n\n", argValue.c_str()); - RequestArgument& arg = _postArgs[_postArgsLen++]; + RequestArgument& arg = postArgs[postArgsLen++]; arg.key = argName; arg.value = argValue; @@ -488,25 +487,20 @@ bool ESP8266WebServerTemplate::_parseForm(ClientType& client, const } int iarg; - int totalArgs = ((WEBSERVER_MAX_POST_ARGS - _postArgsLen) < _currentArgCount)?(WEBSERVER_MAX_POST_ARGS - _postArgsLen):_currentArgCount; + int totalArgs = ((WEBSERVER_MAX_POST_ARGS - postArgsLen) < _currentArgCount)?(WEBSERVER_MAX_POST_ARGS - postArgsLen):_currentArgCount; for (iarg = 0; iarg < totalArgs; iarg++){ - RequestArgument& arg = _postArgs[_postArgsLen++]; + RequestArgument& arg = postArgs[postArgsLen++]; arg.key = _currentArgs[iarg].key; arg.value = _currentArgs[iarg].value; } - if (_currentArgs) delete[] _currentArgs; - _currentArgs = new RequestArgument[_postArgsLen]; - for (iarg = 0; iarg < _postArgsLen; iarg++){ + delete[] _currentArgs; + _currentArgs = new RequestArgument[postArgsLen]; + for (iarg = 0; iarg < postArgsLen; iarg++){ RequestArgument& arg = _currentArgs[iarg]; - arg.key = _postArgs[iarg].key; - arg.value = _postArgs[iarg].value; + arg.key = postArgs[iarg].key; + arg.value = postArgs[iarg].value; } _currentArgCount = iarg; - if (_postArgs) { - delete[] _postArgs; - _postArgs = nullptr; - _postArgsLen = 0; - } return true; } DBGWS("Error: line: %s\n", line.c_str()); From e6df3455847b177204c220cffe4a57a1270aef36 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Fri, 9 Feb 2024 17:39:38 +0300 Subject: [PATCH 2/2] CI - clang-format-15 (#9085) Stop CI from pulling LLVM repos and using GNUPG keyservers, ubuntu-latest already has clang-format-{13,14,15} Fixes long-standing issue with -style=file:... --- .github/workflows/style-check.yml | 12 ----------- .../PostHttpClient/PostHttpClient.ino | 2 +- .../examples/FSBrowser/FSBrowser.ino | 6 +++--- .../ESP8266WebServer/examples/Graph/Graph.ino | 4 ++-- .../BearSSL_Server/BearSSL_Server.ino | 4 ++-- .../OTA-mDNS-LittleFS/OTA-mDNS-LittleFS.ino | 2 +- .../LittleFS/examples/SpeedTest/SpeedTest.ino | 4 ++-- .../Netdump/examples/Netdump/Netdump.ino | 2 +- .../examples/LowPowerDemo/LowPowerDemo.ino | 8 +++---- .../examples/NTP-TZ-DST/NTP-TZ-DST.ino | 4 ++-- .../lwIP_enc28j60/src/utility/enc28j60.cpp | 4 +++- tests/clang-format-core.yaml | 1 - tests/restyle.sh | 21 +++++++------------ 13 files changed, 29 insertions(+), 45 deletions(-) diff --git a/.github/workflows/style-check.yml b/.github/workflows/style-check.yml index dd492eac72..50a99f2e53 100644 --- a/.github/workflows/style-check.yml +++ b/.github/workflows/style-check.yml @@ -24,20 +24,8 @@ jobs: with: python-version: '3.x' - name: Style check - env: - LLVM_SNAPSHOT_KEY: "6084F3CF814B57C1CF12EFD515CF4D18AF4F7421" run: | - export GNUPGHOME=$(mktemp -d) - gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$LLVM_SNAPSHOT_KEY" - gpg --batch --armor --export "$LLVM_SNAPSHOT_KEY" | \ - sudo tee /etc/apt/trusted.gpg.d/llvm-snapshot.gpg.asc - gpgconf --kill all - rm -r $GNUPGHOME - echo "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-13 main" | \ - sudo tee /etc/apt/sources.list.d/llvm.list sudo apt update - sudo apt install clang-format-13 - pip3 install pyyaml bash ./tests/ci/style_check.sh # Validate orthography diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino index 72d51142c7..bb248d35ed 100644 --- a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino @@ -15,7 +15,7 @@ bin/PostServer/PostServer then put your PC's IP address in SERVER_IP below, port 9080 (instead of default 80): */ -//#define SERVER_IP "10.0.1.7:9080" // PC address with emulation on host +// #define SERVER_IP "10.0.1.7:9080" // PC address with emulation on host #define SERVER_IP "192.168.1.42" #ifndef STASSID diff --git a/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino b/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino index 5a0937df0c..5469f48184 100644 --- a/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino +++ b/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino @@ -25,15 +25,15 @@ // Select the FileSystem by uncommenting one of the lines below -//#define USE_SPIFFS +// #define USE_SPIFFS #define USE_LITTLEFS -//#define USE_SDFS +// #define USE_SDFS // Uncomment the following line to embed a version of the web page in the code // (program code will be larger, but no file will have to be written to the filesystem). // Note: the source file "extras/index_htm.h" must have been generated by "extras/reduce_index.sh" -//#define INCLUDE_FALLBACK_INDEX_HTM +// #define INCLUDE_FALLBACK_INDEX_HTM //////////////////////////////// diff --git a/libraries/ESP8266WebServer/examples/Graph/Graph.ino b/libraries/ESP8266WebServer/examples/Graph/Graph.ino index 0d6f724bae..224eccf0fb 100644 --- a/libraries/ESP8266WebServer/examples/Graph/Graph.ino +++ b/libraries/ESP8266WebServer/examples/Graph/Graph.ino @@ -24,9 +24,9 @@ // Select the FileSystem by uncommenting one of the lines below -//#define USE_SPIFFS +// #define USE_SPIFFS #define USE_LITTLEFS -//#define USE_SDFS +// #define USE_SDFS //////////////////////////////// diff --git a/libraries/ESP8266WiFi/examples/BearSSL_Server/BearSSL_Server.ino b/libraries/ESP8266WiFi/examples/BearSSL_Server/BearSSL_Server.ino index 5795a7362c..598a0adc77 100644 --- a/libraries/ESP8266WiFi/examples/BearSSL_Server/BearSSL_Server.ino +++ b/libraries/ESP8266WiFi/examples/BearSSL_Server/BearSSL_Server.ino @@ -48,7 +48,7 @@ const char *pass = STAPSK; // The HTTPS server BearSSL::WiFiServerSecure server(443); -//#define USE_EC // Enable Elliptic Curve signed cert +// #define USE_EC // Enable Elliptic Curve signed cert #define USING_INSECURE_CERTS_AND_KEYS_AND_CAS 1 #include @@ -58,7 +58,7 @@ BearSSL::WiFiServerSecure server(443); // Caching SSL sessions shortens the length of the SSL handshake. // You can see the performance improvement by looking at the // Network tab of the developer tools of your browser. -//#define DYNAMIC_CACHE // Whether to dynamically allocate the cache. +// #define DYNAMIC_CACHE // Whether to dynamically allocate the cache. #if defined(USE_CACHE) && defined(DYNAMIC_CACHE) // Dynamically allocated cache. diff --git a/libraries/ESP8266mDNS/examples/OTA-mDNS-LittleFS/OTA-mDNS-LittleFS.ino b/libraries/ESP8266mDNS/examples/OTA-mDNS-LittleFS/OTA-mDNS-LittleFS.ino index d94d96077c..78d4aa6c30 100644 --- a/libraries/ESP8266mDNS/examples/OTA-mDNS-LittleFS/OTA-mDNS-LittleFS.ino +++ b/libraries/ESP8266mDNS/examples/OTA-mDNS-LittleFS/OTA-mDNS-LittleFS.ino @@ -47,7 +47,7 @@ const char *ap_default_psk = APPSK; ///< Default PSK. /// @} /// Uncomment the next line for verbose output over UART. -//#define SERIAL_VERBOSE +// #define SERIAL_VERBOSE /** @brief Read WiFi connection information from file system. diff --git a/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino b/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino index 5b0870b37b..c3dd6b2c1f 100644 --- a/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino +++ b/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino @@ -8,8 +8,8 @@ // WARNING: The filesystem will be formatted at the start of the test! #define TESTFS LittleFS -//#define TESTFS SPIFFS -//#define TESTFS SDFS +// #define TESTFS SPIFFS +// #define TESTFS SDFS // How large of a file to test #define TESTSIZEKB 512 diff --git a/libraries/Netdump/examples/Netdump/Netdump.ino b/libraries/Netdump/examples/Netdump/Netdump.ino index 37210a8a0a..35ffc6b493 100644 --- a/libraries/Netdump/examples/Netdump/Netdump.ino +++ b/libraries/Netdump/examples/Netdump/Netdump.ino @@ -4,7 +4,7 @@ #include #include #include -//#include +// #include #include #include diff --git a/libraries/esp8266/examples/LowPowerDemo/LowPowerDemo.ino b/libraries/esp8266/examples/LowPowerDemo/LowPowerDemo.ino index 5d1fb2fb44..27dbb77184 100644 --- a/libraries/esp8266/examples/LowPowerDemo/LowPowerDemo.ino +++ b/libraries/esp8266/examples/LowPowerDemo/LowPowerDemo.ino @@ -42,7 +42,7 @@ #include #include // WiFiState structure details -//#define DEBUG // prints WiFi connection info to serial, uncomment if you want WiFi messages +// #define DEBUG // prints WiFi connection info to serial, uncomment if you want WiFi messages #ifdef DEBUG #define DEBUG_PRINTLN(x) Serial.println(x) #define DEBUG_PRINT(x) Serial.print(x) @@ -56,8 +56,8 @@ // uncomment one of the two lines below for your LED connection (optional) #define LED 5 // D1/GPIO5 external LED for modules with built-in LEDs so it doesn't add amperage -//#define LED 2 // D4/GPIO2 LED for ESP-01,07 modules; D4 is LED_BUILTIN on most other modules -// you can use LED_BUILTIN, but it adds to the measured amperage by 0.3mA to 6mA. +// #define LED 2 // D4/GPIO2 LED for ESP-01,07 modules; D4 is LED_BUILTIN on most other modules +// you can use LED_BUILTIN, but it adds to the measured amperage by 0.3mA to 6mA. ADC_MODE(ADC_VCC); // allows you to monitor the internal VCC level; it varies with WiFi load // don't connect anything to the analog input pin(s)! @@ -72,7 +72,7 @@ IPAddress dns1(0, 0, 0, 0); IPAddress dns2(0, 0, 0, 0); uint32_t timeout = 30E3; // 30 second timeout on the WiFi connection -//#define TESTPOINT // used to track the timing of several test cycles (optional) +// #define TESTPOINT // used to track the timing of several test cycles (optional) #ifdef TESTPOINT #define testPointPin 4 // D2/GPIO4, you can use any pin that supports interrupts #define testPoint_HIGH digitalWrite(testPointPin, HIGH) diff --git a/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino b/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino index 6388c1a9cf..8a9ea0e82c 100644 --- a/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino +++ b/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino @@ -31,11 +31,11 @@ // check for your nearest city in TZ.h // espressif headquarter TZ -//#define MYTZ TZ_Asia_Shanghai +// #define MYTZ TZ_Asia_Shanghai // example for "Not Only Whole Hours" timezones: // Kolkata/Calcutta is shifted by 30mn -//#define MYTZ TZ_Asia_Kolkata +// #define MYTZ TZ_Asia_Kolkata // example of a timezone with a variable Daylight-Saving-Time: // demo: watch automatic time adjustment on Summer/Winter change (DST) diff --git a/libraries/lwIP_enc28j60/src/utility/enc28j60.cpp b/libraries/lwIP_enc28j60/src/utility/enc28j60.cpp index 09f8ae0fe2..0ee3096486 100644 --- a/libraries/lwIP_enc28j60/src/utility/enc28j60.cpp +++ b/libraries/lwIP_enc28j60/src/utility/enc28j60.cpp @@ -398,7 +398,9 @@ bool ENC28J60::reset(void) /* Wait for OST */ PRINTF("waiting for ESTAT_CLKRDY\n"); - while ((readreg(ESTAT) & ESTAT_CLKRDY) == 0) { }; + while ((readreg(ESTAT) & ESTAT_CLKRDY) == 0) + { + }; PRINTF("ESTAT_CLKRDY\n"); setregbank(ERXTX_BANK); diff --git a/tests/clang-format-core.yaml b/tests/clang-format-core.yaml index 5a72d62beb..540037d13e 100644 --- a/tests/clang-format-core.yaml +++ b/tests/clang-format-core.yaml @@ -6,7 +6,6 @@ KeepEmptyLinesAtTheStartOfBlocks: false SpaceAfterTemplateKeyword: false SpaceBeforeInheritanceColon: false SpacesBeforeTrailingComments: 2 -AlignTrailingComments: true AllowShortBlocksOnASingleLine: Empty AllowShortFunctionsOnASingleLine: Empty AllowShortIfStatementsOnASingleLine: false diff --git a/tests/restyle.sh b/tests/restyle.sh index fb3df24dba..ce8e906a76 100755 --- a/tests/restyle.sh +++ b/tests/restyle.sh @@ -7,14 +7,9 @@ root=$(git rev-parse --show-toplevel) test -d ${root}/cores/esp8266 test -d ${root}/libraries -# allow `env CLANG_FORMAT=clang-format-13`, or some other version -# default to v13, latest stable version from https://apt.llvm.org -CLANG_FORMAT=${CLANG_FORMAT:-clang-format-13} - -# TODO: waiting for llvm-14 to allow --style=file: -makeClangFormatStyle() { - python3 -c 'import sys,yaml; sys.stdout.write(yaml.dump(yaml.safe_load(open(sys.argv[1], "r")), default_flow_style=True)); sys.stdout.flush();' $1 -} +# allow `env CLANG_FORMAT=clang-format-N`, or some other version +# default to v15, latest stable version from ubuntu-latest Github Actions image +CLANG_FORMAT=${CLANG_FORMAT:-clang-format-15} ######################################### # 'all' variable should be "cores/esp8266 libraries" @@ -37,14 +32,14 @@ tests cd $root -style=$(makeClangFormatStyle ${root}/tests/clang-format-core.yaml) +style=${root}/tests/clang-format-core.yaml for target in $all; do if [ -d "$target" ]; then find $target \ '(' -name "*.cpp" -o -name "*.c" -o -name "*.h" ')' \ - -exec $CLANG_FORMAT --verbose --style="$style" -i {} \; + -exec $CLANG_FORMAT --verbose --style="file:$style" -i {} \; else - $CLANG_FORMAT --verbose --style="$style" -i $target + $CLANG_FORMAT --verbose --style="file:$style" -i $target fi done @@ -55,12 +50,12 @@ done # exclude=$(git submodule --quiet foreach git rev-parse --show-toplevel | grep libraries) if [ -z $1 ] ; then - style=$(makeClangFormatStyle ${root}/tests/clang-format-arduino.yaml) + style=${root}/tests/clang-format-arduino.yaml find libraries \ -path libraries/ESP8266SdFat -prune -o \ -path libraries/Ethernet -prune -o \ -path libraries/SoftwareSerial -prune -o \ - -name '*.ino' -exec $CLANG_FORMAT --verbose --style="$style" -i {} \; + -name '*.ino' -exec $CLANG_FORMAT --verbose --style="file:$style" -i {} \; fi #########################################