Skip to content

Commit

Permalink
sql: fix possible null dereference in sql_expr_coll()
Browse files Browse the repository at this point in the history
Some built-in functions can accept different number of arguments.
Check of argument count for such functions takes place right before
its execution. So it is possible that expression-list representing
arguments for built-in function is NULL. On the other hand, in
sql_expr_coll() (which returns collation of expression) it is assumed
that if function features SQL_FUNC_DERIVEDCOLL flag (it implies that
resulting collation depends on collation of arguments) then it has at
least one argument. The last assumption is wrong considering for example
SUBSTR() function: it may have 1 or 2 arguments, so check of argument
count doesn't occur during compilation. Hence, if it is required to
calculate collation for SUBSTR() function and there's no arguments,
Tarantool crashes due to null-dereference.
This patch fixes this bug with one additional check in sql_expr_coll().
  • Loading branch information
Korablev77 committed Dec 17, 2019
1 parent afa692e commit 4c13972
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/box/sql/expr.c
Expand Up @@ -332,7 +332,8 @@ sql_expr_coll(Parse *parse, Expr *p, bool *is_explicit_coll, uint32_t *coll_id,
sql_func_by_signature(p->u.zToken, arg_count);
if (func == NULL)
break;
if (sql_func_flag_is_set(func, SQL_FUNC_DERIVEDCOLL)) {
if (sql_func_flag_is_set(func, SQL_FUNC_DERIVEDCOLL) &&
arg_count > 0) {
/*
* Now we use quite straightforward
* approach assuming that resulting
Expand Down
9 changes: 9 additions & 0 deletions test/sql/collation.result
Expand Up @@ -292,6 +292,15 @@ box.execute("SELECT * FROM t WHERE a COLLATE \"binary\" = c COLLATE \"unicode\";
- null
- Illegal mix of collations
...
-- Make sure that using function featuring variable arguemnts
-- length and resulting collation which depends on arguments
-- is processed correctly.
--
box.execute("SELECT * FROM t WHERE a COLLATE \"binary\" = substr();")
---
- null
- 'Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 0'
...
-- Compound queries perform implicit comparisons between values.
-- Hence, rules for collations compatibilities are the same.
--
Expand Down
5 changes: 5 additions & 0 deletions test/sql/collation.test.lua
Expand Up @@ -80,6 +80,11 @@ box.execute("SELECT * FROM t WHERE b = c;")
box.execute("SELECT * FROM t WHERE b COLLATE \"binary\" = c;")
box.execute("SELECT * FROM t WHERE a = c;")
box.execute("SELECT * FROM t WHERE a COLLATE \"binary\" = c COLLATE \"unicode\";")
-- Make sure that using function featuring variable arguemnts
-- length and resulting collation which depends on arguments
-- is processed correctly.
--
box.execute("SELECT * FROM t WHERE a COLLATE \"binary\" = substr();")

-- Compound queries perform implicit comparisons between values.
-- Hence, rules for collations compatibilities are the same.
Expand Down

0 comments on commit 4c13972

Please sign in to comment.