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 <skitt@redhat.com>
This commit is contained in:
Stephen Kitt 2023-07-27 14:31:05 +02:00
parent 4f874a224a
commit cc1670ddd2
No known key found for this signature in database
GPG Key ID: 1CC5FA453662A71D

View File

@ -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)
}
}
}