From ca7e0e776e73e4f2bb16a5432d8b28f18a4d8d22 Mon Sep 17 00:00:00 2001 From: Wenqiang Zhao Date: Mon, 19 Oct 2026 15:33:45 +0800 Subject: [PATCH] modbus: reject oversized byte counts in write multiple coils Require the FC 0x0F byte count to equal the rounded-up number of bytes needed by the coil quantity. Previously, larger byte counts were accepted and the coil mapping was updated instead of returning Illegal Data Value. Add regression tests for quantities 1, 8 and 9 that check the exception response and confirm that the mapping is unchanged. Fixes #883. --- src/modbus.c | 5 +++-- tests/unit-test-client.c | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/modbus.c b/src/modbus.c index abdd718c4..867878fc4 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -1049,10 +1049,11 @@ int modbus_reply(modbus_t *ctx, } break; case MODBUS_FC_WRITE_MULTIPLE_COILS: { int nb = (req[offset + 3] << 8) + req[offset + 4]; - int nb_bits = req[offset + 5]; + int nb_bytes = 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_bytes != (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..ead5e542b 100644 --- a/tests/unit-test-client.c +++ b/tests/unit-test-client.c @@ -990,6 +990,52 @@ int test_server(modbus_t *ctx, int use_backend) if (rc == -1) goto close; + /* Reject oversized coil byte counts for aligned and unaligned quantities + * without modifying the mapping. */ + { + const int quantities[] = {1, 8, 9}; + uint8_t bits[9] = {0}; + const uint8_t zero_bits[9] = {0}; + uint8_t req[] = {slave, + MODBUS_FC_WRITE_MULTIPLE_COILS, + UT_BITS_ADDRESS >> 8, + UT_BITS_ADDRESS & 0xFF, + 0, + 0, + 0, + 0xFF, + 0xFF, + 0xFF}; + + for (i = 0; i < 3; i++) { + int nb = quantities[i]; + int nb_bytes = (nb / 8) + ((nb % 8) ? 1 : 0) + 1; + + rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, nb, zero_bits); + printf("* Prepare %d coils for oversized byte count: ", nb); + ASSERT_TRUE(rc == nb, "FAILED (%d)\n", rc); + + req[5] = nb; + req[6] = nb_bytes; + rc = modbus_send_raw_request(ctx, req, 7 + nb_bytes); + printf("* Send oversized byte count for %d coils: ", nb); + ASSERT_TRUE(rc == backend_length + 6 + nb_bytes, "FAILED (%d)\n", rc); + + rc = modbus_receive_confirmation(ctx, rsp); + printf("* Reject oversized byte count for %d coils: ", nb); + 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, + "FAILED (%d)\n", + rc); + + rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, nb, bits); + printf("* Preserve %d coils after invalid byte count: ", nb); + ASSERT_TRUE(rc == nb && memcmp(bits, zero_bits, nb) == 0, "FAILED\n"); + } + } + /* Test invalid function code */ modbus_send_raw_request( ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t));