Skip to content

Commit c960e7c

Browse files
bluetoothbotclaude
andauthored
fix: render gap.py invalid-AD debug log without raising TypeError (#220)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 493115d commit c960e7c

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/bluetooth_data_tools/gap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ def _uncached_parse_advertisement_bytes(
203203
offset += 1 + length
204204
if end > total_length or end - start <= 0:
205205
_LOGGER.debug(
206-
"Invalid BLE GAP AD structure at offset %s: %s (%s)",
206+
"Invalid BLE GAP AD structure at offset %s: %s (length=%s)",
207207
offset,
208208
gap_bytes,
209+
length,
209210
)
210211
continue
211212
if gap_type_num == TYPE_SHORT_LOCAL_NAME and local_name is None:

tests/test_gap.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,19 @@ def test_out_of_bounds_length():
563563
)
564564

565565

566+
def test_invalid_ad_debug_log_renders_without_error(caplog):
567+
"""Malformed AD must produce a renderable debug log (regression: format-string mismatch)."""
568+
# length=5 (claims 4 payload bytes after the type byte) but only 2 follow.
569+
# Unique payload so the lru_cache doesn't return a previous result.
570+
data = b"\x05\x09\xab\xcd"
571+
with caplog.at_level("DEBUG", logger="bluetooth_data_tools.gap"):
572+
adv = parse_advertisement_data((data,))
573+
assert adv.local_name is None
574+
# Render every captured record — a format-string mismatch raises here.
575+
messages = [r.getMessage() for r in caplog.records]
576+
assert any("Invalid BLE GAP AD structure" in m for m in messages)
577+
578+
566579
def test_out_of_bounds_length_by_one():
567580
"""Test out of bound length by one."""
568581

0 commit comments

Comments
 (0)