Skip to content

Commit

Permalink
Protect against invalid enums being returned (#36792)
Browse files Browse the repository at this point in the history
* Adding type protection here

* Restyled by clang-format

* Safer way to do this

* Restyled by clang-format

* Fixing format

* Restyled by clang-format

* Adding error

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
woody-apple and restyled-commits authored Dec 11, 2024
1 parent 29c6fd8 commit 2c11741
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/darwin/Framework/CHIP/MTRDevice_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,20 @@ - (oneway void)device:(NSNumber *)nodeID internalStateUpdated:(NSDictionary *)di
- (MTRDeviceState)state
{
NSNumber * stateNumber = MTR_SAFE_CAST(self._internalState[kMTRDeviceInternalPropertyDeviceState], NSNumber);
return stateNumber ? static_cast<MTRDeviceState>(stateNumber.unsignedIntegerValue) : MTRDeviceStateUnknown;
switch (static_cast<MTRDeviceState>(stateNumber.unsignedIntegerValue)) {
case MTRDeviceStateUnknown:
return MTRDeviceStateUnknown;

case MTRDeviceStateUnreachable:
return MTRDeviceStateUnreachable;

case MTRDeviceStateReachable:
return MTRDeviceStateReachable;
}

MTR_LOG_ERROR("stateNumber from internal state is an invalid value: %@", stateNumber);

return MTRDeviceStateUnknown;
}

- (BOOL)deviceCachePrimed
Expand Down

0 comments on commit 2c11741

Please sign in to comment.