Skip to content

fix: reject record batches with a negative record count - #1445

Open
ankit-songara wants to merge 1 commit into
segmentio:mainfrom
ankit-songara:fix/negative-record-count-panic
Open

fix: reject record batches with a negative record count#1445
ankit-songara wants to merge 1 commit into
segmentio:mainfrom
ankit-songara:fix/negative-record-count-panic

Conversation

@ankit-songara

Copy link
Copy Markdown

Summary

readFromVersion2 reads numRecords as a signed int32 after the batch length and CRC checks, then uses it directly as the length for make([]optimizedRecord, numRecords). A Fetch response containing a structurally valid v2 record batch with numRecords=-1 passes those checks and then panics with makeslice: len out of range, which crashes any consumer that doesn't wrap the fetch path in a recover.

This adds a sign check right before the allocation so the batch is rejected with a normal error instead of panicking.

Fixes #1438

Test plan

  • Added TestRecordSetNegativeRecordCount in protocol/record_batch_test.go: builds a real v2 record batch via RecordSet.WriteTo, patches the record count field to -1, recomputes the CRC over the crc-protected region, then feeds it through RecordSet.ReadFrom and asserts an error is returned (and that decoding does not panic).
  • Verified the new test panics on main without the fix and passes with it.
  • go build ./protocol/..., go vet ./protocol/..., go test ./protocol/... all pass.
  • gofmt -l clean on both touched files (ignoring the pre-existing CRLF-vs-LF noise from gofmt -l on this Windows checkout, which flags these files identically before and after my change).

readFromVersion2 read numRecords as a signed int32 and used it
directly as the length for make([]optimizedRecord, numRecords)
without checking its sign. A Fetch response with a crafted v2 record
batch reporting numRecords=-1 passes the batch length and CRC checks
but then panics with "makeslice: len out of range", crashing any
client that doesn't recover around the fetch path.

Reject the batch with an error as soon as a negative count is seen,
before the allocation.

Fixes segmentio#1438
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.

kafka-go Fetch response negative record count panic

2 participants