Skip to content

Commit

Permalink
[cli] fix memory leak on TCP init failure
Browse files Browse the repository at this point in the history
This commit fixes potential memory leak for CLI TCP module.

Signed-off-by: Łukasz Duda <[email protected]>
  • Loading branch information
LuDuda committed May 27, 2024
1 parent a54f4c4 commit 34b1a30
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/cli/cli_tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,12 @@ template <> otError TcpExample::Process<Cmd("init")>(Arg aArgs[])
mUseCircularSendBuffer = true;
mUseTls = true;

// mbedtls_debug_set_threshold(0);

otPlatCryptoRandomInit();
mbedtls_x509_crt_init(&mSrvCert);
mbedtls_pk_init(&mPKey);

mbedtls_ssl_init(&mSslContext);
mbedtls_ssl_config_init(&mSslConfig);
mbedtls_ssl_conf_rng(&mSslConfig, Crypto::MbedTls::CryptoSecurePrng, nullptr);
// mbedtls_ssl_conf_dbg(&mSslConfig, MbedTlsDebugOutput, this);
mbedtls_ssl_conf_authmode(&mSslConfig, MBEDTLS_SSL_VERIFY_NONE);
mbedtls_ssl_conf_ciphersuites(&mSslConfig, sCipherSuites);

Expand Down Expand Up @@ -261,6 +257,20 @@ template <> otError TcpExample::Process<Cmd("init")>(Arg aArgs[])
mInitialized = true;

exit:
#if OPENTHREAD_CONFIG_TLS_ENABLE
if ((error != OT_ERROR_NONE) && mUseTls)
{
mbedtls_ssl_config_free(&mSslConfig);
mbedtls_ssl_free(&mSslContext);

mbedtls_pk_free(&mPKey);
mbedtls_x509_crt_free(&mSrvCert);

otTcpCircularSendBufferForceDiscardAll(&mSendBuffer);
OT_UNUSED_VARIABLE(otTcpCircularSendBufferDeinitialize(&mSendBuffer));
}
#endif // OPENTHREAD_CONFIG_TLS_ENABLE

return error;
}

Expand All @@ -286,7 +296,6 @@ template <> otError TcpExample::Process<Cmd("deinit")>(Arg aArgs[])
#if OPENTHREAD_CONFIG_TLS_ENABLE
if (mUseTls)
{
otPlatCryptoRandomDeinit();
mbedtls_ssl_config_free(&mSslConfig);
mbedtls_ssl_free(&mSslContext);

Expand Down

0 comments on commit 34b1a30

Please sign in to comment.