Skip to content

Commit 53db14e

Browse files
committed
Protect the entire log_indState against malformed message
1 parent f0d39d3 commit 53db14e

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/main/python/main/ayab/engine/control.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -177,33 +177,36 @@ def check_serial_API6(self) -> tuple[Token, int]:
177177
return token, param
178178

179179
def __log_indState(self, msg: bytes) -> None:
180-
directionMap = {0x00: "Left ", 0x01: "Right", 0xFF: "?"}
181-
machineSideMap = {0x00: "? ", 0x01: "Left", 2: "Right"}
182-
beltShiftMap = {0x00: "?", 0x01: "Regular", 0x02: "Shifted"}
183-
carriageMap = {0x00: "K", 0x01: "L", 0x02: "G", 0x03: "K270", 0xFF: "None"}
184-
185-
error = msg[1]
186-
state = msg[2]
187-
hallLeft = struct.unpack(">H", msg[3:5])[0]
188-
hallRight = struct.unpack(">H", msg[5:7])[0]
189-
carriage = carriageMap.get(msg[7], f"0x{msg[7]:02x}?")
190-
position = msg[8]
191-
direction = directionMap.get(msg[9], f"0x{msg[9]:02x}?")
192-
193-
try: # AyabAsync supplies additional parameters
194-
hallActive = machineSideMap.get(msg[10], f"0x{msg[10]:02x}?")
195-
beltShift = beltShiftMap.get(msg[11], f"0x{msg[11]:02x}?")
196-
self.logger.info(
197-
f"IndState: {error:1d} {state:1d} {position:3d}"
198-
f" {carriage:5s} ({beltShift:11s},{direction:5s})"
199-
f" Hall:{hallActive:5s} ({hallLeft:5d}, {hallRight:5d})"
200-
)
201-
except IndexError:
202-
self.logger.info(
203-
f"IndState: {error:1d} {state:1d} {position:3d}"
204-
f" {carriage:5s} ({direction:5s})"
205-
f" Hall:({hallLeft:5d}, {hallRight:5d})"
206-
)
180+
try:
181+
directionMap = {0x00: "Left ", 0x01: "Right", 0xFF: "?"}
182+
machineSideMap = {0x00: "? ", 0x01: "Left", 2: "Right"}
183+
beltShiftMap = {0x00: "?", 0x01: "Regular", 0x02: "Shifted"}
184+
carriageMap = {0x00: "K", 0x01: "L", 0x02: "G", 0x03: "K270", 0xFF: "None"}
185+
186+
error = msg[1]
187+
state = msg[2]
188+
hallLeft = struct.unpack(">H", msg[3:5])[0]
189+
hallRight = struct.unpack(">H", msg[5:7])[0]
190+
carriage = carriageMap.get(msg[7], f"0x{msg[7]:02x}?")
191+
position = msg[8]
192+
direction = directionMap.get(msg[9], f"0x{msg[9]:02x}?")
193+
194+
try: # AyabAsync supplies additional parameters
195+
hallActive = machineSideMap.get(msg[10], f"0x{msg[10]:02x}?")
196+
beltShift = beltShiftMap.get(msg[11], f"0x{msg[11]:02x}?")
197+
self.logger.info(
198+
f"IndState: {error:1d} {state:1d} {position:3d}"
199+
f" {carriage:5s} ({beltShift:11s},{direction:5s})"
200+
f" Hall:{hallActive:5s} ({hallLeft:5d}, {hallRight:5d})"
201+
)
202+
except IndexError:
203+
self.logger.info(
204+
f"IndState: {error:1d} {state:1d} {position:3d}"
205+
f" {carriage:5s} ({direction:5s})"
206+
f" Hall:({hallLeft:5d}, {hallRight:5d})"
207+
)
208+
except (IndexError, struct.error) as e:
209+
self.logger.warning(f"Unable to parse indState message: {e}")
207210

208211
def __log_cnfInfo(self, msg: bytes) -> None:
209212
api = msg[1]

0 commit comments

Comments
 (0)