From cc1670ddd2b5d610ea19f2bb8fa2569729179714 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Thu, 27 Jul 2023 14:31:05 +0200 Subject: [PATCH] Limit intstr.Parse() to 32-bit integer parsing Currently, intstr.Parse() attempts to parse its input using strconv.Atoi(); if that succeeds, it stores the result using FromInt(). This means that integer values which fit in int but not in int32 end up corrupted, even though IntOrString can store them correctly (as a string). This changes intstr.Parse() to use strconv.ParseInt(), limiting the input size to 32 bits. Thus values larger than that are stored correctly using a string. Signed-off-by: Stephen Kitt --- .../pkg/util/intstr/intstr_test.go | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr_test.go b/staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr_test.go index 217d3cf4ecf..197e58d2d41 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr_test.go @@ -264,3 +264,63 @@ func TestGetValueFromIntOrPercentNil(t *testing.T) { t.Errorf("expected error got none") } } + +func TestParse(t *testing.T) { + tests := []struct { + input string + output IntOrString + }{ + { + input: "0", + output: IntOrString{Type: Int, IntVal: 0}, + }, + { + input: "2147483647", // math.MaxInt32 + output: IntOrString{Type: Int, IntVal: 2147483647}, + }, + { + input: "-2147483648", // math.MinInt32 + output: IntOrString{Type: Int, IntVal: -2147483648}, + }, + { + input: "2147483648", // math.MaxInt32+1 + output: IntOrString{Type: String, StrVal: "2147483648"}, + }, + { + input: "-2147483649", // math.MinInt32-1 + output: IntOrString{Type: String, StrVal: "-2147483649"}, + }, + { + input: "9223372036854775807", // math.MaxInt64 + output: IntOrString{Type: String, StrVal: "9223372036854775807"}, + }, + { + input: "-9223372036854775808", // math.MinInt64 + output: IntOrString{Type: String, StrVal: "-9223372036854775808"}, + }, + { + input: "9223372036854775808", // math.MaxInt64+1 + output: IntOrString{Type: String, StrVal: "9223372036854775808"}, + }, + { + input: "-9223372036854775809", // math.MinInt64-1 + output: IntOrString{Type: String, StrVal: "-9223372036854775809"}, + }, + } + + for i, test := range tests { + t.Logf("test case %d", i) + value := Parse(test.input) + if test.output.Type != value.Type { + t.Errorf("expected type %d (%v), but got %d (%v)", test.output.Type, test.output, value.Type, value) + continue + } + if value.Type == Int && test.output.IntVal != value.IntVal { + t.Errorf("expected int value %d (%v), but got %d (%v)", test.output.IntVal, test.output, value.IntVal, value) + continue + } + if value.Type == String && test.output.StrVal != value.StrVal { + t.Errorf("expected string value %q (%v), but got %q (%v)", test.output.StrVal, test.output, value.StrVal, value) + } + } +}