ensure unpacking of proper length during decoding (#2664)#2665
Merged
Conversation
janiversen
requested changes
May 9, 2025
| 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]) |
Collaborator
There was a problem hiding this comment.
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.
Contributor
Author
There was a problem hiding this comment.
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)
janiversen
approved these changes
May 9, 2025
Collaborator
janiversen
left a comment
There was a problem hiding this comment.
Seems it actually solves the case.
Thanks.
Ps. added test case.
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.
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