Fix leak in TlsClient object.
sckClientOpen() is the most likely part of this code to error so move it up above SSL session creation to reduce the chance of a leak.
@@ -281,7 +281,11 @@ tlsClientOpen(TlsClient *this)
TRY_BEGIN()
{
- // Create internal TLS session
+ // Open the socket session first since this is mostly likely to fail
+ SocketSession *socketSession = sckClientOpen(this->socketClient);
+
+ // Create internal TLS session. If there is a failure before the TlsSession object is created there may be a leak
+ // of the TLS session but this is likely to result in program termination so it doesn't seem worth coding for.
cryptoError((session = SSL_new(this->context)) == NULL, "unable to create TLS session");
// Set server host name used for validation
@@ -290,11 +294,10 @@ tlsClientOpen(TlsClient *this)
"unable to set TLS host name");
// Create the TLS session
- result = tlsSessionNew(session, sckClientOpen(this->socketClient), this->timeout);
+ result = tlsSessionNew(session, socketSession, this->timeout);
}
CATCH_ANY()
{
- tlsSessionFree(result);
result = NULL;
// Retry if wait time has not expired
