mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
Really free stack after last BearSSL obj destroyed (#5185)
The BearSSL second stack, once allocated, was never deallocated. The reference count of the stack pointer never hit 0 due to the initial creation counting as one. Now, check to see if there is only one use_count and if so then delete the stack.
This commit is contained in:
parent
4e3af9795b
commit
270788bedb
@ -91,12 +91,16 @@ WiFiClientSecure::WiFiClientSecure() : WiFiClient() {
|
|||||||
_clear();
|
_clear();
|
||||||
_clearAuthenticationSettings();
|
_clearAuthenticationSettings();
|
||||||
_certStore = nullptr; // Don't want to remove cert store on a clear, should be long lived
|
_certStore = nullptr; // Don't want to remove cert store on a clear, should be long lived
|
||||||
|
_ensureStackAvailable();
|
||||||
|
_local_bearssl_stack = _bearssl_stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WiFiClientSecure::_ensureStackAvailable() {
|
||||||
if (!_bearssl_stack) {
|
if (!_bearssl_stack) {
|
||||||
const int stacksize = 4500; // Empirically determined stack for EC and RSA connections
|
const int stacksize = 4500; // Empirically determined stack for EC and RSA connections
|
||||||
_bearssl_stack = std::shared_ptr<uint8_t>(new uint8_t[stacksize], std::default_delete<uint8_t[]>());
|
_bearssl_stack = std::shared_ptr<uint8_t>(new uint8_t[stacksize], std::default_delete<uint8_t[]>());
|
||||||
br_esp8266_stack_proxy_init(_bearssl_stack.get(), stacksize);
|
br_esp8266_stack_proxy_init(_bearssl_stack.get(), stacksize);
|
||||||
}
|
}
|
||||||
_local_bearssl_stack = _bearssl_stack;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFiClientSecure::~WiFiClientSecure() {
|
WiFiClientSecure::~WiFiClientSecure() {
|
||||||
@ -106,7 +110,11 @@ WiFiClientSecure::~WiFiClientSecure() {
|
|||||||
}
|
}
|
||||||
free(_cipher_list);
|
free(_cipher_list);
|
||||||
_freeSSL();
|
_freeSSL();
|
||||||
_local_bearssl_stack = nullptr; // Potentially delete it if we're the last SSL object
|
_local_bearssl_stack = nullptr;
|
||||||
|
// If there are no other uses than the initial creation, free the stack
|
||||||
|
if (_bearssl_stack.use_count() == 1) {
|
||||||
|
_bearssl_stack = nullptr;
|
||||||
|
}
|
||||||
if (_deleteChainKeyTA) {
|
if (_deleteChainKeyTA) {
|
||||||
delete _ta;
|
delete _ta;
|
||||||
delete _chain;
|
delete _chain;
|
||||||
@ -119,6 +127,8 @@ WiFiClientSecure::WiFiClientSecure(ClientContext* client,
|
|||||||
int iobuf_in_size, int iobuf_out_size, const BearSSLX509List *client_CA_ta) {
|
int iobuf_in_size, int iobuf_out_size, const BearSSLX509List *client_CA_ta) {
|
||||||
_clear();
|
_clear();
|
||||||
_clearAuthenticationSettings();
|
_clearAuthenticationSettings();
|
||||||
|
_ensureStackAvailable();
|
||||||
|
_local_bearssl_stack = _bearssl_stack;
|
||||||
_iobuf_in_size = iobuf_in_size;
|
_iobuf_in_size = iobuf_in_size;
|
||||||
_iobuf_out_size = iobuf_out_size;
|
_iobuf_out_size = iobuf_out_size;
|
||||||
_client = client;
|
_client = client;
|
||||||
@ -136,6 +146,8 @@ WiFiClientSecure::WiFiClientSecure(ClientContext *client,
|
|||||||
int iobuf_in_size, int iobuf_out_size, const BearSSLX509List *client_CA_ta) {
|
int iobuf_in_size, int iobuf_out_size, const BearSSLX509List *client_CA_ta) {
|
||||||
_clear();
|
_clear();
|
||||||
_clearAuthenticationSettings();
|
_clearAuthenticationSettings();
|
||||||
|
_ensureStackAvailable();
|
||||||
|
_local_bearssl_stack = _bearssl_stack;
|
||||||
_iobuf_in_size = iobuf_in_size;
|
_iobuf_in_size = iobuf_in_size;
|
||||||
_iobuf_out_size = iobuf_out_size;
|
_iobuf_out_size = iobuf_out_size;
|
||||||
_client = client;
|
_client = client;
|
||||||
|
@ -229,6 +229,7 @@ class WiFiClientSecure : public WiFiClient {
|
|||||||
private:
|
private:
|
||||||
// Single memory buffer used for BearSSL auxilliary stack, insead of growing main Arduino stack for all apps
|
// Single memory buffer used for BearSSL auxilliary stack, insead of growing main Arduino stack for all apps
|
||||||
static std::shared_ptr<uint8_t> _bearssl_stack;
|
static std::shared_ptr<uint8_t> _bearssl_stack;
|
||||||
|
void _ensureStackAvailable(); // Allocate the stack if necessary
|
||||||
// The local copy, only used to enable a reference count
|
// The local copy, only used to enable a reference count
|
||||||
std::shared_ptr<uint8_t> _local_bearssl_stack;
|
std::shared_ptr<uint8_t> _local_bearssl_stack;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user