Skip to content

Commit 727f193

Browse files
committedJul 10, 2024
Add check for stepSize and stepMode and move the parameters validation at function entry
1 parent 1168d91 commit 727f193

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed
 

‎src/app/clusters/level-control/level-control.cpp

+25-17
Original file line numberDiff line numberDiff line change
@@ -950,31 +950,30 @@ static void moveHandler(app::CommandHandler * commandObj, const app::ConcreteCom
950950
app::DataModel::Nullable<uint8_t> rate, chip::Optional<BitMask<OptionsBitmap>> optionsMask,
951951
chip::Optional<BitMask<OptionsBitmap>> optionsOverride)
952952
{
953-
EndpointId endpoint = commandPath.mEndpointId;
954-
CommandId commandId = commandPath.mCommandId;
955-
956-
EmberAfLevelControlState * state = getState(endpoint);
957953
Status status;
958-
app::DataModel::Nullable<uint8_t> currentLevel;
959954
uint8_t difference;
955+
EmberAfLevelControlState * state;
956+
app::DataModel::Nullable<uint8_t> currentLevel;
960957

961-
if (state == nullptr)
958+
EndpointId endpoint = commandPath.mEndpointId;
959+
CommandId commandId = commandPath.mCommandId;
960+
// Validate the received rate and moveMode first.
961+
if ((!rate.IsNull() && (rate.Value() == 0)) || moveMode == MoveModeEnum::kUnknownEnumValue)
962962
{
963-
status = Status::Failure;
963+
status = Status::InvalidCommand;
964964
goto send_default_response;
965965
}
966966

967-
if (!shouldExecuteIfOff(endpoint, commandId, optionsMask, optionsOverride))
967+
state = getState(endpoint);
968+
if (state == nullptr)
968969
{
969-
status = Status::Success;
970+
status = Status::Failure;
970971
goto send_default_response;
971972
}
972973

973-
// Always validate the rate parameter received. A rate of 0 is invalid.
974-
if (!rate.IsNull() && (rate.Value() == 0))
974+
if (!shouldExecuteIfOff(endpoint, commandId, optionsMask, optionsOverride))
975975
{
976-
// Providing rate of 0 is not allowed.
977-
status = Status::InvalidCommand;
976+
status = Status::Success;
978977
goto send_default_response;
979978
}
980979

@@ -1092,14 +1091,22 @@ static void stepHandler(app::CommandHandler * commandObj, const app::ConcreteCom
10921091
uint8_t stepSize, app::DataModel::Nullable<uint16_t> transitionTimeDs,
10931092
chip::Optional<BitMask<OptionsBitmap>> optionsMask, chip::Optional<BitMask<OptionsBitmap>> optionsOverride)
10941093
{
1095-
EndpointId endpoint = commandPath.mEndpointId;
1096-
CommandId commandId = commandPath.mCommandId;
1097-
1098-
EmberAfLevelControlState * state = getState(endpoint);
10991094
Status status;
1095+
EmberAfLevelControlState * state;
11001096
app::DataModel::Nullable<uint8_t> currentLevel;
1097+
1098+
EndpointId endpoint = commandPath.mEndpointId;
1099+
CommandId commandId = commandPath.mCommandId;
11011100
uint8_t actualStepSize = stepSize;
11021101

1102+
// Validate the received stepSize and stepMode first.
1103+
if (stepSize == 0 || stepMode == StepModeEnum::kUnknownEnumValue)
1104+
{
1105+
status = Status::InvalidCommand;
1106+
goto send_default_response;
1107+
}
1108+
1109+
state = getState(endpoint);
11031110
if (state == nullptr)
11041111
{
11051112
status = Status::Failure;
@@ -1161,6 +1168,7 @@ static void stepHandler(app::CommandHandler * commandObj, const app::ConcreteCom
11611168
}
11621169
break;
11631170
default:
1171+
// Should never happen as it is verified at function entry.
11641172
status = Status::InvalidCommand;
11651173
goto send_default_response;
11661174
}

0 commit comments

Comments
 (0)