fix: guard relay_switch get_basic_info against short responses#509
Draft
bluetoothbot wants to merge 2 commits into
Draft
fix: guard relay_switch get_basic_info against short responses#509bluetoothbot wants to merge 2 commits into
bluetoothbot wants to merge 2 commits into
Conversation
Closes sblibs#369. `_parse_common_data` reads `raw_data[1]`, `[2]`, `[16]` and `_parse_user_data` calls `parse_uint24_be(raw_data, 1)` plus `parse_power_data(raw_data, 13, ...)`. The base `_get_basic_info` only filters single-byte payloads matching `b"\\x07"` or `b"\\x00"`, so any other truncated response (e.g. the reporter's `b"\\x02"`) bubbles up and raises IndexError/ValueError inside the parsers. Gate `get_basic_info` (1PM/garage door/plug mini EU) on `_data` ≥ 17 bytes and `_channel1_data` ≥ 15 bytes, and the 2PM override on `_channel2_data` ≥ 15 bytes. Log a warning with the hex payload to aid diagnosis. Add regression coverage for each of the three truncation paths across all four single-channel models and the 2PM variant.
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
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.
What
Guard
relay_switch.get_basic_info(RELAY_SWITCH_1, RELAY_SWITCH_1PM, RELAY_SWITCH_2PM, PLUG_MINI_EU, GARAGE_DOOR_OPENER) against truncated BLE responses.Why
Closes #369. The reporter saw
IndexError: bytearray index out of rangefrom_parse_common_dataandValueError: Insufficient data: need at least 4 bytes, got 1from_parse_user_data(viaparse_uint24_be) when a single-byte payload —b"\x02"— reached the parser. The base_get_basic_infoonly filters the exact bytesb"\x07"andb"\x00", so anything else slips through and crashes inside the parser. Same class of bug as #491/#495/#496/#499/#500 — this branch is the relay-switch arm of that sweep, which was skipped at the time.How
SwitchbotRelaySwitch.get_basic_info: bail with a warning if_data< 17 (last index touched is[16]) or_channel1_data< 15 (last 2-byte power slot is at offset 13).SwitchbotRelaySwitch2PM.get_basic_info: same guard for_channel2_data.Testing
tests/test_relay_switch.py— addedtest_get_basic_info_2PM_short_response(5 truncation scenarios) andtest_get_basic_info_short_responseparametrised across all four single-channel models × 2 truncation scenarios. Each case assertsget_basic_info()returnsNoneinstead of raising.1226 passed.Quality Report
Changes: 2 files changed, 124 insertions(+)
Code scan: clean
Tests: passed (1226 passed)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline