Skip to content

model: stop Time JSON wrapping around at the edges of its range - #997

Open
Rohilalala wants to merge 1 commit into
prometheus:mainfrom
Rohilalala:fix/model-time-json-range
Open

Rohilalala wants to merge 1 commit into
prometheus:mainfrom
Rohilalala:fix/model-time-json-range

Conversation

@Rohilalala

Copy link
Copy Markdown

Time.UnmarshalJSON multiplied the seconds by 1000 with no 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 -9223372036854775616, a time near Earliest.

  • Out-of-range input now returns an error.
  • The exception is the two values older versions wrote for Latest and Earliest, which now decode to Latest and Earliest. Output from older servers, such as ZeroSamplePair, stays readable.
  • Time.String() now formats from integers, so every Time round-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: Latest and Earliest now encode exactly, as 9223372036854775.807 and -9223372036854775.808, instead of the rounded values. Older versions decode the new text to the same times.

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>
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.

1 participant