Skip to content

Commit

Permalink
expression: fix painc on substring_index (#7806) (#7896)
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason committed Oct 15, 2018
1 parent 2af0f18 commit 0b4762c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,11 @@ func (b *builtinSubstringIndexSig) evalString(row types.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
}
Expand Down
1 change: 1 addition & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)`)
Expand Down

0 comments on commit 0b4762c

Please sign in to comment.