Skip to content

Commit

Permalink
Allow socket exception response with wrong length.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Apr 14, 2024
1 parent 14c3a82 commit fa935db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pymodbus/framer/socket_framer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def check_frame(self):
self._header["len"],
self._header["uid"],
) = struct.unpack(">HHHB", self._buffer[0 : self._hsize])
if self._header["len"] == 2 and len(self._buffer) >= self._hsize + 2:
self._header["len"] = 3
if self._header["len"] < 2:
length = self._hsize + self._header["len"] -1
self._buffer = self._buffer[length:]
Expand Down
15 changes: 15 additions & 0 deletions test/test_framers.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,21 @@ def _handle_response(_reply):
framer.processIncomingPacket(message, _handle_response, slave=0)
assert response_ok, "Response is valid, but not accepted"

def test_recv_socket_exception_faulty(self):
"""Test receive packet."""
response_ok = False

def _handle_response(_reply):
"""Handle response."""
nonlocal response_ok
response_ok = True

message = bytearray(b"\x00\x02\x00\x00\x00\x02\x01\x84\x02")
response_ok = False
framer = ModbusSocketFramer(ClientDecoder())
framer.processIncomingPacket(message, _handle_response, slave=0)
assert response_ok, "Response is valid, but not accepted"

# ---- 100% coverage
@pytest.mark.parametrize(
("framer", "message"),
Expand Down

0 comments on commit fa935db

Please sign in to comment.