Skip to content

Commit

Permalink
samples: matter: Added nullptr checks after memory allocation
Browse files Browse the repository at this point in the history
In few places there are missing nullptr checks, what can
lead to using not allocated memory.

Signed-off-by: Kamil Kasperczyk <[email protected]>
  • Loading branch information
kkasperczyk-no committed Jan 17, 2025
1 parent 4389cc3 commit 8571f61
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 52 deletions.
1 change: 1 addition & 0 deletions samples/matter/common/src/binding/binding_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Nrf::Matter

void BindingHandler::OnInvokeCommandSucces(BindingData *bindingData)
{
VerifyOrReturn(bindingData != nullptr, LOG_ERR("Invalid binding data"));
LOG_DBG("Binding command applied successfully!");

/* If session was recovered and communication works, reset flag to the initial state. */
Expand Down
114 changes: 66 additions & 48 deletions samples/matter/light_switch/src/shell_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,51 @@ namespace Unicast
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::On::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::On::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

static CHIP_ERROR OffCommandHandler(int argc, char **argv)
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Off::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Off::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

static CHIP_ERROR ToggleCommandHandler(int argc, char **argv)
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}
} /* namespace Unicast */

Expand Down Expand Up @@ -142,42 +151,51 @@ namespace Group
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::On::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::On::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

CHIP_ERROR OffCommandHandler(int argc, char **argv)
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Off::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Off::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

CHIP_ERROR ToggleCommandHandler(int argc, char **argv)
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

} /* namespace Group */
Expand Down
10 changes: 6 additions & 4 deletions samples/matter/thermostat/src/temperature_measurement/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ void TemperatureSensor::InternalMeasurement()
void TemperatureSensor::ExternalMeasurement()
{
Nrf::Matter::BindingHandler::BindingData *data = Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->ClusterId = Clusters::TemperatureMeasurement::Id;
data->EndpointId = mTemperatureMeasurementEndpointId;
data->InvokeCommandFunc = ExternalTemperatureMeasurementReadHandler;
BindingHandler::RunBoundClusterAction(data);
if (data) {
data->ClusterId = Clusters::TemperatureMeasurement::Id;
data->EndpointId = mTemperatureMeasurementEndpointId;
data->InvokeCommandFunc = ExternalTemperatureMeasurementReadHandler;
BindingHandler::RunBoundClusterAction(data);
}
}

void TemperatureSensor::ExternalTemperatureMeasurementReadHandler(const EmberBindingTableEntry &binding,
Expand Down

0 comments on commit 8571f61

Please sign in to comment.