File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ package model
1515
1616import (
1717 "encoding/json"
18+ "errors"
1819 "fmt"
1920 "math"
2021 "regexp"
@@ -202,13 +203,23 @@ func ParseDuration(durationStr string) (Duration, error) {
202203
203204 // Parse the match at pos `pos` in the regex and use `mult` to turn that
204205 // into ms, then add that value to the total parsed duration.
206+ var overflowErr error
205207 m := func (pos int , mult time.Duration ) {
206208 if matches [pos ] == "" {
207209 return
208210 }
209211 n , _ := strconv .Atoi (matches [pos ])
212+
213+ // Check if the provided duration overflows time.Duration (> ~ 290years).
214+ if n > int ((1 << 63 - 1 )/ mult / time .Millisecond ) {
215+ overflowErr = errors .New ("duration out of range" )
216+ }
210217 d := time .Duration (n ) * time .Millisecond
211218 dur += d * mult
219+
220+ if dur < 0 {
221+ overflowErr = errors .New ("duration out of range" )
222+ }
212223 }
213224
214225 m (2 , 1000 * 60 * 60 * 24 * 365 ) // y
@@ -219,7 +230,7 @@ func ParseDuration(durationStr string) (Duration, error) {
219230 m (12 , 1000 ) // s
220231 m (14 , 1 ) // ms
221232
222- return Duration (dur ), nil
233+ return Duration (dur ), overflowErr
223234}
224235
225236func (d Duration ) String () string {
Original file line number Diff line number Diff line change @@ -282,6 +282,10 @@ func TestDuration_UnmarshalJSON(t *testing.T) {
282282 in : `"10y"` ,
283283 out : 10 * 365 * 24 * time .Hour ,
284284 },
285+ {
286+ in : `"289y"` ,
287+ out : 289 * 365 * 24 * time .Hour ,
288+ },
285289 }
286290
287291 for _ , c := range cases {
@@ -314,6 +318,10 @@ func TestParseBadDuration(t *testing.T) {
314318 "-1w" ,
315319 "1.5d" ,
316320 "d" ,
321+ "294y" ,
322+ "200y10400w" ,
323+ "107675d" ,
324+ "2584200h" ,
317325 "" ,
318326 }
319327
You can’t perform that action at this time.
0 commit comments