Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segmentation fault of SUBSTRING (#1903) #1915

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions dbms/src/Functions/FunctionsString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,14 @@ struct SubstringUTF8Impl
ColumnString::Offset bytes_start = 0;
ColumnString::Offset bytes_length = 0;
size_t start = 0;
if(original_start >= 0)
if (original_start >= 0)
start = original_start;
else
{
// set the start as string_length - abs(original_start) + 1
std::vector<ColumnString::Offset> start_offsets;
ColumnString::Offset current = prev_offset;
while (current < offsets[i] -1)
while (current < offsets[i] - 1)
{
start_offsets.push_back(current);
if (data[current] < 0xBF)
Expand All @@ -500,9 +500,18 @@ struct SubstringUTF8Impl
else
current += 1;
}
if (static_cast<size_t>(-original_start) > start_offsets.size())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have test when original_start == INT64_MIN?

@windtalker

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, so we should check it explicitly here.

{
// return empty string
res_data.resize(res_data.size() + 1);
res_data[res_offset] = 0;
res_offset++;
res_offsets[i] = res_offset;
continue;
}
start = start_offsets.size() + original_start + 1;
pos = start;
j = start_offsets[start-1];
j = start_offsets[start - 1];
}
while (j < offsets[i] - 1)
{
Expand Down
23 changes: 23 additions & 0 deletions tests/fullstack-test/expr/substring_utf8.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
mysql> drop table if exists test.t
mysql> create table test.t(a char(10))
mysql> insert into test.t values(''), ('abc')
mysql> alter table test.t set tiflash replica 1

func> wait_table test t

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; select * from test.t where substring(a, -3, 4) = 'abc'
a
abc

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; select * from test.t where substring(a, -3, 2) = 'ab'
a
abc

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; select * from test.t where substring(a, -4, 3) = 'abc'
# Empty

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; select count(*) from test.t where substring(a, 0, 3) = '' order by a
count(*)
2

mysql> drop table if exists test.t