Skip to content

Commit

Permalink
Stop crashing in Low Power cluster Sleep command if there's no delega…
Browse files Browse the repository at this point in the history
…te. (#19494)

We were forgetting to return after sending the error response, so
ended up dereferencing the null delegate.

Fixes #19481
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Jul 13, 2022
1 parent e5b9661 commit 1203500
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/app/clusters/low-power-server/low-power-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <app/ConcreteCommandPath.h>
#include <app/util/af.h>
#include <platform/CHIPDeviceConfig.h>
#include <protocols/interaction_model/StatusCode.h>

using namespace chip;
using namespace chip::app::Clusters::LowPower;
Expand Down Expand Up @@ -87,22 +88,23 @@ void SetDefaultDelegate(EndpointId endpoint, Delegate * delegate)
bool emberAfLowPowerClusterSleepCallback(app::CommandHandler * command, const app::ConcreteCommandPath & commandPath,
const Commands::Sleep::DecodableType & commandData)
{
CHIP_ERROR err = CHIP_NO_ERROR;
using Protocols::InteractionModel::Status;

EndpointId endpoint = commandPath.mEndpointId;

Delegate * delegate = GetDelegate(endpoint);
VerifyOrExit(isDelegateNull(delegate, endpoint) != true, err = CHIP_ERROR_INCORRECT_STATE);

exit:
if (err != CHIP_NO_ERROR)
Status status;
if (isDelegateNull(delegate, endpoint))
{
ChipLogError(Zcl, "emberAfLowPowerClusterSleepCallback error: %s", err.AsString());
emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_FAILURE);
ChipLogError(Zcl, "emberAfLowPowerClusterSleepCallback: no delegate");
status = Status::Failure;
}

bool success = delegate->HandleSleep();
EmberAfStatus status = success ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE;
emberAfSendImmediateDefaultResponse(status);
else
{
bool success = delegate->HandleSleep();
status = success ? Status::Success : Status::Failure;
}
command->AddStatus(commandPath, status);
return true;
}

Expand Down

0 comments on commit 1203500

Please sign in to comment.