File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ package model
15
15
16
16
import (
17
17
"encoding/json"
18
+ "errors"
18
19
"fmt"
19
20
"math"
20
21
"regexp"
@@ -202,13 +203,23 @@ func ParseDuration(durationStr string) (Duration, error) {
202
203
203
204
// Parse the match at pos `pos` in the regex and use `mult` to turn that
204
205
// into ms, then add that value to the total parsed duration.
206
+ var overflowErr error
205
207
m := func (pos int , mult time.Duration ) {
206
208
if matches [pos ] == "" {
207
209
return
208
210
}
209
211
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
+ }
210
217
d := time .Duration (n ) * time .Millisecond
211
218
dur += d * mult
219
+
220
+ if dur < 0 {
221
+ overflowErr = errors .New ("duration out of range" )
222
+ }
212
223
}
213
224
214
225
m (2 , 1000 * 60 * 60 * 24 * 365 ) // y
@@ -219,7 +230,7 @@ func ParseDuration(durationStr string) (Duration, error) {
219
230
m (12 , 1000 ) // s
220
231
m (14 , 1 ) // ms
221
232
222
- return Duration (dur ), nil
233
+ return Duration (dur ), overflowErr
223
234
}
224
235
225
236
func (d Duration ) String () string {
Original file line number Diff line number Diff line change @@ -282,6 +282,10 @@ func TestDuration_UnmarshalJSON(t *testing.T) {
282
282
in : `"10y"` ,
283
283
out : 10 * 365 * 24 * time .Hour ,
284
284
},
285
+ {
286
+ in : `"289y"` ,
287
+ out : 289 * 365 * 24 * time .Hour ,
288
+ },
285
289
}
286
290
287
291
for _ , c := range cases {
@@ -314,6 +318,10 @@ func TestParseBadDuration(t *testing.T) {
314
318
"-1w" ,
315
319
"1.5d" ,
316
320
"d" ,
321
+ "294y" ,
322
+ "200y10400w" ,
323
+ "107675d" ,
324
+ "2584200h" ,
317
325
"" ,
318
326
}
319
327
You can’t perform that action at this time.
0 commit comments