Skip to content

Commit

Permalink
expression: Wrong type value in INET6_ATON/INET_ATON should return er…
Browse files Browse the repository at this point in the history
…rors (#32849)

close #32221
  • Loading branch information
fanrenhoo authored Apr 26, 2022
1 parent ad2ce1d commit 28ea194
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
12 changes: 6 additions & 6 deletions expression/builtin_miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (b *builtinInetAtonSig) evalInt(row chunk.Row) (int64, bool, error) {
}
// ip address should not end with '.'.
if len(val) == 0 || val[len(val)-1] == '.' {
return 0, true, nil
return 0, false, errWrongValueForType.GenWithStackByArgs("string", val, "inet_aton")
}

var (
Expand All @@ -447,17 +447,17 @@ func (b *builtinInetAtonSig) evalInt(row chunk.Row) (int64, bool, error) {
digit := uint64(c - '0')
byteResult = byteResult*10 + digit
if byteResult > 255 {
return 0, true, nil
return 0, false, errWrongValueForType.GenWithStackByArgs("string", val, "inet_aton")
}
} else if c == '.' {
dotCount++
if dotCount > 3 {
return 0, true, nil
return 0, false, errWrongValueForType.GenWithStackByArgs("string", val, "inet_aton")
}
result = (result << 8) + byteResult
byteResult = 0
} else {
return 0, true, nil
return 0, false, errWrongValueForType.GenWithStackByArgs("string", val, "inet_aton")
}
}
// 127 -> 0.0.0.127
Expand Down Expand Up @@ -568,12 +568,12 @@ func (b *builtinInet6AtonSig) evalString(row chunk.Row) (string, bool, error) {
}

if len(val) == 0 {
return "", true, nil
return "", false, errWrongValueForType.GenWithStackByArgs("string", val, "inet_aton6")
}

ip := net.ParseIP(val)
if ip == nil {
return "", true, nil
return "", false, errWrongValueForType.GenWithStackByArgs("string", val, "inet_aton6")
}

var isMappedIpv6 bool
Expand Down
20 changes: 16 additions & 4 deletions expression/builtin_miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/testkit/testutil"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -55,8 +56,12 @@ func TestInetAton(t *testing.T) {
f, err := fc.getFunction(ctx, datumsToConstants(tt["Input"]))
require.NoError(t, err)
d, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
testutil.DatumEqual(t, tt["Expected"][0], d)
if tt["Expected"][0].IsNull() && !tt["Input"][0].IsNull() {
require.True(t, terror.ErrorEqual(err, errWrongValueForType))
} else {
require.NoError(t, err)
testutil.DatumEqual(t, tt["Expected"][0], d)
}
}
}

Expand Down Expand Up @@ -287,6 +292,8 @@ func TestInet6AtoN(t *testing.T) {
{"::ffff:1.2.3.4", []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x01, 0x02, 0x03, 0x04}},
{"", nil},
{"Not IP address", nil},
{"1.0002.3.4", nil},
{"1.2.256", nil},
{"::ffff:255.255.255.255", []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
}
fc := funcs[ast.Inet6Aton]
Expand All @@ -295,8 +302,13 @@ func TestInet6AtoN(t *testing.T) {
f, err := fc.getFunction(ctx, datumsToConstants([]types.Datum{ip}))
require.NoError(t, err)
result, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
testutil.DatumEqual(t, types.NewDatum(test.expect), result)
expect := types.NewDatum(test.expect)
if expect.IsNull() {
require.True(t, terror.ErrorEqual(err, errWrongValueForType))
} else {
require.NoError(t, err)
testutil.DatumEqual(t, expect, result)
}
}

var argNull types.Datum
Expand Down
5 changes: 2 additions & 3 deletions expression/builtin_miscellaneous_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ var vecBuiltinMiscellaneousCases = map[string][]vecExprBenchCase{
},
ast.InetAton: {
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{&ipv4StrGener{newDefaultRandGen()}}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}},
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{
newSelectStringGener(
[]string{
"11.11.11.11.", // last char is .
"266.266.266.266", // int in string exceed 255
"11.11.11.11",
"255.255.255.255",
"127",
".122",
".123.123",
Expand Down

0 comments on commit 28ea194

Please sign in to comment.