Skip to content

Commit db9d872

Browse files
marius-predarestyled-commits
andauthoredNov 19, 2024
[NXP][third_party] Fixed memory leak in BR mDNS code (#36543)
* [NXP][third_party] Fixed memory leak in BR mDNS code This commit fixes potential memory leaks that could happen if multiple resolve of browse operations are done without calling the appropriate stop function to clean up the allocated resources. If using the Matter CLI this issue is not possible as the CLI always stops the previous operation but other processes calling the resolve/browse API might not follow this flow. Also fixed the return value of the StopBrowse function. Signed-off-by: Marius Preda <marius.preda@nxp.com> * Restyled by clang-format --------- Signed-off-by: Marius Preda <marius.preda@nxp.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 6012d99 commit db9d872

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed
 

‎src/platform/nxp/common/DnssdImpl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi
9999
chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context,
100100
intptr_t * browseIdentifier)
101101
{
102-
if (ConnectivityMgr().IsWiFiStationConnected()) //|| ESP32Utils::HasIPv6LinkLocalAddress(ESP32Utils::kDefaultEthernetNetifKey))
102+
if (ConnectivityMgr().IsWiFiStationConnected())
103103
{
104104
ReturnErrorOnFailure(NxpChipDnssdBrowse(type, protocol, addressType, interface, callback, context, browseIdentifier));
105105
}
@@ -119,7 +119,7 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
119119
CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback,
120120
void * context)
121121
{
122-
if (ConnectivityMgr().IsWiFiStationConnected()) //|| ESP32Utils::HasIPv6LinkLocalAddress(ESP32Utils::kDefaultEthernetNetifKey))
122+
if (ConnectivityMgr().IsWiFiStationConnected())
123123
{
124124
ReturnErrorOnFailure(NxpChipDnssdResolve(service, interface, callback, context));
125125
}

‎src/platform/nxp/common/DnssdImplBr.cpp

+23-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <openthread/mdns.h>
2929
#include <openthread/srp_server.h>
3030

31+
#include <DnssdImplBr.h>
32+
3133
using namespace ::chip::DeviceLayer;
3234
using namespace chip::DeviceLayer::Internal;
3335

@@ -363,6 +365,11 @@ CHIP_ERROR NxpChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol,
363365
if (type == nullptr || callback == nullptr)
364366
return CHIP_ERROR_INVALID_ARGUMENT;
365367

368+
if (mBrowseContext != nullptr)
369+
{
370+
NxpChipDnssdStopBrowse(reinterpret_cast<intptr_t>(mBrowseContext));
371+
}
372+
366373
mBrowseContext = Platform::New<mDnsQueryCtx>(context, callback);
367374
VerifyOrReturnError(mBrowseContext != nullptr, CHIP_ERROR_NO_MEMORY);
368375

@@ -412,7 +419,8 @@ CHIP_ERROR NxpChipDnssdStopBrowse(intptr_t browseIdentifier)
412419
// that has been freed in DispatchBrowseEmpty.
413420
if ((true == bBrowseInProgress) && (browseContext))
414421
{
415-
browseContext->error = MapOpenThreadError(otMdnsStopBrowser(thrInstancePtr, &browseContext->mBrowseInfo));
422+
error = otMdnsStopBrowser(thrInstancePtr, &browseContext->mBrowseInfo);
423+
browseContext->error = MapOpenThreadError(error);
416424

417425
// browse context will be freed in DispatchBrowseEmpty
418426
DispatchBrowseEmpty(reinterpret_cast<intptr_t>(browseContext));
@@ -430,6 +438,13 @@ CHIP_ERROR NxpChipDnssdResolve(DnssdService * browseResult, Inet::InterfaceId in
430438

431439
otInstance * thrInstancePtr = ThreadStackMgrImpl().OTInstance();
432440

441+
if (mResolveContext != nullptr)
442+
{
443+
// In case there is an ongoing query and NxpChipDnssdResolveNoLongerNeeded has not been called yet
444+
// free the allocated context and do a proper cleanup of the previous transaction
445+
NxpChipDnssdResolveNoLongerNeeded(mResolveContext->mMdnsService.mName);
446+
}
447+
433448
mResolveContext = Platform::New<mDnsQueryCtx>(context, callback);
434449
VerifyOrReturnError(mResolveContext != nullptr, CHIP_ERROR_NO_MEMORY);
435450

@@ -450,12 +465,16 @@ CHIP_ERROR NxpChipDnssdResolve(DnssdService * browseResult, Inet::InterfaceId in
450465
mResolveContext->mSrvInfo.mServiceInstance = mResolveContext->mMdnsService.mName;
451466
mResolveContext->mSrvInfo.mServiceType = mResolveContext->mServiceType;
452467

453-
return MapOpenThreadError(otMdnsStartSrvResolver(thrInstancePtr, &mResolveContext->mSrvInfo));
468+
error = MapOpenThreadError(otMdnsStartSrvResolver(thrInstancePtr, &mResolveContext->mSrvInfo));
454469
}
455-
else
470+
471+
if (error != CHIP_NO_ERROR)
456472
{
457-
return error;
473+
Platform::Delete<mDnsQueryCtx>(mResolveContext);
474+
mResolveContext = nullptr;
458475
}
476+
477+
return error;
459478
}
460479
void NxpChipDnssdResolveNoLongerNeeded(const char * instanceName)
461480
{

0 commit comments

Comments
 (0)