Skip to content

ensure unpacking of proper length during decoding (#2664)#2665

Merged
janiversen merged 2 commits into
pymodbus-dev:devfrom
brherger:bugfix/issue-2664
May 9, 2025
Merged

ensure unpacking of proper length during decoding (#2664)#2665
janiversen merged 2 commits into
pymodbus-dev:devfrom
brherger:bugfix/issue-2664

Conversation

@brherger
Copy link
Copy Markdown
Contributor

@brherger brherger commented May 9, 2025

I am not sure if this fixes the issue with the garbage cleanup, but it seems like a correct and consistent thing to include to avoid the exception.

fixes #2664

def decode(self, data: bytes) -> None:
"""Decode a request pdu."""
self.address, self.count = struct.unpack(">HH", data)
self.address, self.count = struct.unpack(">HH", data[:4])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am really confused, how can this help with the exception ???

The buffer needs to be >= 4, so the exception arises when the buffer is less than 4 bytes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no way of knowing what is coming in, not logged in the exception. It will throw an exception for != 4.
This is also done elsewhere in these files.

>>> data =  b"\x11\x22\x33\x44\x55\x66"
>>> struct.unpack(">HH", data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: unpack requires a buffer of 4 bytes
>>> struct.unpack(">HH", data[:3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: unpack requires a buffer of 4 bytes
>>> struct.unpack(">HH", data[:4])
(4386, 13124)

Copy link
Copy Markdown
Collaborator

@janiversen janiversen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it actually solves the case.

Thanks.

Ps. added test case.

@janiversen janiversen merged commit ef44d0f into pymodbus-dev:dev May 9, 2025
1 check passed
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.

Server not handling garbage byte after frame

3 participants