Skip to content

Fix slice panic in PolledClient.GetMessages for out-of-range start/count - #3063

Draft
rootkiller6788 wants to merge 1 commit into
google:mainfrom
rootkiller6788:fix/polled-messages-slice-panic
Draft

Fix slice panic in PolledClient.GetMessages for out-of-range start/count#3063
rootkiller6788 wants to merge 1 commit into
google:mainfrom
rootkiller6788:fix/polled-messages-slice-panic

Conversation

@rootkiller6788

Copy link
Copy Markdown

Problem

The /polled_connections/{connId}/messages HTTP endpoint parses start and count from the request query string and passes them to PolledClient.GetMessages without any range validation. A negative start (e.g. ?start=-1) or a start beyond the number of buffered messages (e.g. ?start=100) caused make([]interface{}, count) or c.messages[start:end] to panic with a slice out-of-range error, terminating the request handler.

Change

Clamp start to [0, len(messages)] and count to the remaining messages (len(messages)-start) in GetMessages, so the method returns a sensible slice for any input:

  • start < 0 is treated as 0
  • start > len(messages) returns an empty slice
  • count < 0 (meaning "all remaining") or count larger than the remaining messages is clamped to the remaining count

This also avoids the start + count integer-overflow edge case for very large count values by using len(messages)-start instead of computing end := start + count.

Verification

  • Added TestGetMessagesBounds covering negative start, start past the end (with and without count), and an oversized count.
  • Existing TestMessages behavior is preserved.

The /polled_connections/{connId}/messages endpoint parses start and count from the request query and passes them to GetMessages unchecked. A negative start or a start beyond the number of buffered messages caused make([]interface{}, count) or c.messages[start:end] to panic with a slice out-of-range error, killing the request handler.

Clamp start to [0, len(messages)] and count to the remaining messages so the method returns a sensible slice for any input instead of panicking. Adds TestGetMessagesBounds covering negative start, start past the end, and an oversized count.
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