Repository navigation
Conversation
In IEEE-754 float64, math.MaxInt64 (2^63 - 1) rounds up to 2^63 (9223372036854775808.0). Consequently, FloatToTime(math.Exp2(63) / 1e9) passed `timestamp > math.MaxInt64` and overflowed int64(timestamp) to math.MinInt64, silently wrapping year 2262 to 1677-09-21 instead of returning an overflow error. Use `timestamp >= math.Exp2(63)` so 2^63 nanoseconds is rejected as overflow while preserving the full representable int64 range. Signed-off-by: Yan Chen <yan8925298@gmail.com>
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.
Summary
Fix an
int64boundary overflow inhelpers/templates.FloatToTime:In IEEE-754
float64,math.MaxInt64(2^63 - 1 = 9223372036854775807) rounds up to2^63(9223372036854775808.0). Whenv = math.Exp2(63) / 1e9(9223372036.8547764),v * 1e9equals9223372036854775808.0, sotimestamp > math.MaxInt64evaluates tofalse. The subsequentint64(timestamp)overflows tomath.MinInt64, causingFloatToTimeandHumanizeTimestampto silently wrap year 2262 back to1677-09-21 00:12:43.146 +0000 UTCinstead of returning the overflow error.Checking
timestamp >= math.Exp2(63)(matchingexpfmt/openmetrics_parse.go) rejects2^63nanoseconds while keeping the full representableint64range intact (math.Nextafter(math.Exp2(63)/1e9, 0)through-math.Exp2(63)/1e9).Verification
TestHumanizeTimestampErrorand addedTestFloatToTimeInt64Boundaryinhelpers/templates/time_test.go.An error is expected but got nilformath.Exp2(63)/1e9).go test -race -count=1 -v ./helpers/templates/...,go vet ./helpers/templates/..., andgofmt -l ./helpers/templatesall pass.Note: AI-assisted diagnosis and drafting; all code, tests, and local verification were reviewed and executed locally.