diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-03 23:12:15 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-03 23:12:15 +0200 |
| commit | b64e20fe58da8d9afecda06c2d701096b267cca0 (patch) | |
| tree | 39cd1eb61f527de784e9bb873505d3b52e3312ba /internal/duration | |
| parent | e96b0b370bcdd55ad2d5b20187e4bbae78785ff2 (diff) | |
Task 352: expand comprehensive test coverage
Diffstat (limited to 'internal/duration')
| -rw-r--r-- | internal/duration/parse.go | 8 | ||||
| -rw-r--r-- | internal/duration/parse_test.go | 2 |
2 files changed, 10 insertions, 0 deletions
diff --git a/internal/duration/parse.go b/internal/duration/parse.go index f2febe8..f144ac1 100644 --- a/internal/duration/parse.go +++ b/internal/duration/parse.go @@ -11,6 +11,11 @@ import ( var bareIntegerPattern = regexp.MustCompile(`^[+-]?\d+$`) +const ( + maxInt64 = int64(^uint64(0) >> 1) + minInt64 = -maxInt64 - 1 +) + // Parse converts duration text into time.Duration. // Go-style durations (e.g. "1h30m") are supported, and bare integers are seconds. func Parse(value string) (time.Duration, error) { @@ -24,6 +29,9 @@ func Parse(value string) (time.Duration, error) { if err != nil { return 0, fmt.Errorf("parse seconds %q: %w", value, err) } + if seconds > maxInt64/int64(time.Second) || seconds < minInt64/int64(time.Second) { + return 0, fmt.Errorf("duration seconds %q overflows time.Duration", value) + } return time.Duration(seconds) * time.Second, nil } diff --git a/internal/duration/parse_test.go b/internal/duration/parse_test.go index 3b812cd..931f904 100644 --- a/internal/duration/parse_test.go +++ b/internal/duration/parse_test.go @@ -63,6 +63,8 @@ func TestParseInvalidDurations(t *testing.T) { " ", "abc", "1h30x", + "9223372036854775807", + "-9223372036854775808", } for _, input := range tests { |
