From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id A159544532A for ; Thu, 16 Jul 2020 17:46:21 +0300 (MSK) From: imeevma@tarantool.org Date: Thu, 16 Jul 2020 17:46:20 +0300 Message-Id: <8128b363970ac8e314eb6085524c47e17e3995cf.1594909974.git.imeevma@gmail.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v6 09/22] sql: check args of position() List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: korablev@tarantool.org, tsafin@tarantool.org, tarantool-patches@dev.tarantool.org After this patch, the argument types of the position() function will be checked properly. Part of #4159 --- src/box/sql/func.c | 24 ++++------------ test/sql-tap/func5.test.lua | 51 +++++++++++++++++++++++++++++++++- test/sql-tap/position.test.lua | 6 ++-- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 2910f308f..a1b284762 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -532,22 +532,8 @@ position_func(struct sql_context *context, int argc, struct Mem **argv) if (haystack_type == MP_NIL || needle_type == MP_NIL) return; - /* - * Position function can be called only with string - * or blob params. - */ - struct Mem *inconsistent_type_arg = NULL; - if (needle_type != MP_STR && needle_type != MP_BIN) - inconsistent_type_arg = needle; - if (haystack_type != MP_STR && haystack_type != MP_BIN) - inconsistent_type_arg = haystack; - if (inconsistent_type_arg != NULL) { - diag_set(ClientError, ER_INCONSISTENT_TYPES, - "text or varbinary", - mem_type_to_str(inconsistent_type_arg)); - context->is_aborted = true; - return; - } + assert(needle_type == MP_STR || needle_type == MP_BIN); + assert(haystack_type == MP_STR || haystack_type == MP_BIN); /* * Both params of Position function must be of the same * type. @@ -2683,9 +2669,9 @@ static struct { }, { .name = "POSITION", .param_count = 2, - .first_arg = FIELD_TYPE_ANY, - .args = FIELD_TYPE_ANY, - .is_blob_like_str = false, + .first_arg = FIELD_TYPE_STRING, + .args = FIELD_TYPE_STRING, + .is_blob_like_str = true, .returns = FIELD_TYPE_INTEGER, .aggregate = FUNC_AGGREGATE_NONE, .is_deterministic = true, diff --git a/test/sql-tap/func5.test.lua b/test/sql-tap/func5.test.lua index b704ed7d5..170dee9f9 100755 --- a/test/sql-tap/func5.test.lua +++ b/test/sql-tap/func5.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool test = require("sqltester") -test:plan(99) +test:plan(106) --!./tcltestrunner.lua -- 2010 August 27 @@ -836,4 +836,53 @@ test:do_catchsql_test( 1, "Type mismatch: can not convert varbinary to string" }) +test:do_execsql_test( + "func-5-6.12.1", [[ + SELECT position(NULL, NULL); + ]],{ + "" + }) + +test:do_catchsql_test( + "func-5-6.12.2", [[ + SELECT position(23, 123); + ]], { + 1, "Type mismatch: can not convert 23 to string" + }) + +test:do_catchsql_test( + "func-5-6.12.3", [[ + SELECT position(-12, -123); + ]], { + 1, "Type mismatch: can not convert -12 to string" + }) + +test:do_catchsql_test( + "func-5-6.12.4", [[ + SELECT position(-5.5, -5.5); + ]], { + 1, "Type mismatch: can not convert -5.5 to string" + }) + +test:do_execsql_test( + "func-5-6.12.5", [[ + SELECT position('23', '-123'); + ]], { + 3 + }) + +test:do_catchsql_test( + "func-5-6.12.6", [[ + SELECT position(false, true); + ]], { + 1, "Type mismatch: can not convert FALSE to string" + }) + +test:do_execsql_test( + "func-5-6.12.7", [[ + SELECT position(X'34', X'3334'); + ]], { + 2 + }) + test:finish_test() diff --git a/test/sql-tap/position.test.lua b/test/sql-tap/position.test.lua index e0455abc9..0d4f6f371 100755 --- a/test/sql-tap/position.test.lua +++ b/test/sql-tap/position.test.lua @@ -228,7 +228,7 @@ test:do_test( return test:catchsql "SELECT position(34, 12345);" end, { -- - 1, "Inconsistent types: expected text or varbinary got unsigned" + 1, "Type mismatch: can not convert 34 to string" -- }) @@ -238,7 +238,7 @@ test:do_test( return test:catchsql "SELECT position(34, 123456.78);" end, { -- - 1, "Inconsistent types: expected text or varbinary got real" + 1, "Type mismatch: can not convert 34 to string" -- }) @@ -248,7 +248,7 @@ test:do_test( return test:catchsql "SELECT position(x'3334', 123456.78);" end, { -- - 1, "Inconsistent types: expected text or varbinary got real" + 1, "Type mismatch: can not convert 123456.78 to string" -- }) -- 2.25.1