Skip to content

Fix xid8 overflow when decoding values for sql.Scanner - #2693

Merged
jackc merged 1 commit into
jackc:masterfrom
k-zshiba:fix-xid8-sql-scanner-overflow
Oct 21, 2026
Merged

jackc merged 1 commit into
jackc:masterfrom
k-zshiba:fix-xid8-sql-scanner-overflow

Conversation

@k-zshiba

Copy link
Copy Markdown
Contributor

Uint64Codec.DecodeDatabaseSQLValue unconditionally casts decoded values to int64. When an xid8 value exceeds math.MaxInt64, scanners receive a negative integer without an error. For example, 18446744073709551615 is read as "-1" through sql.NullString.

Reuse Uint64.Value() to preserve the full unsigned range. Values within the signed range remain int64, larger values are returned as decimal strings, and NULL remains nil. This also allows scanners targeting int64 to report an out-of-range error instead of accepting a wrapped value.

Add regression coverage for math.MaxInt64, math.MaxInt64 + 1, math.MaxUint64, and NULL in both text and binary formats. The regression test fails before the fix and passes afterward.

Validation:

go test -race ./pgtype -run '^TestUint64Codec' -v -count=1

Passed with Go 1.25.5 against a temporary PostgreSQL 20devel instance.

Closes #2692

Uint64Codec.DecodeDatabaseSQLValue cast uint64 values to int64 without
checking the range. Values above math.MaxInt64 were therefore passed to
sql.Scanner as negative integers.

Reuse Uint64.Value so values within the signed range remain int64 and
larger values are represented as decimal strings without losing data.
Add regression coverage for the signed boundary, maximum uint64, and
NULL in both text and binary formats.
@jackc
jackc merged commit 0dd05bb into jackc:master Oct 21, 2026
22 checks passed
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.

xid8 values above math.MaxInt64 become negative when scanned into sql.Scanner

2 participants