Fix parse_response crash on empty response#151
Conversation
|
Note: this may overlap with #148 — happy to close/rebase once that lands, whichever the maintainer prefers. |
| import sys | ||
| from unittest.mock import MagicMock | ||
|
|
||
| class MockBleakGATTCharacteristic: | ||
| pass | ||
|
|
||
| class MockBleakClient: | ||
| pass | ||
|
|
||
| class MockBleakClientWithServiceCache: | ||
| pass |
There was a problem hiding this comment.
Maybe from a different commit?
When the mower returns an empty response (response_length == 0), parse_response() attempts to parse fields according to the schema. For schema definitions containing uint8 or bool fields, this leads to direct index access data[dpos] on the empty bytearray, raising an IndexError. This fix checks if response_length is 0 right after assigning `data` and returns None immediately, representing a valid "no data available" response.
35ed26d to
edd5903
Compare
|
Good catch — you're right, this wasn't needed here. I removed the tests/init.py mock scaffolding and verified the test suite (including test_decode_empty_response) still passes cleanly without it, since Force-pushed a clean commit (edd5903) that now only touches:
Nothing else in the diff. Let me know if anything else needs adjusting. |
|
I checked this against #148. I do not think #148 currently covers this exact empty-response case. #148 adds response-result helpers, serialized command flow, and variable trailing-field parsing via So this PR looks orthogonal and useful unless the same guard/test is folded into #148. |
Problem
When the mower returns a response with
response_length == 0(for example, callingGetMessagewhen there are no logged errors), the library still attempts to parse fields according to the schema.For commands whose schemas contain
uint8orboolfields, the parsing logic accesses the data array using direct indexing (data[dpos]). On an empty bytearray, this immediately raises anIndexError: bytearray index out of range, causing the library to crash.Solution
Directly after slicing
datafrom the response packet, check ifresponse_length == 0. If so, returnNoneimmediately, representing a valid "no data available" response.Verification
A unit test
test_decode_empty_responsewas added totests/test_response.pyto verify that parsing a response packet with a data length of0returnsNoneinstead of raising anIndexError.