Skip to content

Commit

Permalink
types: clip to zero when convert negative decimal to uint (#41791) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Mar 1, 2023
1 parent df32e3a commit 637e538
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 2 additions & 3 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func convertDecimalStrToUint(sc *stmtctx.StatementContext, str string, upperBoun
if intStr == "" {
intStr = "0"
}
if sc.ShouldClipToZero() && intStr[0] == '-' {
if intStr[0] == '-' {
return 0, overflow(str, tp)
}

Expand All @@ -256,8 +256,7 @@ func convertDecimalStrToUint(sc *stmtctx.StatementContext, str string, upperBoun
round++
}

upperBound -= round
upperStr := strconv.FormatUint(upperBound, 10)
upperStr := strconv.FormatUint(upperBound-round, 10)
if len(intStr) > len(upperStr) ||
(len(intStr) == len(upperStr) && intStr > upperStr) {
return upperBound, overflow(str, tp)
Expand Down
13 changes: 11 additions & 2 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,16 +1273,25 @@ func TestConvertDecimalStrToUint(t *testing.T) {
{"9223372036854775807.4999", 9223372036854775807, true},
{"18446744073709551614.55", 18446744073709551615, true},
{"18446744073709551615.344", 18446744073709551615, true},
{"18446744073709551615.544", 0, false},
{"18446744073709551615.544", 18446744073709551615, false},
{"-111.111", 0, false},
{"-10000000000000000000.0", 0, false},
}
for _, ca := range cases {
result, err := convertDecimalStrToUint(&stmtctx.StatementContext{}, ca.input, math.MaxUint64, 0)
if !ca.succ {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, ca.result, result)
}
require.Equal(t, ca.result, result, "input=%v", ca.input)
}

result, err := convertDecimalStrToUint(&stmtctx.StatementContext{}, "-99.0", math.MaxUint8, 0)
require.Error(t, err)
require.Equal(t, uint64(0), result)

result, err = convertDecimalStrToUint(&stmtctx.StatementContext{}, "-100.0", math.MaxUint8, 0)
require.Error(t, err)
require.Equal(t, uint64(0), result)
}

0 comments on commit 637e538

Please sign in to comment.