Skip to content

Commit

Permalink
model.Duration: disallow empty string (prometheus#247)
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
  • Loading branch information
roidelapluie authored Aug 3, 2020
1 parent 9c5aa1e commit 217fd62
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions model/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ var durationRE = regexp.MustCompile("^(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9
// ParseDuration parses a string into a time.Duration, assuming that a year
// always has 365d, a week always has 7d, and a day always has 24h.
func ParseDuration(durationStr string) (Duration, error) {
// Allow 0 without a unit.
if durationStr == "0" {
switch durationStr {
case "0":
// Allow 0 without a unit.
return 0, nil
case "":
return 0, fmt.Errorf("empty duration string")
}
matches := durationRE.FindStringSubmatch(durationStr)
if matches == nil {
Expand Down
2 changes: 2 additions & 0 deletions model/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func TestParseBadDuration(t *testing.T) {
"1y1m1d",
"-1w",
"1.5d",
"d",
"",
}

for _, c := range cases {
Expand Down

0 comments on commit 217fd62

Please sign in to comment.