-
-
Notifications
You must be signed in to change notification settings - Fork 69
fix(flags): interpret bare numeric timeout as seconds #1675
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package flags | ||
|
|
||
| import ( | ||
| "math" | ||
| "strconv" | ||
| "testing" | ||
| ) | ||
|
|
||
| // FuzzIsPureNumeric verifies that isPureNumeric never panics and that | ||
| // when it returns true, strconv.ParseFloat either succeeds with a finite | ||
| // value or fails only with a range error. | ||
| func FuzzIsPureNumeric(f *testing.F) { | ||
| // Valid bare numbers. | ||
| f.Add("0") | ||
| f.Add("42") | ||
| f.Add("300") | ||
| f.Add("1.5") | ||
| f.Add(".5") | ||
| f.Add("1.") | ||
| f.Add("-10") | ||
| f.Add("+3") | ||
| f.Add("-1.5") | ||
| f.Add("+.5") | ||
| f.Add("-.5") | ||
| f.Add("+0") | ||
| f.Add("-0") | ||
|
|
||
| // Invalid: multiple dots and misplaced signs. | ||
| f.Add("1.2.3") | ||
| f.Add("1..2") | ||
| f.Add("1-2") | ||
| f.Add("12+") | ||
| f.Add("1+2") | ||
| f.Add("5-") | ||
| f.Add("+-1") | ||
| f.Add("-+5") | ||
| f.Add("++3") | ||
|
|
||
| // Other invalid cases (units, letters, control chars, long inputs, etc.). | ||
| f.Add("") | ||
| f.Add(".") | ||
| f.Add("+") | ||
| f.Add("-") | ||
| f.Add("+.") | ||
| f.Add("-.5") | ||
| f.Add("1a") | ||
| f.Add("1e3") | ||
| f.Add("1E-3") | ||
| f.Add("Inf") | ||
| f.Add("NaN") | ||
| f.Add("\x00") | ||
| f.Add("1\x002") | ||
| f.Add("¹²³") // Unicode digits — must be rejected | ||
| f.Add("1,000") | ||
| f.Add(" 42") | ||
| f.Add("1.2.3.4.5") | ||
| f.Add(string(make([]byte, 1024))) // long string of zeros (will be filled in fuzzer) | ||
|
|
||
| // Duration-like inputs. | ||
| f.Add("30s") | ||
| f.Add("2m") | ||
| f.Add("1h") | ||
| f.Add("1d") | ||
|
|
||
| f.Fuzz(func(t *testing.T, input string) { | ||
| result := isPureNumeric(input) | ||
|
|
||
| if result { | ||
| // When true, ParseFloat must succeed with finite value or fail only on range. | ||
| val, err := strconv.ParseFloat(input, 64) | ||
| if err != nil { | ||
| numErr, ok := err.(*strconv.NumError) | ||
| if ok && numErr.Err == strconv.ErrRange { | ||
| return // out-of-range magnitude is acceptable | ||
| } | ||
|
|
||
| t.Errorf("isPureNumeric(%q) = true, but strconv.ParseFloat returned non-range error: %v", input, err) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if math.IsNaN(val) || math.IsInf(val, 0) { | ||
| t.Errorf("isPureNumeric(%q) = true, but parsed value %v is NaN or Inf", input, val) | ||
| } | ||
| } | ||
|
|
||
| // We do not assert the inverse (ParseFloat success ⇒ isPureNumeric true) | ||
| // because we intentionally reject scientific notation, Inf, NaN, etc. | ||
| // Those should fall through to normal duration parsing. | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.