Skip to content

Fix IndexError crash in TCP packet parser (packet types 115 and 131) - #168

Open
imwithsam wants to merge 1 commit into
nikshriv:mainfrom
imwithsam:fix/tcp-parser-index-out-of-range
Open

Fix IndexError crash in TCP packet parser (packet types 115 and 131)#168
imwithsam wants to merge 1 commit into
nikshriv:mainfrom
imwithsam:fix/tcp-parser-index-out-of-range

Conversation

@imwithsam

Copy link
Copy Markdown

Summary

  • The packet_type == 115 and packet_type == 131 handlers in _read_tcp_messages each have several self.home_devices[home_id][idx] lookups with no bounds check.
  • The packet_type == 67 handler right below them already guards its equivalent lookup with if int(packet[3]) < len(self.home_devices[home_id]): before indexing — this PR brings the other two handlers in line with that existing pattern.
  • Without the guard, a packet whose device index isn't present in the locally cached home_devices list throws an uncaught IndexError: list index out of range, which gets caught by the outer try/except and logged as Error parsing an incoming Cync TCP packet — the packet is silently dropped with no further handling.

Observed this live on a real setup: the "state and brightness change" sub-handler (the one at packet[21]) hit this exact IndexError repeatedly on a bulb whose index wasn't (yet) in home_devices[home_id] for that home. Confirmed the same unguarded pattern exists in 5 other spots across both handlers, so fixed all of them for consistency rather than just the one that happened to crash.

Test plan

  • python3 -m py_compile passes on the modified file
  • Applied the same fix to a live Home Assistant instance running this integration and confirmed the container starts cleanly with no new errors
  • Would appreciate a maintainer/community check on an account that regularly reproduces this crash, to confirm packets that previously crashed the parser are now cleanly skipped instead

Several device-index lookups (self.home_devices[home_id][idx]) in the
packet_type == 115 and packet_type == 131 handlers were unguarded,
unlike the equivalent lookup in the packet_type == 67 handler, which
already checks the index against len(self.home_devices[home_id])
before using it.

When Cync's cloud sends a packet whose device index isn't present in
the locally cached home_devices list, this throws an uncaught
IndexError: list index out of range, dropping the packet entirely
(caught by the outer try/except and logged as 'Error parsing an
incoming Cync TCP packet', with no further handling).

Added the same bounds check already used in the packet_type == 67
handler to the 5 remaining unguarded lookups, so a packet referencing
an out-of-range index is safely skipped instead of crashing the parse
loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant