From 14eb73f3a169eaba82815769a03710a05f4ab1b5 Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Fri, 28 Sep 2018 14:41:34 +0800 Subject: [PATCH 1/4] expression: fix painc on substring_index --- expression/builtin_string.go | 3 +++ expression/integration_test.go | 1 + 2 files changed, 4 insertions(+) diff --git a/expression/builtin_string.go b/expression/builtin_string.go index f20c7fe27a377..74fb0baa969c2 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -1208,6 +1208,9 @@ func (b *builtinSubstringIndexSig) evalString(row chunk.Row) (d string, isNull b end = count } } else { + if count <= -int64(math.Pow(2, 63)) { + return "", false, nil + } // If count is negative, everything to the right of the final delimiter (counting from the right) is returned. count = -count if count < end { diff --git a/expression/integration_test.go b/expression/integration_test.go index 089732337147e..cdb8063583d94 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -736,6 +736,7 @@ func (s *testIntegrationSuite) TestStringBuiltin(c *C) { result.Check(testkit.Rows("www.pingcap 12345 45 2017 01:01")) result = tk.MustQuery(`select substring_index('www.pingcap.com', '.', 0), substring_index('www.pingcap.com', '.', 100), substring_index('www.pingcap.com', '.', -100)`) result.Check(testkit.Rows(" www.pingcap.com www.pingcap.com")) + tk.MustQuery(`select substring_index('xyz', 'abc', 9223372036854775808)`).Check(testkit.Rows(``)) result = tk.MustQuery(`select substring_index('www.pingcap.com', 'd', 1), substring_index('www.pingcap.com', '', 1), substring_index('', '.', 1)`) result.Check(testutil.RowsWithSep(",", "www.pingcap.com,,")) result = tk.MustQuery(`select substring_index(null, '.', 1), substring_index('www.pingcap.com', null, 1), substring_index('www.pingcap.com', '.', null)`) From 028df836dc951a5569fbb584d66cb57286926e44 Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 8 Oct 2018 15:05:13 +0800 Subject: [PATCH 2/4] address comment --- expression/builtin_string.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/expression/builtin_string.go b/expression/builtin_string.go index 74fb0baa969c2..d26cd0de439e4 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -1208,7 +1208,8 @@ func (b *builtinSubstringIndexSig) evalString(row chunk.Row) (d string, isNull b end = count } } else { - if count <= -int64(math.Pow(2, 63)) { + if count <= -(1 << 63) { + // -count overflows max int64, returns an empty string. return "", false, nil } // If count is negative, everything to the right of the final delimiter (counting from the right) is returned. From 6c8a611d80e2d692c98f9eaa4db07ef235f0371f Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 8 Oct 2018 17:57:13 +0800 Subject: [PATCH 3/4] address comment --- expression/builtin_string.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/builtin_string.go b/expression/builtin_string.go index d26cd0de439e4..b5b0d8ea7589d 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -1208,7 +1208,7 @@ func (b *builtinSubstringIndexSig) evalString(row chunk.Row) (d string, isNull b end = count } } else { - if count <= -(1 << 63) { + if -count < 0 { // -count overflows max int64, returns an empty string. return "", false, nil } From f706ceb1226c348165354b74504bb9b7bb0b2c5e Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 8 Oct 2018 17:59:07 +0800 Subject: [PATCH 4/4] tiny refine --- expression/builtin_string.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/expression/builtin_string.go b/expression/builtin_string.go index b5b0d8ea7589d..fd429d893a354 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -1208,12 +1208,13 @@ func (b *builtinSubstringIndexSig) evalString(row chunk.Row) (d string, isNull b end = count } } else { - if -count < 0 { + // If count is negative, everything to the right of the final delimiter (counting from the right) is returned. + count = -count + if count < 0 { // -count overflows max int64, returns an empty string. return "", false, nil } - // If count is negative, everything to the right of the final delimiter (counting from the right) is returned. - count = -count + if count < end { start = end - count }