From 9f092c5adde2853087a44f656a93430c98015fe4 Mon Sep 17 00:00:00 2001 From: erliangqi Date: Sun, 29 Sep 2024 16:51:56 +0800 Subject: [PATCH] Abnormal branch did not use SoftBusMutexDestroy to free memory Signed-off-by: erliangqi --- .../udp_negotiation/src/trans_udp_negotiation.c | 12 ++++++++++-- .../include/client_trans_tcp_direct_listener.h | 1 + .../src/client_trans_tcp_direct_listener.c | 7 +++++++ .../tcp_direct/src/client_trans_tcp_direct_manager.c | 9 ++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/core/transmission/trans_channel/udp_negotiation/src/trans_udp_negotiation.c b/core/transmission/trans_channel/udp_negotiation/src/trans_udp_negotiation.c index ab90f3169..e650161a2 100644 --- a/core/transmission/trans_channel/udp_negotiation/src/trans_udp_negotiation.c +++ b/core/transmission/trans_channel/udp_negotiation/src/trans_udp_negotiation.c @@ -1156,7 +1156,11 @@ int32_t TransUdpChannelInit(IServerChannelCallBack *callback) TRANS_INIT, "g_udpNegLock init failed."); ret = TransUdpChannelMgrInit(); - TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "trans udp channel manager init failed."); + if (ret != SOFTBUS_OK) { + TRANS_LOGE(TRANS_CTRL, "trans udp channel manager init failed."); + (void)SoftBusMutexDestroy(&g_udpNegLock); + return ret; + } AuthTransListener transUdpCb = { .onDataReceived = UdpModuleCb, @@ -1165,7 +1169,11 @@ int32_t TransUdpChannelInit(IServerChannelCallBack *callback) }; ret = RegAuthTransListener(MODULE_UDP_INFO, &transUdpCb); - TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "register udp callback to auth failed."); + if (ret != SOFTBUS_OK) { + TRANS_LOGE(TRANS_CTRL, "register udp callback to auth failed."); + (void)SoftBusMutexDestroy(&g_udpNegLock); + return ret; + } TRANS_LOGI(TRANS_INIT, "server trans udp channel init success."); return SOFTBUS_OK; diff --git a/sdk/transmission/trans_channel/tcp_direct/include/client_trans_tcp_direct_listener.h b/sdk/transmission/trans_channel/tcp_direct/include/client_trans_tcp_direct_listener.h index 31ae099fb..4d85730dc 100644 --- a/sdk/transmission/trans_channel/tcp_direct/include/client_trans_tcp_direct_listener.h +++ b/sdk/transmission/trans_channel/tcp_direct/include/client_trans_tcp_direct_listener.h @@ -26,6 +26,7 @@ int32_t TransTdcCreateListener(int32_t fd); void TransTdcReleaseFd(int32_t fd); int32_t TransTdcStopRead(int32_t fd); void TransTdcCloseFd(int32_t fd); +void TdcLockDeinit(void); #ifdef __cplusplus } diff --git a/sdk/transmission/trans_channel/tcp_direct/src/client_trans_tcp_direct_listener.c b/sdk/transmission/trans_channel/tcp_direct/src/client_trans_tcp_direct_listener.c index 2d6aed12a..6a01e641a 100644 --- a/sdk/transmission/trans_channel/tcp_direct/src/client_trans_tcp_direct_listener.c +++ b/sdk/transmission/trans_channel/tcp_direct/src/client_trans_tcp_direct_listener.c @@ -49,6 +49,13 @@ static void TdcLockInit(void) atomic_store_explicit(&(g_lock.lockInit), true, memory_order_release); } } + +void TdcLockDeinit(void) +{ + g_lock.lockInit = false; + (void)SoftBusMutexDestroy(&g_lock.lock); +} + static int32_t ClientTdcOnConnectEvent(ListenerModule module, int cfd, const ConnectOption *clientAddr) { diff --git a/sdk/transmission/trans_channel/tcp_direct/src/client_trans_tcp_direct_manager.c b/sdk/transmission/trans_channel/tcp_direct/src/client_trans_tcp_direct_manager.c index e9521f168..1580373df 100644 --- a/sdk/transmission/trans_channel/tcp_direct/src/client_trans_tcp_direct_manager.c +++ b/sdk/transmission/trans_channel/tcp_direct/src/client_trans_tcp_direct_manager.c @@ -155,7 +155,7 @@ void TransTdcCloseChannel(int32_t channelId) TransTdcReleaseFd(item->detail.fd); item->detail.needRelease = true; if (item->detail.fdRefCnt <= 0) { - SoftBusMutexDestroy(&(item->detail.fdLock)); + (void)SoftBusMutexDestroy(&(item->detail.fdLock)); ListDelete(&item->node); SoftBusFree(item); item = NULL; @@ -190,11 +190,13 @@ static TcpDirectChannelInfo *TransGetNewTcpChannel(const ChannelInfo *channel) return NULL; } if (memcpy_s(item->detail.sessionKey, SESSION_KEY_LENGTH, channel->sessionKey, SESSION_KEY_LENGTH) != EOK) { + (void)SoftBusMutexDestroy(&(item->detail.fdLock)); SoftBusFree(item); TRANS_LOGE(TRANS_SDK, "sessionKey copy failed"); return NULL; } if (strcpy_s(item->detail.myIp, IP_LEN, channel->myIp) != EOK) { + (void)SoftBusMutexDestroy(&(item->detail.fdLock)); SoftBusFree(item); TRANS_LOGE(TRANS_SDK, "myIp copy failed"); return NULL; @@ -244,7 +246,7 @@ static void TransTdcDelChannelInfo(int32_t channelId, int32_t errCode) TransTdcReleaseFd(item->detail.fd); } ListDelete(&item->node); - SoftBusMutexDestroy(&(item->detail.fdLock)); + (void)SoftBusMutexDestroy(&(item->detail.fdLock)); SoftBusFree(item); item = NULL; (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock); @@ -392,6 +394,7 @@ void TransTdcManagerDeinit(void) DestroySoftBusList(g_tcpDirectChannelInfoList); g_tcpDirectChannelInfoList = NULL; PendingDeinit(PENDING_TYPE_DIRECT); + TdcLockDeinit(); } int32_t ClientTransTdcOnChannelOpenFailed(int32_t channelId, int32_t errCode) @@ -468,7 +471,7 @@ void TransUpdateFdState(int32_t channelId) if (item->channelId == channelId) { item->detail.fdRefCnt--; if (item->detail.needRelease && item->detail.fdRefCnt <= 0) { - SoftBusMutexDestroy(&(item->detail.fdLock)); + (void)SoftBusMutexDestroy(&(item->detail.fdLock)); ListDelete(&item->node); SoftBusFree(item); item = NULL;