From 10a4bc6e76815bfe4914abc966b175e2ee82f528 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Thu, 26 May 2022 09:01:29 +0200 Subject: [PATCH] - fixed memory leak in reuse of client connection (related to socket extension buffer) --- src/mms/iso_client/iso_client_connection.c | 60 +++++++++++++--------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/src/mms/iso_client/iso_client_connection.c b/src/mms/iso_client/iso_client_connection.c index 6892c405..c4ca6f52 100644 --- a/src/mms/iso_client/iso_client_connection.c +++ b/src/mms/iso_client/iso_client_connection.c @@ -193,48 +193,63 @@ IsoClientConnection_create(IsoConnectionParameters parameters, IsoIndicationCall static bool sendConnectionRequestMessage(IsoClientConnection self) { + int socketExtensionBufferSize = CONFIG_MMS_MAXIMUM_PDU_SIZE + 1000; + uint8_t* socketExtensionBuffer = NULL; + if (self->cotpConnection) { /* Destroy existing handle set when connection is reused */ if (self->cotpConnection->handleSet) Handleset_destroy(self->cotpConnection->handleSet); self->cotpConnection->handleSet = NULL; + + socketExtensionBuffer = self->cotpConnection->socketExtensionBuffer; } - int socketExtensionBufferSize = CONFIG_MMS_MAXIMUM_PDU_SIZE + 1000; - uint8_t* socketExtensionBuffer = (uint8_t*)GLOBAL_MALLOC(socketExtensionBufferSize); + if (socketExtensionBuffer == NULL) { + socketExtensionBuffer = (uint8_t*)GLOBAL_MALLOC(socketExtensionBufferSize); + } - /* COTP (ISO transport) handshake */ - CotpConnection_init(self->cotpConnection, self->socket, self->receiveBuffer, self->cotpReadBuffer, self->cotpWriteBuffer, - socketExtensionBuffer, socketExtensionBufferSize); + if (socketExtensionBuffer) { + + /* COTP (ISO transport) handshake */ + CotpConnection_init(self->cotpConnection, self->socket, self->receiveBuffer, self->cotpReadBuffer, self->cotpWriteBuffer, + socketExtensionBuffer, socketExtensionBufferSize); #if (CONFIG_MMS_SUPPORT_TLS == 1) - if (self->parameters->tlsConfiguration) { + if (self->parameters->tlsConfiguration) { - TLSConfiguration_setClientMode(self->parameters->tlsConfiguration); + TLSConfiguration_setClientMode(self->parameters->tlsConfiguration); - /* create TLSSocket and start TLS authentication */ - TLSSocket tlsSocket = TLSSocket_create(self->socket, self->parameters->tlsConfiguration, false); + /* create TLSSocket and start TLS authentication */ + TLSSocket tlsSocket = TLSSocket_create(self->socket, self->parameters->tlsConfiguration, false); - if (tlsSocket) - self->cotpConnection->tlsSocket = tlsSocket; - else { + if (tlsSocket) + self->cotpConnection->tlsSocket = tlsSocket; + else { - if (DEBUG_ISO_CLIENT) - printf("ISO_CLIENT: TLS handshake failed!\n"); + if (DEBUG_ISO_CLIENT) + printf("ISO_CLIENT: TLS handshake failed!\n"); - return false; + return false; + } } - } #endif /* (CONFIG_MMS_SUPPORT_TLS == 1) */ - /* COTP (ISO transport) handshake */ - CotpIndication cotpIndication = - CotpConnection_sendConnectionRequestMessage(self->cotpConnection, self->parameters); + /* COTP (ISO transport) handshake */ + CotpIndication cotpIndication = + CotpConnection_sendConnectionRequestMessage(self->cotpConnection, self->parameters); + + if (cotpIndication != COTP_OK) + return false; + else + return true; + } + else { + if (DEBUG_ISO_CLIENT) + printf("ISO_CLIENT: Failed to allocate socket extension buffer\n"); - if (cotpIndication != COTP_OK) return false; - else - return true; + } } static void @@ -755,7 +770,6 @@ IsoClientConnection_close(IsoClientConnection self) } } - void IsoClientConnection_destroy(IsoClientConnection self) {