Skip to content

json: report integer duration overflow against time.Duration#167

Open
sueun-dev wants to merge 1 commit into
segmentio:masterfrom
sueun-dev:fix-duration-overflow-type
Open

json: report integer duration overflow against time.Duration#167
sueun-dev wants to merge 1 commit into
segmentio:masterfrom
sueun-dev:fix-duration-overflow-type

Conversation

@sueun-dev

Copy link
Copy Markdown

decodeDuration parses integer durations with parseInt(b, durationType), but on error it returned inputError(b, int32Type) and then checked v < math.MinInt64 || v > math.MaxInt64. v is an int64, so that branch is unreachable (staticcheck SA4003, the lint reported in #150), and both error paths reported int32 instead of time.Duration.

Decoding an integer that overflows int64 into a time.Duration field showed this:

before: json: cannot unmarshal "100000000000000000000000}" into Go struct field ...D of type int32
after:  json: cannot unmarshal number 100000000000000000000000} overflows into Go struct field ...D of type time.Duration

time.Duration is backed by int64, so decodeDuration can propagate the parseInt error the same way decodeInt64 does, with no separate bounds check. Added a regression test; go test ./... passes and staticcheck no longer flags the line.

Fixes #150

decodeDuration parses integer durations with parseInt(b, durationType),
but on error it returned inputError(b, int32Type) and then checked
v < math.MinInt64 || v > math.MaxInt64. v is an int64, so that branch is
unreachable (staticcheck SA4003, the lint in segmentio#150), and both error paths
reported int32 instead of time.Duration.

time.Duration is backed by int64, so propagate the parseInt error the
same way decodeInt64 does. Add a regression test.

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

Lint about impossible condition

1 participant