Skip to content

Commit

Permalink
[mdns-mdnssd] ensure allocated DnssdHostRegistration is freed (#2004)
Browse files Browse the repository at this point in the history
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()`.
  • Loading branch information
abtink authored Sep 8, 2023
1 parent b4d6f3b commit 8a280e3
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/mdns/mdns_mdnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DnssdHostRegistration> registration;

VerifyOrExit(mState == Publisher::State::kReady, ret = OTBR_ERROR_INVALID_STATE);

Expand All @@ -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)
Expand All @@ -593,7 +593,7 @@ otbrError PublisherMDnsSd::PublishHostImpl(const std::string &aName,
registration->GetRecordRefMap()[recordRef] = address;
}

AddHostRegistration(std::unique_ptr<DnssdHostRegistration>(registration));
AddHostRegistration(std::move(registration));

exit:
if (error != kDNSServiceErr_NoError || ret != OTBR_ERROR_NONE)
Expand Down

0 comments on commit 8a280e3

Please sign in to comment.