[srp-server] free aHost in CommitSrpUpdate() in case of error (#6754)

This commit ensures that in `Srp::Server::CommitSrpUpdate()` the
`aHost` parameter is correctly freed in case of an error and an early
jump to exit (e.g., from `SuccessOrExit(aError)` at the start of this
method). The `aHost` is now freed at the `exit` label and a boolean
`shouldFreeHost` tracks whether or not it needs to be freed. The
boolean variable is set to `false` when  the ownership of `aHost` is
taken over (e.g., from the call to `AddHost(aHost)`).
This commit is contained in:
Abtin Keshavarzian
2021-06-21 18:08:54 -07:00
committed by GitHub
parent e086cf5c88
commit a662c32eb0
2 changed files with 12 additions and 9 deletions
+11 -8
View File
@@ -205,10 +205,10 @@ const Server::Host *Server::GetNextHost(const Server::Host *aHost)
// This method adds a SRP service host and takes ownership of it.
// The caller MUST make sure that there is no existing host with the same hostname.
void Server::AddHost(Host *aHost)
void Server::AddHost(Host &aHost)
{
OT_ASSERT(mHosts.FindMatching(aHost->GetFullName()) == nullptr);
IgnoreError(mHosts.Add(*aHost));
OT_ASSERT(mHosts.FindMatching(aHost.GetFullName()) == nullptr);
IgnoreError(mHosts.Add(aHost));
}
void Server::RemoveHost(Host *aHost, bool aRetainName, bool aNotifyServiceHandler)
@@ -330,6 +330,7 @@ void Server::CommitSrpUpdate(Error aError,
uint32_t hostKeyLease;
uint32_t grantedLease;
uint32_t grantedKeyLease;
bool shouldFreeHost = true;
SuccessOrExit(aError);
@@ -361,8 +362,6 @@ void Server::CommitSrpUpdate(Error aError,
existingHost->RemoveService(service, /* aRetainName */ true, /* aNotifyServiceHandler */ false);
}
}
aHost.Free();
}
else if (existingHost != nullptr)
{
@@ -391,13 +390,12 @@ void Server::CommitSrpUpdate(Error aError,
newService->GetFullName());
}
}
aHost.Free();
}
else
{
otLogInfoSrp("[server] add new host %s", aHost.GetFullName());
AddHost(&aHost);
AddHost(aHost);
shouldFreeHost = false;
#if OPENTHREAD_CONFIG_SRP_SERVER_PORT_SWITCH_ENABLE
if (!mHasRegisteredAnyService)
{
@@ -422,6 +420,11 @@ exit:
{
SendResponse(aDnsHeader, ErrorToDnsResponseCode(aError), aMessageInfo);
}
if (shouldFreeHost)
{
aHost.Free();
}
}
void Server::Start(void)
+1 -1
View File
@@ -637,7 +637,7 @@ private:
const Service *FindService(const char *aFullName) const;
void HandleUpdate(const Dns::UpdateHeader &aDnsHeader, Host *aHost, const Ip6::MessageInfo &aMessageInfo);
void AddHost(Host *aHost);
void AddHost(Host &aHost);
void RemoveHost(Host *aHost, bool aRetainName, bool aNotifyServiceHandler);
bool HasNameConflictsWith(Host &aHost) const;
void SendResponse(const Dns::UpdateHeader & aHeader,