fix(postgres): range-check int64 narrowing on scan - #505
Open
FumingPower3925 wants to merge 1 commit into
Open
Conversation
decodeTextInto, decodeBinaryInto and scanValue converted the decoded int64 into int / int32 / int16 / uint32 with a bare conversion, so an out-of-range value (e.g. 2147483648 scanned into *int32) wrapped silently instead of failing the scan. Add bounds-checking helpers that return a 'celeris-postgres: scan: value N out of range for <type>' error, mirroring the encoder's int4/int2 overflow checks, and route every narrowing site through them. Destinations are left untouched on error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
driver/postgres/pool.gonarrowed the decodedint64intoint,int32,int16anduint32with a bare conversion in three places:decodeTextInto(*int,*int32) — the CodeQL alerts H2C frame types and HPACK integration #4/Capability probing system #5 at pool.go:777/791decodeBinaryInto(*int,*int32,*int16,*uint32) — same pattern on the binary fast pathscanValue(*int,*int32,*int16from anint64driver.Value) — the fallback path, same patternAn out-of-range value (a
bigintcolumn scanned into anint32, a negative into auint32, any 64-bit value intointon a 32-bit build) wrapped silently instead of failing the scan.This PR adds four small helpers (
int64ToInt,int64ToInt32,int64ToInt16,int64ToUint32) that bounds-check againstmath.MinX/MaxXand returnceleris-postgres: scan: value %d out of range for <type>— the scan-side twin of the encoder's existingint4 overflow/int2 overflowrejections — and routes all nine narrowing sites through them. On error the destination is left untouched. The*intguard is a no-op on 64-bit and real on 32-bit, as the issue specifies.Why
database/sqland the driver's own encode side both treat a value that does not fit as an error; silently returning a wrong number is a correctness bug, and CodeQLgo/incorrect-integer-conversionflagged it as a true positive.Fail-first evidence
New file
driver/postgres/decode_range_test.go, run against unfixedmain(9a8fc6a) before the fix:(the boundary tests already passed on unfixed code, as expected — they pin the min/max values so the guards are not off by one.)
After the fix:
Verification
gofmt -l driver/postgres/cleango vet ./...clean,go build ./...cleangolangci-lint run ./driver/postgres/...(v2.13.2, go1.27.0): 0 issuesgo test ./driver/postgres/... -race -count=1:postgresok (32.9s),protocolok — all existing decode/scan tests stay greenFixes #502