Skip to content

Commit

Permalink
fix lpad/rpad coredump because of trailing zero (#3881) (#3883)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jan 18, 2022
1 parent ef74a74 commit a3f4db4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
28 changes: 14 additions & 14 deletions dbms/src/Functions/FunctionsString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2989,9 +2989,9 @@ class tidbPadImpl
per_pad_offset = (per_pad_offset + pad_bytes) % (padding_size - 1);
--left;
}
// Including the tailing '\0'.
copyResult(res, res_offset, data, 0, data_size);
res_offset += data_size;
// The tailing '\0' will be handled later.
copyResult(res, res_offset, data, 0, data_size - 1);
res_offset += data_size - 1;
}
else
{
Expand All @@ -3006,8 +3006,6 @@ class tidbPadImpl
per_pad_offset = (per_pad_offset + pad_bytes) % (padding_size - 1);
--left;
}
res[res_offset] = '\0';
res_offset++;
}
}
else
Expand All @@ -3021,9 +3019,11 @@ class tidbPadImpl
per_pad_offset += pad_bytes;
++left;
}
res[res_offset] = '\0';
res_offset++;
}
// Add trailing zero.
res.resize(res.size() + 1);
res[res_offset] = '\0';
res_offset++;
return false;
}

Expand Down Expand Up @@ -3059,9 +3059,9 @@ class tidbPadImpl
++res_offset;
--left;
}
// Including the tailing '\0'.
copyResult(res, res_offset, data, 0, data_size);
res_offset += data_size;
// The tailing '\0' will be handled later.
copyResult(res, res_offset, data, 0, data_size - 1);
res_offset += data_size - 1;
}
else
{
Expand All @@ -3081,17 +3081,17 @@ class tidbPadImpl
++res_offset;
--left;
}
res[res_offset] = '\0';
res_offset++;
}
}
else
{
copyResult(res, res_offset, data, 0, tmp_target_len);
res_offset += tmp_target_len;
res[res_offset] = '\0';
res_offset++;
}
// Add trailing zero.
res.resize(res.size() + 1);
res[res_offset] = '\0';
res_offset++;
return false;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/fullstack-test/expr/pad.test
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ mysql> insert into test.t1 values(5, '杭州', '西湖')
mysql> insert into test.t1 values(5, '杭州', 'test')
mysql> insert into test.t1 values(5, '', '西湖')
mysql> alter table test.t1 set tiflash replica 1
mysql> drop table if exists test.t2
mysql> create table test.t2(c1 varchar(100))
mysql> insert into test.t2 values('b')
mysql> alter table test.t2 set tiflash replica 1
func> wait_table test t
func> wait_table test t1
func> wait_table test t2

mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select lpad(c2, c1, c3) from test.t
+------------------+
Expand Down Expand Up @@ -89,3 +94,6 @@ mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select
+----------------+----------+
| | 5 |
+----------------+----------+

mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; SELECT max(lpad('y',0,c1)) FROM test.t2
max(lpad('y',0,c1))

0 comments on commit a3f4db4

Please sign in to comment.