Skip to content

Commit

Permalink
validate byte_count
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Ruddick committed Jul 23, 2024
1 parent 857ddf2 commit cd0050c
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pymodbus/pdu/register_read_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# pylint: disable=missing-type-doc
import struct

from pymodbus.exceptions import ModbusIOException
from pymodbus.pdu import ModbusExceptions as merror
from pymodbus.pdu import ModbusRequest, ModbusResponse

Expand Down Expand Up @@ -88,6 +89,8 @@ def decode(self, data):
:param data: The request to decode
"""
byte_count = int(data[0])
if byte_count < 2 or byte_count > 246 or byte_count != len(data) - 1:
raise ModbusIOException(f"Invalid response {data} has byte count of {byte_count}")
self.registers = []
for i in range(1, byte_count + 1, 2):
self.registers.append(struct.unpack(">H", data[i : i + 2])[0])
Expand Down

0 comments on commit cd0050c

Please sign in to comment.