From 2c11741dedbe26485ff3355aca8b1a23d5bd30e3 Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Tue, 10 Dec 2024 22:13:50 -0800 Subject: [PATCH] Protect against invalid enums being returned (#36792) * 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 --- src/darwin/Framework/CHIP/MTRDevice_XPC.mm | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm index d5356320b24d4c..bc5fa52b327236 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_XPC.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_XPC.mm @@ -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(stateNumber.unsignedIntegerValue) : MTRDeviceStateUnknown; + switch (static_cast(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