Skip to content

pgtype: return an error when Bits.Len exceeds the bytes provided - #2730

Open
thejesh23 wants to merge 1 commit into
jackc:masterfrom
thejesh23:bits-len-exceeds-bytes
Open

thejesh23 wants to merge 1 commit into
jackc:masterfrom
thejesh23:bits-len-exceeds-bytes

Conversation

@thejesh23

Copy link
Copy Markdown

pgtype.Bits{Bytes: []byte{0xff}, Len: 9, Valid: true}.Value() panics with index out of range [1] with length 1. The text encoder reads Bytes[i/8] for every i < Len and never checks that Bytes holds Len bits. The same panic happens anywhere the text plan is used (simple protocol, text-format arrays).

The fix returns an error from encodePlanBitsCodecText.Encode when Len > 8*len(Bytes). Only those inputs behave differently. Valid values encode exactly as before, including trailing extra bytes and Len 0.

TestBitsLenExceedsBytes panics on master and passes with the fix. go test -race ./... passes locally against PostgreSQL 16 on Go 1.27. gofmt, gofumpt -extra and golangci-lint report nothing new.

AI proposal: Claude Opus 5.5 in Claude Code found the bug, wrote the test and the patch. Its prompt was an audit brief to find and verify functional bugs in pgx, then to fix one logged finding, writing the regression test first and showing it fails on master.

🤖 Generated with Claude Code

The text encoder indexed Bytes[Len/8] without checking that Bytes holds
Len bits, so Bits{Bytes: []byte{0xff}, Len: 9, Valid: true}.Value()
panicked with index out of range instead of returning an error.
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