Skip to content

Commit ba4b299

Browse files
committed
Fix index-out-of-bounds issue when reading slave response
1 parent fa6430c commit ba4b299

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

umodbus/serial.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,19 @@ def _exit_read(self, response: bytearray) -> bool:
143143
:param response: The response
144144
:type response: bytearray
145145
146-
:returns: State of basic read response evaluation
146+
:returns: State of basic read response evaluation,
147+
True if entire response has been read
147148
:rtype: bool
148149
"""
149-
if response[1] >= Const.ERROR_BIAS:
150-
if len(response) < Const.ERROR_RESP_LEN:
150+
response_len = len(response)
151+
if response_len >= 2 and response[1] >= Const.ERROR_BIAS:
152+
if response_len < Const.ERROR_RESP_LEN:
151153
return False
152-
elif (Const.READ_COILS <= response[1] <= Const.READ_INPUT_REGISTER):
154+
elif response_len >= 3 and (Const.READ_COILS <= response[1] <= Const.READ_INPUT_REGISTER):
153155
expected_len = Const.RESPONSE_HDR_LENGTH + 1 + response[2] + Const.CRC_LENGTH
154-
if len(response) < expected_len:
156+
if response_len < expected_len:
155157
return False
156-
elif len(response) < Const.FIXED_RESP_LEN:
158+
elif response_len < Const.FIXED_RESP_LEN:
157159
return False
158160

159161
return True

0 commit comments

Comments
 (0)