Skip to content

Commit c5ad022

Browse files
committed
Update 4 possible crash fix on ws - issue fhessel#89
1 parent d50488c commit c5ad022

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

Diff for: src/HTTPConnection.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ void HTTPConnection::closeConnection() {
139139

140140
if (_wsHandler != nullptr) {
141141
HTTPS_LOGD("Free WS Handler");
142+
_wsHandler->onClose();
142143
delete _wsHandler;
143144
_wsHandler = NULL;
144145
}
@@ -204,9 +205,9 @@ int HTTPConnection::updateBuffer() {
204205
// TODO: If we are in state websocket, we might need to do something here
205206
return 0;
206207
} else {
207-
// An error occured
208+
// An error occurred
208209
_connectionState = STATE_ERROR;
209-
HTTPS_LOGE("An receive error occured, FID=%d", _socket);
210+
HTTPS_LOGE("An receive error occurred, FID=%d, code=%d", _socket, readReturnCode);
210211
closeConnection();
211212
return -1;
212213
}
@@ -254,7 +255,7 @@ size_t HTTPConnection::readBuffer(byte* buffer, size_t length) {
254255

255256
size_t HTTPConnection::pendingBufferSize() {
256257
updateBuffer();
257-
258+
if (isClosed()) return 0;
258259
return _bufferUnusedIdx - _bufferProcessed + pendingByteCount();
259260
}
260261

@@ -596,17 +597,21 @@ void HTTPConnection::loop() {
596597
}
597598

598599
// If the handler has terminated the connection, clean up and close the socket too
599-
if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) {
600-
HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket);
601-
delete _wsHandler;
602-
_wsHandler = nullptr;
603-
_connectionState = STATE_CLOSING;
600+
if (_wsHandler != nullptr){
601+
if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) {
602+
HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket);
603+
delete _wsHandler;
604+
_wsHandler = nullptr;
605+
_connectionState = STATE_CLOSING;
606+
}
607+
}
608+
else {
609+
HTTPS_LOGI("WS closed due to SSL level issue and cleanded up");
604610
}
605611
break;
606612
default:;
607613
}
608614
}
609-
610615
}
611616

612617

Diff for: src/HTTPSConnection.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ size_t HTTPSConnection::writeBuffer(byte* buffer, size_t length) {
109109
}
110110

111111
size_t HTTPSConnection::readBytesToBuffer(byte* buffer, size_t length) {
112-
return SSL_read(_ssl, buffer, length);
112+
size_t ret = SSL_read(_ssl, buffer, length);
113+
if (ret < 0) {
114+
HTTPS_LOGD("SSL_read error: %d", SSL_get_error(_ssl, ret));
115+
}
116+
return ret;
113117
}
114118

115119
size_t HTTPSConnection::pendingByteCount() {

0 commit comments

Comments
 (0)