From 8a280e3516641f707324bbc83059736d02f68955 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 8 Sep 2023 14:10:17 -0700 Subject: [PATCH] [mdns-mdnssd] ensure allocated `DnssdHostRegistration` is freed (#2004) This commit changes the `PublisherMDnsSd::PublishHostImpl` so that the allocated `DnssdHostRegistration` instance is tracked as a `std::unique_ptr`. This ensures that the allocated instance is correctly freed if any steps after the allocation fail before we get to `AddHostRegistration()`. --- src/mdns/mdns_mdnssd.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/mdns/mdns_mdnssd.cpp b/src/mdns/mdns_mdnssd.cpp index 43f11f14d85..227e8caccbf 100644 --- a/src/mdns/mdns_mdnssd.cpp +++ b/src/mdns/mdns_mdnssd.cpp @@ -560,10 +560,10 @@ otbrError PublisherMDnsSd::PublishHostImpl(const std::string &aName, const AddressList &aAddresses, ResultCallback &&aCallback) { - otbrError ret = OTBR_ERROR_NONE; - int error = 0; - std::string fullName; - DnssdHostRegistration *registration; + otbrError ret = OTBR_ERROR_NONE; + int error = 0; + std::string fullName; + std::unique_ptr registration; VerifyOrExit(mState == Publisher::State::kReady, ret = OTBR_ERROR_INVALID_STATE); @@ -579,7 +579,7 @@ otbrError PublisherMDnsSd::PublishHostImpl(const std::string &aName, otbrLogDebug("Created new DNSServiceRef for hosts: %p", mHostsRef); } - registration = new DnssdHostRegistration(aName, aAddresses, std::move(aCallback), mHostsRef, this); + registration.reset(new DnssdHostRegistration(aName, aAddresses, std::move(aCallback), mHostsRef, this)); otbrLogInfo("Registering new host %s", aName.c_str()); for (const auto &address : aAddresses) @@ -593,7 +593,7 @@ otbrError PublisherMDnsSd::PublishHostImpl(const std::string &aName, registration->GetRecordRefMap()[recordRef] = address; } - AddHostRegistration(std::unique_ptr(registration)); + AddHostRegistration(std::move(registration)); exit: if (error != kDNSServiceErr_NoError || ret != OTBR_ERROR_NONE)