diff --git a/pymodbus/framer/socket_framer.py b/pymodbus/framer/socket_framer.py index f89d93bd7..cb40debda 100644 --- a/pymodbus/framer/socket_framer.py +++ b/pymodbus/framer/socket_framer.py @@ -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:] diff --git a/test/test_framers.py b/test/test_framers.py index 0eafac228..e813ebcde 100644 --- a/test/test_framers.py +++ b/test/test_framers.py @@ -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"),