diff --git a/expression/builtin_string.go b/expression/builtin_string.go index f20c7fe27a377..fd429d893a354 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -1210,6 +1210,11 @@ func (b *builtinSubstringIndexSig) evalString(row chunk.Row) (d string, isNull b } else { // 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 < end { start = end - count } 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)`)