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

imeevma at tarantool.org imeevma at tarantool.org
Thu Jul 16 17:47:08 MSK 2020


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

Closes #4159
---
 src/box/sql/func.c          |  9 ++++---
 test/sql-tap/func5.test.lua | 50 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 00ba7c9c2..5232f6745 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1480,9 +1480,12 @@ zeroblobFunc(sql_context * context, int argc, sql_value ** argv)
 	i64 n;
 	assert(argc == 1);
 	UNUSED_PARAMETER(argc);
+	enum mp_type mp_type = sql_value_type(argv[0]);
+	if (mp_type == MP_NIL)
+		return sql_result_null(context);
+	assert(mp_type == MP_UINT);
 	n = sql_value_int64(argv[0]);
-	if (n < 0)
-		n = 0;
+	assert(n >= 0);
 	if (sql_result_zeroblob64(context, n) != 0) {
 		diag_set(ClientError, ER_SQL_EXECUTE, "string or binary string"\
 			 "is too big");
@@ -2971,7 +2974,7 @@ static struct {
 	}, {
 	 .name = "ZEROBLOB",
 	 .param_count = 1,
-	 .first_arg = FIELD_TYPE_ANY,
+	 .first_arg = FIELD_TYPE_UNSIGNED,
 	 .args = FIELD_TYPE_ANY,
 	 .is_blob_like_str = false,
 	 .returns = FIELD_TYPE_VARBINARY,
diff --git a/test/sql-tap/func5.test.lua b/test/sql-tap/func5.test.lua
index 77105baf2..66ee6782a 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(148)
+test:plan(155)
 
 --!./tcltestrunner.lua
 -- 2010 August 27
@@ -1178,5 +1178,53 @@ test:do_catchsql_test(
     ]], {
         1, "Type mismatch: can not convert varbinary to string"
     })
+test:do_execsql_test(
+    "func-5-6.13.1", [[
+        SELECT zeroblob(NULL);
+    ]],{
+        ""
+    })
+
+test:do_execsql_test(
+    "func-5-6.13.2", [[
+        SELECT zeroblob(3);
+    ]], {
+        '\0\0\0'
+    })
+
+test:do_catchsql_test(
+    "func-5-6.13.3", [[
+        SELECT zeroblob(-123);
+    ]], {
+        1, "Type mismatch: can not convert -123 to unsigned"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.13.4", [[
+        SELECT zeroblob(-5.5);
+    ]], {
+        1, "Type mismatch: can not convert -5.5 to unsigned"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.13.5", [[
+        SELECT zeroblob('-123');
+    ]], {
+        1, "Type mismatch: can not convert -123 to unsigned"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.13.6", [[
+        SELECT zeroblob(false);
+    ]], {
+        1, "Type mismatch: can not convert FALSE to unsigned"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.13.7", [[
+        SELECT zeroblob(X'3334');
+    ]], {
+        1, "Type mismatch: can not convert varbinary to unsigned"
+    })
 
 test:finish_test()
-- 
2.25.1



More information about the Tarantool-patches mailing list