fix: make uint32 saturation portable - #6
Conversation
kolkov
left a comment
There was a problem hiding this comment.
Confirmed — min(max(v, 0), math.MaxUint32) fails to compile on 32-bit targets because math.MaxUint32 overflows int. The uint64 comparison fix is correct and preserves the clamping contract on both 32-bit and 64-bit.
Tests adapt to the host int width, which is the right approach for a function that must work on both architectures.
LGTM, merging.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
CI fails on all three PRs with the same config issue:
Could you drop the |
Summary
min/maxexpression insaturateUint32with explicit signed and unsigned bounds checksmath.MaxUint32behavior on 64-bit systemsintwidthWhy
The current expression requires representing
math.MaxUint32as anint, so the root package does not compile for 32-bit targets. Checking the signed lower bound first and comparing throughuint64avoids that compile-time overflow without changing the helper's contract.Verification
GOOS=linux GOARCH=386 go test -c .reproduces the overflowGOOS=linux GOARCH=386 go test -c .passesCGO_ENABLED=0 GOOS=linux GOARCH=386 go build ./...go test -count=1 ./...go test -race -count=1 ./...go build ./...go vet ./...go test -coverprofile=coverage.out -covermode=atomic ./...: repository 93.6%; changed coverable statements 100%gofmtandgit diff --checkCurrent CI status
The upstream fork workflow for head
d0acbc8is awaiting maintainer approval in Actions run 31433844937. Until that approval, GitHub cannot publish the repository's Actions or Codecov checks/comments. Local changed-line coverage is 100%.