Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle partial message in ReadDeviceInformationResponse #1738

Merged
merged 2 commits into from
Aug 16, 2023

Conversation

lsgunth
Copy link
Contributor

@lsgunth lsgunth commented Aug 16, 2023

When a partial message is received for a ReadDeviceInformationResponse, an exception occurs when calculating the frame size:

The caller is expecting an IndexError if there isn't enough data in the packet. So to fix this, catch the struct error and raise an IndexError.

Copy link
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.

The change looks good, please solve the linter problem in CI.

When a partial message is received for a ReadDeviceInformationResponse,
an exception occurs when calculating the frame size:

 ERROR:asyncio:Exception in callback SerialTransport._read_ready()
 handle: <Handle SerialTransport._read_ready()>
 Traceback (most recent call last):
   File "/usr/lib/python3.9/asyncio/events.py", line 80, in _run
       self._context.run(self._callback, *self._args)
   File "pymodbus/transport/transport_serial.py", line 109, in _read_ready
       self._protocol.data_received(data)
   File "pymodbus/transport/transport.py", line 307, in data_received
       self.datagram_received(data, None)
   File "pymodbus/transport/transport.py", line 328, in datagram_received
       cut = self.callback_data(self.recv_buffer, addr=addr)
   File "pymodbus/client/base.py", line 218, in callback_data
       self.framer.processIncomingPacket(data, self._handle_response, slave=0)
   File "pymodbus/framer/base.py", line 137, in processIncomingPacket
       self.frameProcessIncomingPacket(single, callback, slave, **kwargs)
   File "pymodbus/framer/rtu_framer.py", line 213, in frameProcessIncomingPacket
       if not self.isFrameReady():
   File "pymodbus/framer/rtu_framer.py", line 134, in isFrameReady
       size = self.populateHeader()
   File "pymodbus/framer/rtu_framer.py", line 152, in populateHeader
       size = self.get_expected_response_length(data)
   File "pymodbus/framer/rtu_framer.py", line 320, in get_expected_response_length
       return pdu_class.calculateRtuFrameSize(data)
   File "mei_message.py", line 130, in calculateRtuFrameSize
       _, object_length = struct.unpack(">BB", buffer[size : size + 2])
 struct.error: unpack requires a buffer of 2 bytes

The caller is expecting an IndexError if there isn't enough data in the
packet. So to fix this, catch the struct error and raise an IndexError.
@lsgunth
Copy link
Contributor Author

lsgunth commented Aug 16, 2023

Sorry, I was rushing to get that done last night. I had fixed the linter error but forgot to commit the fix. Should pass now.

Thanks!

@janiversen janiversen merged commit 8de66a2 into pymodbus-dev:dev Aug 16, 2023
@janiversen
Copy link
Collaborator

Thanks, looking forward to see other pull requests from you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants