pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.26k stars 5.84k forks source link

error report: 'runtime error: index out of range [1] with length 1' when using aes_decrypt sometimes #43076

Closed lcwangchao closed 1 year ago

lcwangchao commented 1 year ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

create table tx(s1 blob, s2 blob);
insert into tx values (aes_encrypt('a', 'b'), aes_encrypt('c', 'd'));
select * from tx where aes_decrypt(s1, 'b')='' and aes_decrypt(s2, 'd')='';

2. What did you expect to see? (Required)

no error

3. What did you see instead (Required)

TiDB root@127.0.0.1:test> select * from tx where aes_decrypt(s1, 'b')='' and aes_decrypt(s2, 'd')='';
(1105, 'runtime error: index out of range [1] with length 1')

4. What is your TiDB version? (Required)

TiDB root@127.0.0.1:test> select tidb_version();
+-----------------------------------------------------------+
| tidb_version()                                            |
+-----------------------------------------------------------+
| Release Version: v7.1.0-alpha                             |
| Edition: Community                                        |
| Git Commit Hash: c282c9c94276ef698f93d6aa89df7dde16f6f479 |
| Git Branch: heads/refs/tags/v7.1.0-alpha                  |
| UTC Build Time: 2023-04-10 14:26:56                       |
| GoVersion: go1.20.3                                       |
| Race Enabled: false                                       |
| TiKV Min Version: 6.2.0-alpha                             |
| Check Table Before Drop: false                            |
| Store: tikv                                               |
+-----------------------------------------------------------+
1 row in set
lcwangchao commented 1 year ago

The panic stack is here

[2023/04/14 21:17:19.125 +08:00] [INFO] [conn.go:1154] ["command dispatched failed"] [conn=2199023255957] [connInfo="id:2199023255957, addr:127.0.0.1:58798 status:10, collation:utf8_general_ci, user:root"] [command=Query] [status="inTxn:0, autocommit:1"] [sql="select * from tx where aes_decrypt(s1, 'b')='' and aes_decrypt(s2, 'd')=''"] [txn_mode=PESSIMISTIC] [timestamp=440789430986604544] [err="runtime error: index out of range [1] with length 1\ngithub.com/pingcap/tidb/executor.(*recordSet).Next.func1\n\t/Users/wangchao/Code/pingcap/tidb/executor/adapter.go:146\nruntime.gopanic\n\t/Users/wangchao/.gvm/gos/go1.20.2/src/runtime/panic.go:890\nruntime.goPanicIndex\n\t/Users/wangchao/.gvm/gos/go1.20.2/src/runtime/panic.go:113\ngithub.com/pingcap/tidb/util/chunk.(*Column).GetBytes\n\t/Users/wangchao/Code/pingcap/tidb/util/chunk/column.go:591\ngithub.com/pingcap/tidb/expression.(*builtinAesDecryptSig).vecEvalString\n\t/Users/wangchao/Code/pingcap/tidb/expression/builtin_encryption_vec.go:77\ngithub.com/pingcap/tidb/expression.(*ScalarFunction).VecEvalString\n\t/Users/wangchao/Code/pingcap/tidb/expression/scalar_function.go:58\ngithub.com/pingcap/tidb/expression.(*builtinEQStringSig).vecEvalInt\n\t/Users/wangchao/Code/pingcap/tidb/expression/builtin_compare_vec_generated.go:1003\ngithub.com/pingcap/tidb/expression.(*ScalarFunction).VecEvalInt\n\t/Users/wangchao/Code/pingcap/tidb/expression/scalar_function.go:48\ngithub.com/pingcap/tidb/expression.EvalExpr\n\t/Users/wangchao/Code/pingcap/tidb/expression/expression.go:574\ngithub.com/pingcap/tidb/expression.VecEvalBool\n\t/Users/wangchao/Code/pingcap/tidb/expression/expression.go:369\ngithub.com/pingcap/tidb/expression.vectorizedFilter\n\t/Users/wangchao/Code/pingcap/tidb/expression/chunk_executor.go:489\ngithub.com/pingcap/tidb/expression.VectorizedFilterConsiderNull\n\t/Users/wangchao/Code/pingcap/tidb/expression/chunk_executor.go:401\ngithub.com/pingcap/tidb/expression.VectorizedFilter\n\t/Users/wangchao/Code/pingcap/tidb/expression/chunk_executor.go:379\ngithub.com/pingcap/tidb/executor.(*SelectionExec).Next\n\t/Users/wangchao/Code/pingcap/tidb/executor/executor.go:1675\ngithub.com/pingcap/tidb/executor.Next\n\t/Users/wangchao/Code/pingcap/tidb/executor/executor.go:326\ngithub.com/pingcap/tidb/executor.(*ExecStmt).next\n\t/Users/wangchao/Code/pingcap/tidb/executor/adapter.go:1194\ngithub.com/pingcap/tidb/executor.(*recordSet).Next\n\t/Users/wangchao/Code/pingcap/tidb/executor/adapter.go:150\ngithub.com/pingcap/tidb/server.(*tidbResultSet).Next\n\t/Users/wangchao/Code/pingcap/tidb/server/driver_tidb.go:428\ngithub.com/pingcap/tidb/server.(*clientConn).writeChunks\n\t/Users/wangchao/Code/pingcap/tidb/server/conn.go:2281\ngithub.com/pingcap/tidb/server.(*clientConn).writeResultSet\n\t/Users/wangchao/Code/pingcap/tidb/server/conn.go:2224\ngithub.com/pingcap/tidb/server.(*clientConn).handleStmt\n\t/Users/wangchao/Code/pingcap/tidb/server/conn.go:2097\ngithub.com/pingcap/tidb/server.(*clientConn).handleQuery\n\t/Users/wangchao/Code/pingcap/tidb/server/conn.go:1864\ngithub.com/pingcap/tidb/server.(*clientConn).dispatch\n\t/Users/wangchao/Code/pingcap/tidb/server/conn.go:1342\ngithub.com/pingcap/tidb/server.(*clientConn).Run\n\t/Users/wangchao/Code/pingcap/tidb/server/conn.go:1123\ngithub.com/pingcap/tidb/server.(*Server).onConn\n\t/Users/wangchao/Code/pingcap/tidb/server/server.go:677\nruntime.goexit\n\t/Users/wangchao/.gvm/gos/go1.20.2/src/runtime/asm_arm64.s:1172"]
lcwangchao commented 1 year ago

It panics here:

https://github.com/pingcap/tidb/blob/579f47e4122fd6561a1b2b07520178b3137101b7/expression/builtin_encryption_vec.go#L73

The vecEvalString does handle the case input.NumRows() == 0

lcwangchao commented 1 year ago

dup with #43063