-
Notifications
You must be signed in to change notification settings - Fork 229
Fix negative duration parsing #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ var ( | |
"w": 7 * 24 * time.Hour, | ||
} | ||
|
||
durationMatcher = regexp.MustCompile(`((\d+)\s*([A-Za-zµ]+))`) | ||
durationMatcher = regexp.MustCompile(`(((?:-\s?)?\d+)\s*([A-Za-zµ]+))`) | ||
) | ||
|
||
// IsDuration returns true if the provided string is a valid duration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ParseDuration uses See https://go.dev/play/p/fCur5WS4zK5 for the type of nonsense To actually address the problem of decimal junk being accepted but ignored, we need the regex to fully define what we allow, and be pinned to the beginning / end of the string. Something like this
|
||
|
@@ -94,10 +94,18 @@ func ParseDuration(cand string) (time.Duration, error) { | |
ok := false | ||
for _, match := range durationMatcher.FindAllStringSubmatch(cand, -1) { | ||
|
||
factor, err := strconv.Atoi(match[2]) // converts string to int | ||
// remove possible leading - and spaces | ||
value, negative := strings.CutPrefix(match[2], "-") | ||
|
||
// if the string is a valid duration, parse it | ||
factor, err := strconv.Atoi(strings.TrimSpace(value)) // converts string to int | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if negative { | ||
factor = -factor | ||
} | ||
unit := strings.ToLower(strings.TrimSpace(match[3])) | ||
|
||
for _, variants := range timeUnits { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you clarify what additional values this allows? it's not clear to me that accepting new internal whitespaces between
-
and the number is a good idea here