Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
46 changes: 46 additions & 0 deletions tests/unit-test-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down