Skip to content

Commit

Permalink
Simplfy and future-proof enum range checks in door locks.
Browse files Browse the repository at this point in the history
Instead of explicitly checking for the min/max values, check for the "unknown
value" value that decoding will decode to if the value is out of range.

Fixes #20936
  • Loading branch information
bzbarsky-apple committed Feb 6, 2023
1 parent 59894da commit c7b48b0
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/app/clusters/door-lock-server/door-lock-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,10 @@ void DoorLockServer::setUserCommandHandler(chip::app::CommandHandler * commandOb
return;
}

if (!userType.IsNull() &&
(userType.Value() < UserTypeEnum::kUnrestrictedUser || userType.Value() > UserTypeEnum::kRemoteOnlyUser))
if (!userType.IsNull() && userType.Value() == UserTypeEnum::kUnknownEnumValue)
{
emberAfDoorLockClusterPrintln(
"[SetUser] Unable to set the user: user type is out of range [endpointId=%d,userIndex=%d,userType=%u]",
"[SetUser] Unable to set the user: user type is unknown [endpointId=%d,userIndex=%d,userType=%u]",
commandPath.mEndpointId, userIndex, to_underlying(userType.Value()));

sendClusterResponse(commandObj, commandPath, EMBER_ZCL_STATUS_INVALID_COMMAND);
Expand Down Expand Up @@ -694,10 +693,9 @@ void DoorLockServer::setCredentialCommandHandler(
return;
}

if (!userType.IsNull() &&
(userType.Value() < UserTypeEnum::kUnrestrictedUser || userType.Value() > UserTypeEnum::kRemoteOnlyUser))
if (!userType.IsNull() && userType.Value() == UserTypeEnum::kUnknownEnumValue)
{
emberAfDoorLockClusterPrintln("[SetCredential] Unable to set the credential: user type is out of range "
emberAfDoorLockClusterPrintln("[SetCredential] Unable to set the credential: user type is unknown "
"[endpointId=%d,credentialIndex=%d,userType=%u]",
commandPath.mEndpointId, credentialIndex, to_underlying(userType.Value()));
sendSetCredentialResponse(commandObj, commandPath, DlStatus::kInvalidField, 0, nextAvailableCredentialSlot);
Expand Down Expand Up @@ -3113,9 +3111,9 @@ void DoorLockServer::setHolidayScheduleCommandHandler(chip::app::CommandHandler
return;
}

if (operatingMode > OperatingModeEnum::kPassage || operatingMode < OperatingModeEnum::kNormal)
if (operatingMode == OperatingModeEnum::kUnknownEnumValue)
{
emberAfDoorLockClusterPrintln("[SetHolidaySchedule] Unable to add schedule - operating mode is out of range"
emberAfDoorLockClusterPrintln("[SetHolidaySchedule] Unable to add schedule - operating mode is unknown"
"[endpointId=%d,scheduleIndex=%d,localStarTime=%" PRIu32 ",localEndTime=%" PRIu32
", operatingMode=%d]",
endpointId, holidayIndex, localStartTime, localEndTime, to_underlying(operatingMode));
Expand Down

0 comments on commit c7b48b0

Please sign in to comment.