8000 Fixed memory leak (ssl points were not freed on client error) · gavinlwz/esp32_https_server@d3ef3c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit d3ef3c8

Browse files
committed
Fixed memory leak (ssl points were not freed on client error)
1 parent ac4f723 commit d3ef3c8

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

https/HTTPSConnection.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -129,41 +129,41 @@ void HTTPSConnection::closeConnection() {
129129
// Set the connection state to closing. We stay in closing as long as SSL has not been shutdown
130130
// correctly
131131
_connectionState = STATE_CLOSING;
132+
}
132133

133-
// Try to tear down SSL
134-
if (_ssl) {
135-
if(SSL_shutdown(_ssl) == 0) {
136-
// SSL_shutdown will return 1 as soon as the client answered with close notify
137-
// This means we are safe to close the socket
138-
SSL_free(_ssl);
139-
_ssl = NULL;
140-
} else if (_shutdownTS + HTTPS_SHUTDOWN_TIMEOUT < millis()) {
141-
// The timeout has been hit, we force SSL shutdown now by freeing the context
142-
SSL_free(_ssl);
143-
_ssl = NULL;
144-
HTTPS_DLOG("[ERR] SSL_shutdown did not receive close notification from the client");
145-
_connectionState = STATE_ERROR;
146-
}
134+
// Try to tear down SSL while we are in the _shutdownTS timeout period or if an error occurred
135+
if (_ssl) {
136+
if(_connectionState == STATE_ERROR || SSL_shutdown(_ssl) == 0) {
137+
// SSL_shutdown will return 1 as soon as the client answered with close notify
138+
// This means we are safe to close the socket
139+
SSL_free(_ssl);
140+
_ssl = NULL;
141+
} else if (_shutdownTS + HTTPS_SHUTDOWN_TIMEOUT < millis()) {
142+
// The timeout has been hit, we force SSL shutdown now by freeing the context
143+
SSL_free(_ssl);
144+
_ssl = NULL;
145+
HTTPS_DLOG("[ERR] SSL_shutdown did not receive close notification from the client");
146+
_connectionState = STATE_ERROR;
147147
}
148+
}
148149

149-
// If SSL has been brought down, close the socket
150-
if (!_ssl) {
151-
// Tear down the socket
152-
if (_socket >= 0) {
153-
HTTPS_DLOGHEX("[<--] Connection has been closed. fid = ", _socket);
154-
close(_socket);
155-
_socket = -1;
156-
_addrLen = 0;
157-
}
150+
// If SSL has been brought down, close the socket
151+
if (!_ssl) {
152+
// Tear down the socket
153+
if (_socket >= 0) {
154+
HTTPS_DLOGHEX("[<--] Connection has been closed. fid = ", _socket);
155+
close(_socket);
156+
_socket = -1;
157+
_addrLen = 0;
158+
}
158159

159-
if (_connectionState != STATE_ERROR) {
160-
_connectionState = STATE_CLOSED;
161-
}
160+
if (_connectionState != STATE_ERROR) {
161+
_connectionState = STATE_CLOSED;
162+
}
162163

163-
if (_httpHeaders != NULL) {
164-
delete _httpHeaders;
165-
_httpHeaders = NULL;
166-
}
164+
if (_httpHeaders != NULL) {
165+
delete _httpHeaders;
166+
_httpHeaders = NULL;
167167
}
168168
}
169169
}

0 commit comments

Comments
 (0)
0