From 788ff74f466258bb0a7683f1fe548e5acc709316 Mon Sep 17 00:00:00 2001 From: Carlo Perassi Date: Fri, 30 Oct 2026 18:52:44 +0100 Subject: [PATCH] modbus_reply: reject a write_bits byte count larger than needed Write Multiple Coils checked only that the byte count was large enough for the quantity. The specification requires it to equal the quantity divided by 8, rounded up, and answers exception 03 otherwise. A request for 4 coils with a byte count of 2 was carried out, from the first data byte; one with a byte count too small was already refused. --- src/modbus.c | 3 ++- tests/unit-test-client.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/modbus.c b/src/modbus.c index abdd718c4..9e5880eae 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -1052,7 +1052,8 @@ int modbus_reply(modbus_t *ctx, int nb_bits = req[offset + 5]; int mapping_address = address - mb_mapping->start_bits; - if (nb < 1 || MODBUS_MAX_WRITE_BITS < nb || nb_bits * 8 < nb) { + if (nb < 1 || MODBUS_MAX_WRITE_BITS < nb || + nb_bits != (nb / 8) + ((nb % 8) ? 1 : 0)) { /* May be the indication has been truncated on reading because of * invalid address (eg. nb is 0 but the request contains values to * write) so it's necessary to flush. */ diff --git a/tests/unit-test-client.c b/tests/unit-test-client.c index c1542e971..def3bad23 100644 --- a/tests/unit-test-client.c +++ b/tests/unit-test-client.c @@ -868,6 +868,19 @@ int test_server(modbus_t *ctx, int use_backend) const int INVALID_FC = 0x42; const int INVALID_FC_REQ_LEN = 6; uint8_t invalid_fc_raw_req[] = {slave, 0x42, 0x00, 0x00, 0x00, 0x00}; + /* 4 coils need a byte count of 1, not 2 */ + const int WRITE_COILS_RAW_REQ_LEN = 9; + uint8_t write_coils_raw_req[] = {slave, + MODBUS_FC_WRITE_MULTIPLE_COILS, + UT_BITS_ADDRESS >> 8, + UT_BITS_ADDRESS & 0xFF, + 0x00, + 0x04, + 0x02, + 0xFF, + 0x01}; + uint8_t tab_bits_before[4]; + uint8_t tab_bits_after[4]; int req_length; uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; @@ -999,6 +1012,25 @@ int test_server(modbus_t *ctx, int use_backend) rsp[backend_offset] == (0x80 + INVALID_FC), "") + /* Test a byte count larger than the number of coils needs */ + rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 4, tab_bits_before); + printf("* modbus_read_bits before write_bits with a too large byte count: "); + ASSERT_TRUE(rc == 4, "FAILED (%d)\n", rc); + + modbus_send_raw_request( + ctx, write_coils_raw_req, WRITE_COILS_RAW_REQ_LEN * sizeof(uint8_t)); + rc = modbus_receive_confirmation(ctx, rsp); + printf("Return an exception on a too large byte count in write_bits: "); + ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) && + rsp[backend_offset] == (0x80 + MODBUS_FC_WRITE_MULTIPLE_COILS) && + rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, + "") + + rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 4, tab_bits_after); + printf("* coils unchanged: "); + ASSERT_TRUE(rc == 4 && is_memory_equal(tab_bits_before, tab_bits_after, 4), + "FAILED\n"); + modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); return 0; close: