Skip to content

Commit d3435e5

Browse files
yashhzdamilcarlucas
authored andcommitted
fix(connection): prevent IndexError on malformed ChibiOS banner line
_extract_chibios_version_from_banner crashed with IndexError when a banner line starting with "ChibiOS:" did not contain a space followed by a version hash (for example a truncated "ChibiOS:" line, or "ChibiOS:hash" without the expected space separator). msg.split(" ")[1] unconditionally indexed the second element of the split, which does not exist when the separator is absent. The IndexError propagated out of the banner-parsing loop during flight controller connection setup and aborted the whole connection attempt. Use split(" ", 1) and guard the length before indexing, falling back to an empty version string. The surrounding code already handles an empty os_custom_version correctly (it logs a version mismatch warning against info.os_custom_version when the two differ). Add two unit tests covering the malformed banner cases that previously raised IndexError. Signed-off-by: Yash Goel <yashvardhan664@gmail.com>
1 parent 63582c1 commit d3435e5

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

ardupilot_methodic_configurator/backend_flightcontroller_connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,8 @@ def _extract_chibios_version_from_banner(self, banner_msgs: list[str]) -> tuple[
631631

632632
for i, msg in enumerate(banner_msgs):
633633
if "ChibiOS:" in msg:
634-
os_custom_version = msg.split(" ")[1].strip()
634+
parts = msg.split(" ", 1)
635+
os_custom_version = parts[1].strip() if len(parts) > 1 else ""
635636
hash_len1 = max(7, len(os_custom_version) - 1)
636637
hash_len2 = max(7, len(self.info.os_custom_version) - 1)
637638
hash_len = min(hash_len1, hash_len2)

tests/test_backend_flightcontroller_connection.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,41 @@ def test_extract_chibios_version_returns_none_index_when_absent(self) -> None:
15181518
assert os_ver == ""
15191519
assert index is None
15201520

1521+
def test_extract_chibios_version_handles_banner_without_trailing_space(self) -> None:
1522+
"""
1523+
_extract_chibios_version_from_banner tolerates a malformed banner line with no space after the prefix.
1524+
1525+
GIVEN: Banner messages where the ChibiOS line has no space between the prefix and the version
1526+
WHEN: _extract_chibios_version_from_banner is called
1527+
THEN: An empty version string should be returned
1528+
AND: The index of the ChibiOS line should still be recorded
1529+
AND: No IndexError should be raised
1530+
"""
1531+
connection = FlightControllerConnection(info=FlightControllerInfo())
1532+
banner_msgs = ["ArduCopter V4.5.0", "ChibiOS:"]
1533+
1534+
os_ver, index = connection._extract_chibios_version_from_banner(banner_msgs)
1535+
1536+
assert os_ver == ""
1537+
assert index == 1
1538+
1539+
def test_extract_chibios_version_handles_banner_with_only_prefix(self) -> None:
1540+
"""
1541+
_extract_chibios_version_from_banner tolerates a banner line that is exactly the ChibiOS prefix.
1542+
1543+
GIVEN: A banner line consisting only of the 'ChibiOS:' prefix with no hash at all
1544+
WHEN: _extract_chibios_version_from_banner is called
1545+
THEN: An empty version string should be returned
1546+
AND: No IndexError should be raised
1547+
"""
1548+
connection = FlightControllerConnection(info=FlightControllerInfo())
1549+
banner_msgs = ["ChibiOS:"]
1550+
1551+
os_ver, index = connection._extract_chibios_version_from_banner(banner_msgs)
1552+
1553+
assert os_ver == ""
1554+
assert index == 0
1555+
15211556
def test_extract_firmware_type_from_message_after_chibios(self) -> None:
15221557
"""
15231558
_extract_firmware_type_from_banner extracts type from message after ChibiOS line.

0 commit comments

Comments
 (0)