Fix: validate register write byte counts#2977
Merged
janiversen merged 4 commits intoJul 23, 2026
Merged
Conversation
janiversen
requested changes
Jul 23, 2026
janiversen
left a comment
Collaborator
There was a problem hiding this comment.
Please solve formatting problem (ubuntu 3.14).
Please use ./check_ci.sh, that checks and reformats just like CI, and afterward commit changed files.
janiversen
requested changes
Jul 23, 2026
| register = struct.unpack(">H", data[i : i + 2])[0] | ||
| self.write_registers.append(register) | ||
| self._payload_byte_count = len(data) - 9 | ||
| payload = data[9 : self.write_byte_count + 9] |
Collaborator
There was a problem hiding this comment.
Why make an extra split ? Line 154 could just use data directly.
Contributor
Author
There was a problem hiding this comment.
Done. The registers are now unpacked directly from data. I applied the same simplification to both 0x17 and 0x10.
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.
Problem
Modbus function codes
0x10(Write Multiple Registers) and0x17(Read/Write Multiple Registers) require the write byte count to equal twice the write quantity, because every register occupies two bytes.The request decoders did not enforce this relationship. For
0x10, decoding was based on the quantity while the declared byte count was discarded, which could cause extra data to be ignored or truncated data to raise a parsing error. For0x17, registers were decoded using the byte count, but a byte count inconsistent with the write quantity could still reach the datastore. As a result, malformed requests could be accepted, incorrectly parsed, or fail without returning the requiredIllegal Data Value (03)response.Changes
0x10and0x17requests, validate both before accessing the datastore, and returnIllegal Data Value (03)when they are inconsistent. Parsing now consumes only complete register values so malformed payloads can be rejected cleanly instead of raisingstruct.error.