Skip to content
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

BUG 1825851: Ensure malformed IntOrPercent returns error #562

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -692,9 +692,9 @@ func getIntOrPercentValue(intOrStr *intstr.IntOrString) (int, bool, error) {
case intstr.String:
isPercent := false
s := intOrStr.StrVal
if strings.Contains(s, "%") {
if strings.HasSuffix(s, "%") {
isPercent = true
s = strings.Replace(intOrStr.StrVal, "%", "", -1)
s = strings.TrimSuffix(intOrStr.StrVal, "%")
}
v, err := strconv.Atoi(s)
if err != nil {
Expand Down
Expand Up @@ -2648,6 +2648,7 @@ func TestGetIntOrPercentValue(t *testing.T) {
intInString30 := intstr.FromString("30")
invalidStringA := intstr.FromString("a")
invalidStringAPercent := intstr.FromString("a%")
invalidStringNumericPercent := intstr.FromString("1%0")

testCases := []struct {
name string
Expand Down Expand Up @@ -2691,6 +2692,13 @@ func TestGetIntOrPercentValue(t *testing.T) {
expectedPercent: true,
expectedError: fmt.Errorf("invalid value \"a%%\": strconv.Atoi: parsing \"a\": invalid syntax"),
},
{
name: "with an '1%0' string",
in: &invalidStringNumericPercent,
expectedValue: 0,
expectedPercent: false,
expectedError: fmt.Errorf("invalid value \"1%%0\": strconv.Atoi: parsing \"1%%0\": invalid syntax"),
},
}

for _, tc := range testCases {
Expand Down