model: stop Time JSON wrapping around at the edges of its range - #997
Open
Rohilalala wants to merge 1 commit into
Open
Rohilalala wants to merge 1 commit into
Rohilalala wants to merge 1 commit into
Conversation
Time.UnmarshalJSON multiplied the seconds by 1000 without a range check, so a time outside the int64 range of milliseconds wrapped around to the opposite sign. Latest and Earliest hit this through their own JSON: Time.String() formatted through float64, which rounds times far from the epoch, so Latest marshalled as 9223372036854776 and came back as a time near Earliest. Return an error for out-of-range input, except the two values older versions wrote for Latest and Earliest, which now decode to Latest and Earliest. Format Time.String() from integers so that every Time round-trips; the output is unchanged for times within 2^40 milliseconds of the epoch. Signed-off-by: Aditya <205600203+Rohilalala@users.noreply.github.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.
Time.UnmarshalJSONmultiplied the seconds by 1000 with no range check, so a time outside the int64 range of milliseconds wrapped around to the opposite sign.LatestandEarliesthit this through their own JSON.Time.String()formatted through float64, which rounds times far from the epoch, soLatestmarshalled as9223372036854776and came back as-9223372036854775616, a time nearEarliest.LatestandEarliest, which now decode toLatestandEarliest. Output from older servers, such asZeroSamplePair, stays readable.Time.String()now formats from integers, so everyTimeround-trips. The output is unchanged for times within 2^40 ms of the epoch: I compared it against the old formatter on 5M random values, and it is faster (26 ns vs 35 ns, 1 alloc).Visible change:
LatestandEarliestnow encode exactly, as9223372036854775.807and-9223372036854775.808, instead of the rounded values. Older versions decode the new text to the same times.