[Tarantool-patches] [PATCH v6 11/22] sql: check args of replace()

imeevma at tarantool.org imeevma at tarantool.org
Thu Jul 16 17:46:24 MSK 2020


After this patch, the argument types of the replace() function
will be checked properly.

Part of #4159
---
 src/box/sql/func.c          | 21 +++++++++------
 test/sql-tap/func5.test.lua | 51 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 63 insertions(+), 9 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index ae9b6d138..a52b9a103 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1509,20 +1509,25 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv)
 
 	assert(argc == 3);
 	UNUSED_PARAMETER(argc);
+	enum mp_type str_type = sql_value_type(argv[0]);
+	enum mp_type pattern_type = sql_value_type(argv[1]);
+	enum mp_type rep_type = sql_value_type(argv[2]);
+	if (str_type == MP_NIL || pattern_type == MP_NIL || rep_type == MP_NIL)
+		return sql_result_null(context);
+	assert(str_type == MP_STR || str_type == MP_BIN);
+	assert(pattern_type == MP_STR || pattern_type == MP_BIN);
+	assert(rep_type == MP_STR || rep_type == MP_BIN);
+
 	zStr = sql_value_text(argv[0]);
 	if (zStr == 0)
 		return;
 	nStr = sql_value_bytes(argv[0]);
 	assert(zStr == sql_value_text(argv[0]));	/* No encoding change */
 	zPattern = sql_value_text(argv[1]);
-	if (zPattern == 0) {
-		assert(sql_value_is_null(argv[1])
-		       || sql_context_db_handle(context)->mallocFailed);
+	if (zPattern == 0)
 		return;
-	}
 	nPattern = sql_value_bytes(argv[1]);
 	if (nPattern == 0) {
-		assert(! sql_value_is_null(argv[1]));
 		sql_result_value(context, argv[0]);
 		return;
 	}
@@ -2745,9 +2750,9 @@ static struct {
 	}, {
 	 .name = "REPLACE",
 	 .param_count = 3,
-	 .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_STRING,
 	 .aggregate = FUNC_AGGREGATE_NONE,
 	 .is_deterministic = true,
diff --git a/test/sql-tap/func5.test.lua b/test/sql-tap/func5.test.lua
index 30653e9e1..bf7a338a1 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(113)
+test:plan(120)
 
 --!./tcltestrunner.lua
 -- 2010 August 27
@@ -934,4 +934,53 @@ test:do_catchsql_test(
         1, "Type mismatch: can not convert varbinary to unsigned"
     })
 
+test:do_execsql_test(
+    "func-5-6.14.1", [[
+        SELECT replace(NULL, NULL, NULL);
+    ]],{
+        ""
+    })
+
+test:do_catchsql_test(
+    "func-5-6.6.2", [[
+        SELECT replace(123, 123, 123);
+    ]], {
+        1, "Type mismatch: can not convert 123 to string"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.14.3", [[
+        SELECT replace(-123, -123, -123);
+    ]], {
+        1, "Type mismatch: can not convert -123 to string"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.14.4", [[
+        SELECT replace(-5.5,-5.5, -5.5);
+    ]], {
+        1, "Type mismatch: can not convert -5.5 to string"
+    })
+
+test:do_execsql_test(
+    "func-5-6.14.5", [[
+        SELECT replace('-123', '-123', '-123');
+    ]], {
+        "-123"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.14.6", [[
+        SELECT replace(false, false, false);
+    ]], {
+        1, "Type mismatch: can not convert FALSE to string"
+    })
+
+test:do_execsql_test(
+    "func-5-6.14.7", [[
+        SELECT replace(X'3334', X'3334', X'3334');
+    ]], {
+        "34"
+    })
+
 test:finish_test()
-- 
2.25.1



More information about the Tarantool-patches mailing list