Skip to content

modbus_reply: reject a write_bits byte count larger than needed - #889

Open
carlok wants to merge 1 commit into
stephane:masterfrom
carlok:write-bits-byte-count
Open

carlok wants to merge 1 commit into
stephane:masterfrom
carlok:write-bits-byte-count

Conversation

@carlok

@carlok carlok commented Oct 30, 2026

Copy link
Copy Markdown

Write Multiple Coils (0x0F) refused a byte count that was too small for the quantity, but it accepted a larger one. It then carried out the request, starting from the first data byte. The specification's state diagram (Modbus Application Protocol V1.1b3, 6.11) answers exception 03 unless the byte count is the quantity divided by 8, rounded up. Write Multiple Registers already checks its byte count both ways (nb_bytes != nb * 2).

The change: the condition becomes nb_bits != (nb / 8) + ((nb % 8) ? 1 : 0), the expression used elsewhere in modbus.c.

The test: unit-test-client.c reads 4 coils at UT_BITS_ADDRESS. It then sends a write of 4 coils with a byte count of 2, and expects exception 03. Finally it reads the coils again and checks they're unchanged. On master the server answers 0f 0130 0004 and writes the coils; here it answers 8f 03. tests/unit-tests.sh and make distcheck pass.

Found by a differential test against pymodbus.

🤖 Generated with Claude Code

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.
@cla-bot

cla-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant