Skip to content

Commit

Permalink
return appropriate error if overflowing duration when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
corylanou committed Sep 14, 2016
1 parent 0974bfc commit 71f0c7e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [#7297](https://github.com/influxdata/influxdb/issues/7297): Use consistent column output from the CLI for column formatted responses.
- [#7231](https://github.com/influxdata/influxdb/issues/7231): Duplicate parsing bug in ALTER RETENTION POLICY.
- [#7285](https://github.com/influxdata/influxdb/issues/7285): Correctly use password-type field in Admin UI. Thanks @dandv!
- [#2792](https://github.com/influxdata/influxdb/issues/2792): Exceeding max retention policy duration gives incorrect error message

## v1.0.0 [2016-09-08]

Expand Down
12 changes: 12 additions & 0 deletions influxql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2654,6 +2654,9 @@ func ParseDuration(s string) (time.Duration, error) {
i++
}

var measure int64
var unit string

// Parsing loop.
for i < len(a) {
// Find the number portion.
Expand All @@ -2672,15 +2675,18 @@ func ParseDuration(s string) (time.Duration, error) {
if err != nil {
return 0, ErrInvalidDuration
}
measure = n

// Extract the unit of measure.
// If the last two characters are "ms" then parse as milliseconds.
// Otherwise just use the last character as the unit of measure.
unit = string(a[i])
switch a[i] {
case 'u', 'µ':
d += time.Duration(n) * time.Microsecond
case 'm':
if i+1 < len(a) && a[i+1] == 's' {
unit = string(a[i:2])
d += time.Duration(n) * time.Millisecond
i += 2
continue
Expand All @@ -2699,6 +2705,12 @@ func ParseDuration(s string) (time.Duration, error) {
}
i++
}

// Check to see if we overflowed a duration
if d < 0 && !isNegative {
return 0, fmt.Errorf("overflowed duration %d%s: choose a smaller duration or INF", measure, unit)
}

if isNegative {
d = -d
}
Expand Down
1 change: 1 addition & 0 deletions influxql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,7 @@ func TestParser_ParseStatement(t *testing.T) {
{s: `ALTER RETENTION POLICY policy1`, err: `found EOF, expected ON at line 1, char 32`}, {s: `ALTER RETENTION POLICY policy1 ON`, err: `found EOF, expected identifier at line 1, char 35`},
{s: `ALTER RETENTION POLICY policy1 ON testdb`, err: `found EOF, expected DURATION, REPLICATION, SHARD, DEFAULT at line 1, char 42`},
{s: `ALTER RETENTION POLICY policy1 ON testdb REPLICATION 1 REPLICATION 2`, err: `found duplicate REPLICATION option at line 1, char 56`},
{s: `ALTER RETENTION POLICY policy1 ON testdb DURATION 15251w`, err: `overflowed duration 15251w: choose a smaller duration or INF at line 1, char 51`},
{s: `SET`, err: `found EOF, expected PASSWORD at line 1, char 5`},
{s: `SET PASSWORD`, err: `found EOF, expected FOR at line 1, char 14`},
{s: `SET PASSWORD something`, err: `found something, expected FOR at line 1, char 14`},
Expand Down

0 comments on commit 71f0c7e

Please sign in to comment.