Skip to content

WiFiClientSecure memory leak in SSLContext::connect #3428

Closed
@Jeroen88

Description

@Jeroen88

Basic Infos

Hardware

Hardware: NodeMCU 1.0 (ESP-12E Module)
Core Version: 2.1.0-rc2 and latest GitHub version

Description

Memory is leaking (some 24 bytes, depending on hostName size) every time I use ESP8266HTTPClient to GET data from a secure site.

Drilled down the problem to SSLContext::connect(ClientContext* ctx, const char* hostName, uint32_t timeout_ms). In this function memory is allocated using the function ssl_ext_set_host_name(ext, hostName); this memory is, however in my opinion, never freed.

Added just before the end of the connect function: ssl_ext_set_host_name(ext, nullptr); Now no more memory is leaking.

Complete function:

void connect(ClientContext* ctx, const char* hostName, uint32_t timeout_ms)
{
    SSL_EXTENSIONS* ext = ssl_ext_new();
    ssl_ext_set_host_name(ext, hostName);
    ssl_ext_set_max_fragment_size(ext, 4096);
    if (_ssl) {
        /* Creating a new TLS session on top of a new TCP connection.
           ssl_free will want to send a close notify alert, but the old TCP connection
           is already gone at this point, so reset s_io_ctx. */
        s_io_ctx = nullptr;
        ssl_free(_ssl);
        _available = 0;
        _read_ptr = nullptr;
    }
    s_io_ctx = ctx;
    _ssl = ssl_client_new(_ssl_ctx, 0, nullptr, 0, ext);
    uint32_t t = millis();

    while (millis() - t < timeout_ms && ssl_handshake_status(_ssl) != SSL_OK) {
        uint8_t* data;
        int rc = ssl_read(_ssl, &data);
        if (rc < SSL_OK) {
            break;
        }
    }
    ssl_ext_set_host_name(ext, nullptr); // THIS FUNCTION FREEs the host_name
}

Settings in IDE

Module: NodeMCU 1.0 (ESP-12E Module)
Flash Size: 4MB/1MB
CPU Frequency: 80Mhz
Flash Mode: qio
Flash Frequency: 40Mhz
Upload Using: OTA / SERIAL
Reset Method: ?ck / nodemcu?

Sketch

#include <Arduino.h>

void setup() {

}

void loop() {

}

Debug Messages

messages here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions