Fix write multiple coils byte count validation - #2974
Merged
janiversen merged 4 commits intoJul 23, 2026
Conversation
janiversen
requested changes
Jul 23, 2026
janiversen
left a comment
Collaborator
There was a problem hiding this comment.
Please fix variable name problem (ubuntu 3.14).
janiversen
requested changes
Jul 23, 2026
|
|
||
| function_code = 15 | ||
| rtu_byte_count_pos = 6 | ||
| count: int |
Collaborator
There was a problem hiding this comment.
All 3 have values that depends on the actual request.
Having those as class variables creates a potential problem, when used in parallel. This is one of the rare cases where init is needed.
Contributor
Author
There was a problem hiding this comment.
Done. The three request-dependent values are now initialized per instance in init.
| self, context: ModbusServerContext, device_id: int | ||
| ) -> ModbusPDU: | ||
| """Run a request against a datastore.""" | ||
| count = len(self.bits) |
Collaborator
There was a problem hiding this comment.
It is very confusing to have a class variable self.count and a local variable count.
Contributor
Author
There was a problem hiding this comment.
Done. The local count variable has been removed, and self.count is now used consistently.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Validate the byte count when processing a Write Multiple Coils (FC 0x0F) request.
Problem
A Write Multiple Coils request specifies a
Quantity of Outputs, aByte Count, and the output-value bytes. The byte count must equalceil(Quantity of Outputs / 8), and the payload length must match that byte count.Previously,
WriteMultipleCoilsRequest.decode()read but ignored theByte Count. If a request declared 16 coils but supplied only one data byte, the decoder produced eight coil values.datastore_update()could then write those eight values and return a successful response for eight coils instead of rejecting the inconsistent request. This silently changed the meaning of the request and could modify datastore state.Changes
The decoder now preserves the declared quantity, byte count, and actual payload length. Before accessing the datastore, the request verifies that the byte count matches
ceil(Quantity of Outputs / 8)and that the payload has exactly the declared length. Invalid requests returnILLEGAL_DATA_VALUE (0x03), while valid client-constructed and decoded requests retain their existing behavior.Tests cover byte counts that are too small or too large, payloads that are shorter or longer than declared, and successful processing of a valid request. They also verify that malformed requests do not call
async_setValues().