Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/meshcore/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ async def handle_rx(self, data: bytearray):
# scope is set. Gate the 31+16 byte read so the sentinel
# dispatches an empty payload instead of empty-string fields.
if len(data) >= 48:
res["scope_name"] = dbuf.read(31).decode("utf-8", "ignore").replace("\0", "")
# The name ends at its first NUL. The firmware strcpy's a
# new name over the old one, so after a shorter name the
# field still holds the old name's tail past the NUL.
res["scope_name"] = dbuf.read(31).split(b"\0", 1)[0].decode("utf-8", "ignore")
res["scope_key"] = dbuf.read(16).hex()
await self.dispatcher.dispatch(Event(EventType.DEFAULT_FLOOD_SCOPE, res))

Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_protocol_surface_gaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,22 @@ async def test_default_flood_scope_sentinel_frame_empty_payload():
# dispatches an empty payload (consumers detect "no scope" via key
# presence, not via empty-string sentinel values).
assert evt.payload == {}


@pytest.mark.asyncio
async def test_default_flood_scope_name_ends_at_first_nul():
"""The name ends at its first NUL, not at the field's end (#119: A4).

Companion v1.18.0 strcpy's a new name over the stored one, so "#au" set
after "#au-vic-gipps" leaves the old tail in the field it sends.
"""
reader, dispatched = _make_reader()
scope_name = b"#au\0vic-gipps".ljust(31, b"\0")
scope_key = bytes(range(16))
frame = bytes([PacketType.DEFAULT_FLOOD_SCOPE.value]) + scope_name + scope_key

await reader.handle_rx(bytearray(frame))

assert len(dispatched) == 1
assert dispatched[0].payload["scope_name"] == "#au"
assert dispatched[0].payload["scope_key"] == scope_key.hex()