Skip to content

Commit 3223a71

Browse files
Add check for stepSize and stepMode and move the parameters validation at function entry
1 parent 20d1595 commit 3223a71

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
@@ -940,31 +940,30 @@ static void moveHandler(app::CommandHandler * commandObj, const app::ConcreteCom
940940
app::DataModel::Nullable<uint8_t> rate, chip::Optional<BitMask<OptionsBitmap>> optionsMask,
941941
chip::Optional<BitMask<OptionsBitmap>> optionsOverride)
942942
{
943-
EndpointId endpoint = commandPath.mEndpointId;
944-
CommandId commandId = commandPath.mCommandId;
945-
946-
EmberAfLevelControlState * state = getState(endpoint);
947943
Status status;
948-
app::DataModel::Nullable<uint8_t> currentLevel;
949944
uint8_t difference;
945+
EmberAfLevelControlState * state;
946+
app::DataModel::Nullable<uint8_t> currentLevel;
950947

951-
if (state == nullptr)
948+
EndpointId endpoint = commandPath.mEndpointId;
949+
CommandId commandId = commandPath.mCommandId;
950+
// Validate the received rate and moveMode first.
951+
if ((!rate.IsNull() && (rate.Value() == 0)) || moveMode == MoveModeEnum::kUnknownEnumValue)
952952
{
953-
status = Status::Failure;
953+
status = Status::InvalidCommand;
954954
goto send_default_response;
955955
}
956956

957-
if (!shouldExecuteIfOff(endpoint, commandId, optionsMask, optionsOverride))
957+
state = getState(endpoint);
958+
if (state == nullptr)
958959
{
959-
status = Status::Success;
960+
status = Status::Failure;
960961
goto send_default_response;
961962
}
962963

963-
// Always validate the rate parameter received. A rate of 0 is invalid.
964-
if (!rate.IsNull() && (rate.Value() == 0))
964+
if (!shouldExecuteIfOff(endpoint, commandId, optionsMask, optionsOverride))
965965
{
966-
// Providing rate of 0 is not allowed.
967-
status = Status::InvalidCommand;
966+
status = Status::Success;
968967
goto send_default_response;
969968
}
970969

@@ -1082,14 +1081,22 @@ static void stepHandler(app::CommandHandler * commandObj, const app::ConcreteCom
10821081
uint8_t stepSize, app::DataModel::Nullable<uint16_t> transitionTimeDs,
10831082
chip::Optional<BitMask<OptionsBitmap>> optionsMask, chip::Optional<BitMask<OptionsBitmap>> optionsOverride)
10841083
{
1085-
EndpointId endpoint = commandPath.mEndpointId;
1086-
CommandId commandId = commandPath.mCommandId;
1087-
1088-
EmberAfLevelControlState * state = getState(endpoint);
10891084
Status status;
1085+
EmberAfLevelControlState * state;
10901086
app::DataModel::Nullable<uint8_t> currentLevel;
1087+
1088+
EndpointId endpoint = commandPath.mEndpointId;
1089+
CommandId commandId = commandPath.mCommandId;
10911090
uint8_t actualStepSize = stepSize;
10921091

1092+
// Validate the received stepSize and stepMode first.
1093+
if (stepSize == 0 || stepMode == StepModeEnum::kUnknownEnumValue)
1094+
{
1095+
status = Status::InvalidCommand;
1096+
goto send_default_response;
1097+
}
1098+
1099+
state = getState(endpoint);
10931100
if (state == nullptr)
10941101
{
10951102
status = Status::Failure;
@@ -1151,6 +1158,7 @@ static void stepHandler(app::CommandHandler * commandObj, const app::ConcreteCom
11511158
}
11521159
break;
11531160
default:
1161+
// Should never happen as it is verified at function entry.
11541162
status = Status::InvalidCommand;
11551163
goto send_default_response;
11561164
}

0 commit comments

Comments
 (0)