Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed int64 overflow for timestamp in v1/api parseDuration and parseTime #2501

Merged
merged 3 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions web/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -477,8 +478,11 @@ func respondError(w http.ResponseWriter, apiErr *apiError, data interface{}) {

func parseTime(s string) (model.Time, error) {
if t, err := strconv.ParseFloat(s, 64); err == nil {
ts := int64(t * float64(time.Second))
return model.TimeFromUnixNano(ts), nil
ts := t * float64(time.Second)
if ts >= float64(math.MaxInt64) || ts <= float64(math.MinInt64) {
return 0, fmt.Errorf("cannot parse %q to a valid timestamp. It overflows int64", s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be < and > and not <= and >=?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does not make any difference, but I agree that it will be never equal (:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you are right, it makes difference (: My bad. No error should happen on equal....

}
return model.TimeFromUnixNano(int64(ts)), nil
}
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
return model.TimeFromUnixNano(t.UnixNano()), nil
Expand All @@ -488,7 +492,11 @@ func parseTime(s string) (model.Time, error) {

func parseDuration(s string) (time.Duration, error) {
if d, err := strconv.ParseFloat(s, 64); err == nil {
return time.Duration(d * float64(time.Second)), nil
ts := d * float64(time.Second)
if ts >= float64(math.MaxInt64) || ts <= float64(math.MinInt64) {
return 0, fmt.Errorf("cannot parse %q to a valid duration. It overflows int64", s)
}
return time.Duration(ts), nil
}
if d, err := model.ParseDuration(s); err == nil {
return time.Duration(d), nil
Expand Down
30 changes: 27 additions & 3 deletions web/api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ func TestEndpoints(t *testing.T) {
},
errType: errorBadData,
},
// Start overflows int64 internally
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

End all comments with period please.

{
endpoint: api.queryRange,
query: url.Values{
"query": []string{"time()"},
"start": []string{"148966367200.372"},
"end": []string{"1489667272.372"},
"step": []string{"1"},
},
errType: errorBadData,
},
{
endpoint: api.labelValues,
params: map[string]string{
Expand Down Expand Up @@ -593,15 +604,20 @@ func TestParseTime(t *testing.T) {
}, {
input: "30s",
fail: true,
}, {
// Internal int64 overflow
input: "-148966367200.372",
fail: true,
}, {
// Internal int64 overflow
input: "148966367200.372",
fail: true,
}, {
input: "123",
result: time.Unix(123, 0),
}, {
input: "123.123",
result: time.Unix(123, 123000000),
}, {
input: "123.123",
result: time.Unix(123, 123000000),
}, {
input: "2015-06-03T13:21:58.555Z",
result: ts,
Expand Down Expand Up @@ -643,6 +659,14 @@ func TestParseDuration(t *testing.T) {
}, {
input: "2015-06-03T13:21:58.555Z",
fail: true,
}, {
// Internal int64 overflow
input: "-148966367200.372",
fail: true,
}, {
// Internal int64 overflow
input: "148966367200.372",
fail: true,
}, {
input: "123",
result: 123 * time.Second,
Expand Down