From 4a7f55106966e3f791cecd3c17e49ebe087ae7f2 Mon Sep 17 00:00:00 2001 From: Anirudh Sahu Date: Sat, 28 Nov 2026 10:25:52 +0530 Subject: [PATCH 1/2] Limit Write Multiple Coils to 1968 coils and answer ILLEGAL_VALUE. --- pymodbus/pdu/bit_message.py | 5 +++-- test/pdu/test_bit.py | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pymodbus/pdu/bit_message.py b/pymodbus/pdu/bit_message.py index 49bb68f0e..98d6fa536 100644 --- a/pymodbus/pdu/bit_message.py +++ b/pymodbus/pdu/bit_message.py @@ -204,7 +204,7 @@ class WriteMultipleCoilsRequest(ModbusPDU): count: int byte_count: int | None data_byte_count: int - MAX_COUNT = 2000 + MAX_COUNT = 1968 # 0x07B0, largest count whose PDU fits in 253 bytes @classmethod def x_calculateRtuFrameSize(cls, data: bytes) -> int: @@ -242,7 +242,6 @@ def x_encode(self) -> bytes: def x_decode(self, data: bytes) -> None: """Decode a write coils request.""" self.address, self.count, self.byte_count = struct.unpack(">HHB", data[0:5]) - self.verifyCount(self.MAX_COUNT) self.data_byte_count = len(data) - 5 self.bits = unpack_bitstring(data[5 : 5 + cast(int, self.byte_count)])[ : self.count @@ -254,6 +253,8 @@ async def datastore_update( """Run a request against a datastore.""" if self.byte_count is None: self.count = len(self.bits) + elif not 1 <= self.count <= self.MAX_COUNT: + return ExceptionResponse(self.function_code, ExcCodes.ILLEGAL_VALUE) else: expected_byte_count = (self.count + 7) // 8 if ( diff --git a/test/pdu/test_bit.py b/test/pdu/test_bit.py index ff8748b98..7b1c2b6b0 100644 --- a/test/pdu/test_bit.py +++ b/test/pdu/test_bit.py @@ -98,6 +98,31 @@ def test_bit_read_decode_accepts_count_above_max(self): pdu = decoder.decode(frame) assert pdu.count == expected_count + def test_write_coils_max_count_fits_pdu(self): + """The largest write count must keep the PDU within 253 bytes.""" + assert bit_msg.WriteMultipleCoilsRequest.MAX_COUNT == 0x07B0 + pdu = bit_msg.WriteMultipleCoilsRequest(address=0, bits=[True] * 0x07B0) + assert len(pdu.x_encode()) + 1 <= 253 + with pytest.raises(ValueError, match="count"): + bit_msg.WriteMultipleCoilsRequest( + address=0, bits=[True] * (0x07B0 + 1) + ).x_encode() + + @pytest.mark.parametrize("count", [0, 0x07B1, 2000]) + async def test_write_coils_count_out_of_range_returns_illegal_value( + self, count, mock_server_context + ): + """An out-of-range write quantity must answer 0x03, not silence.""" + context = mock_server_context() + context.async_setValues = mock.AsyncMock(return_value=0) + byte_count = (count + 7) // 8 + frame = b"\x0f\x00\x00" + count.to_bytes(2, "big") + bytes([byte_count]) + pdu = DecodePDU(True).decode(frame + b"\xff" * byte_count) + result = await pdu.datastore_update(context, 1) + assert result.function_code == 0x8F + assert result.exception_code == ExcCodes.ILLEGAL_VALUE + context.async_setValues.assert_not_awaited() + async def test_bit_datastore_exceptions(self, mock_server_context): """Test bit exception response from datastore.""" context = mock_server_context() From fe77324d0353c88c740623f83324cefd5cd2be17 Mon Sep 17 00:00:00 2001 From: Anirudh Sahu Date: Sat, 28 Nov 2026 10:31:09 +0530 Subject: [PATCH 2/2] Move the Write Multiple Coils count tests into their own class. --- test/pdu/test_bit.py | 54 ++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/test/pdu/test_bit.py b/test/pdu/test_bit.py index 7b1c2b6b0..26903e11f 100644 --- a/test/pdu/test_bit.py +++ b/test/pdu/test_bit.py @@ -98,31 +98,6 @@ def test_bit_read_decode_accepts_count_above_max(self): pdu = decoder.decode(frame) assert pdu.count == expected_count - def test_write_coils_max_count_fits_pdu(self): - """The largest write count must keep the PDU within 253 bytes.""" - assert bit_msg.WriteMultipleCoilsRequest.MAX_COUNT == 0x07B0 - pdu = bit_msg.WriteMultipleCoilsRequest(address=0, bits=[True] * 0x07B0) - assert len(pdu.x_encode()) + 1 <= 253 - with pytest.raises(ValueError, match="count"): - bit_msg.WriteMultipleCoilsRequest( - address=0, bits=[True] * (0x07B0 + 1) - ).x_encode() - - @pytest.mark.parametrize("count", [0, 0x07B1, 2000]) - async def test_write_coils_count_out_of_range_returns_illegal_value( - self, count, mock_server_context - ): - """An out-of-range write quantity must answer 0x03, not silence.""" - context = mock_server_context() - context.async_setValues = mock.AsyncMock(return_value=0) - byte_count = (count + 7) // 8 - frame = b"\x0f\x00\x00" + count.to_bytes(2, "big") + bytes([byte_count]) - pdu = DecodePDU(True).decode(frame + b"\xff" * byte_count) - result = await pdu.datastore_update(context, 1) - assert result.function_code == 0x8F - assert result.exception_code == ExcCodes.ILLEGAL_VALUE - context.async_setValues.assert_not_awaited() - async def test_bit_datastore_exceptions(self, mock_server_context): """Test bit exception response from datastore.""" context = mock_server_context() @@ -334,3 +309,32 @@ def test_pass_falsy_value_in_write_multiple_coils_request(self): """Test pass falsy value to write multiple coils.""" request = bit_msg.WriteMultipleCoilsRequest(address=1, bits=[False]) assert request.bits == [False] + + +class TestWriteMultipleCoilsCount: + """Quantity limits of Write Multiple Coils (0x0F).""" + + def test_write_coils_max_count_fits_pdu(self): + """The largest write count must keep the PDU within 253 bytes.""" + assert bit_msg.WriteMultipleCoilsRequest.MAX_COUNT == 0x07B0 + pdu = bit_msg.WriteMultipleCoilsRequest(address=0, bits=[True] * 0x07B0) + assert len(pdu.x_encode()) + 1 <= 253 + with pytest.raises(ValueError, match="count"): + bit_msg.WriteMultipleCoilsRequest( + address=0, bits=[True] * (0x07B0 + 1) + ).x_encode() + + @pytest.mark.parametrize("count", [0, 0x07B1, 2000]) + async def test_write_coils_count_out_of_range_returns_illegal_value( + self, count, mock_server_context + ): + """An out-of-range write quantity must answer 0x03, not silence.""" + context = mock_server_context() + context.async_setValues = mock.AsyncMock(return_value=0) + byte_count = (count + 7) // 8 + frame = b"\x0f\x00\x00" + count.to_bytes(2, "big") + bytes([byte_count]) + pdu = DecodePDU(True).decode(frame + b"\xff" * byte_count) + result = await pdu.datastore_update(context, 1) + assert result.function_code == 0x8F + assert result.exception_code == ExcCodes.ILLEGAL_VALUE + context.async_setValues.assert_not_awaited()