Skip to content

Commit e292d01

Browse files
kkasperczyk-noArekBalysNordic
authored andcommitted
samples: matter: Added nullptr checks after memory allocation
In few places there are missing nullptr checks, what can lead to using not allocated memory. Signed-off-by: Kamil Kasperczyk <[email protected]>
1 parent d48cbae commit e292d01

File tree

3 files changed

+73
-52
lines changed

3 files changed

+73
-52
lines changed

samples/matter/common/src/binding/binding_handler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace Nrf::Matter
3131

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

3637
/* If session was recovered and communication works, reset flag to the initial state. */

samples/matter/light_switch/src/shell_commands.cpp

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,51 @@ namespace Unicast
6767
{
6868
Nrf::Matter::BindingHandler::BindingData *data =
6969
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
70-
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
71-
data->CommandId = Clusters::OnOff::Commands::On::Id;
72-
data->ClusterId = Clusters::OnOff::Id;
73-
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
74-
data->IsGroup.SetValue(false);
75-
76-
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
77-
return CHIP_NO_ERROR;
70+
if (data) {
71+
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
72+
data->CommandId = Clusters::OnOff::Commands::On::Id;
73+
data->ClusterId = Clusters::OnOff::Id;
74+
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
75+
data->IsGroup.SetValue(false);
76+
77+
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
78+
return CHIP_NO_ERROR;
79+
}
80+
return CHIP_ERROR_NO_MEMORY;
7881
}
7982

8083
static CHIP_ERROR OffCommandHandler(int argc, char **argv)
8184
{
8285
Nrf::Matter::BindingHandler::BindingData *data =
8386
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
84-
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
85-
data->CommandId = Clusters::OnOff::Commands::Off::Id;
86-
data->ClusterId = Clusters::OnOff::Id;
87-
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
88-
data->IsGroup.SetValue(false);
89-
90-
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
91-
return CHIP_NO_ERROR;
87+
if (data) {
88+
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
89+
data->CommandId = Clusters::OnOff::Commands::Off::Id;
90+
data->ClusterId = Clusters::OnOff::Id;
91+
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
92+
data->IsGroup.SetValue(false);
93+
94+
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
95+
return CHIP_NO_ERROR;
96+
}
97+
return CHIP_ERROR_NO_MEMORY;
9298
}
9399

94100
static CHIP_ERROR ToggleCommandHandler(int argc, char **argv)
95101
{
96102
Nrf::Matter::BindingHandler::BindingData *data =
97103
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
98-
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
99-
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
100-
data->ClusterId = Clusters::OnOff::Id;
101-
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
102-
data->IsGroup.SetValue(false);
103-
104-
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
105-
return CHIP_NO_ERROR;
104+
if (data) {
105+
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
106+
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
107+
data->ClusterId = Clusters::OnOff::Id;
108+
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
109+
data->IsGroup.SetValue(false);
110+
111+
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
112+
return CHIP_NO_ERROR;
113+
}
114+
return CHIP_ERROR_NO_MEMORY;
106115
}
107116
} /* namespace Unicast */
108117

@@ -142,42 +151,51 @@ namespace Group
142151
{
143152
Nrf::Matter::BindingHandler::BindingData *data =
144153
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
145-
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
146-
data->CommandId = Clusters::OnOff::Commands::On::Id;
147-
data->ClusterId = Clusters::OnOff::Id;
148-
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
149-
data->IsGroup.SetValue(true);
150-
151-
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
152-
return CHIP_NO_ERROR;
154+
if (data) {
155+
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
156+
data->CommandId = Clusters::OnOff::Commands::On::Id;
157+
data->ClusterId = Clusters::OnOff::Id;
158+
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
159+
data->IsGroup.SetValue(true);
160+
161+
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
162+
return CHIP_NO_ERROR;
163+
}
164+
return CHIP_ERROR_NO_MEMORY;
153165
}
154166

155167
CHIP_ERROR OffCommandHandler(int argc, char **argv)
156168
{
157169
Nrf::Matter::BindingHandler::BindingData *data =
158170
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
159-
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
160-
data->CommandId = Clusters::OnOff::Commands::Off::Id;
161-
data->ClusterId = Clusters::OnOff::Id;
162-
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
163-
data->IsGroup.SetValue(true);
164-
165-
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
166-
return CHIP_NO_ERROR;
171+
if (data) {
172+
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
173+
data->CommandId = Clusters::OnOff::Commands::Off::Id;
174+
data->ClusterId = Clusters::OnOff::Id;
175+
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
176+
data->IsGroup.SetValue(true);
177+
178+
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
179+
return CHIP_NO_ERROR;
180+
}
181+
return CHIP_ERROR_NO_MEMORY;
167182
}
168183

169184
CHIP_ERROR ToggleCommandHandler(int argc, char **argv)
170185
{
171186
Nrf::Matter::BindingHandler::BindingData *data =
172187
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
173-
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
174-
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
175-
data->ClusterId = Clusters::OnOff::Id;
176-
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
177-
data->IsGroup.SetValue(true);
178-
179-
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
180-
return CHIP_NO_ERROR;
188+
if (data) {
189+
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
190+
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
191+
data->ClusterId = Clusters::OnOff::Id;
192+
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
193+
data->IsGroup.SetValue(true);
194+
195+
Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
196+
return CHIP_NO_ERROR;
197+
}
198+
return CHIP_ERROR_NO_MEMORY;
181199
}
182200

183201
} /* namespace Group */

samples/matter/thermostat/src/temperature_measurement/sensor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ void TemperatureSensor::InternalMeasurement()
8585
void TemperatureSensor::ExternalMeasurement()
8686
{
8787
Nrf::Matter::BindingHandler::BindingData *data = Platform::New<Nrf::Matter::BindingHandler::BindingData>();
88-
data->ClusterId = Clusters::TemperatureMeasurement::Id;
89-
data->EndpointId = mTemperatureMeasurementEndpointId;
90-
data->InvokeCommandFunc = ExternalTemperatureMeasurementReadHandler;
91-
BindingHandler::RunBoundClusterAction(data);
88+
if (data) {
89+
data->ClusterId = Clusters::TemperatureMeasurement::Id;
90+
data->EndpointId = mTemperatureMeasurementEndpointId;
91+
data->InvokeCommandFunc = ExternalTemperatureMeasurementReadHandler;
92+
BindingHandler::RunBoundClusterAction(data);
93+
}
9294
}
9395

9496
void TemperatureSensor::ExternalTemperatureMeasurementReadHandler(const EmberBindingTableEntry &binding,

0 commit comments

Comments
 (0)