[Tarantool-patches] [PATCH v1 5/7] sql: disallow bitwise for NUMBER and SCALAR
imeevma at tarantool.org
imeevma at tarantool.org
Wed Aug 11 19:01:48 MSK 2021
Part of #6221
---
src/box/sql/mem.c | 58 ++++++++++++---------------------
test/sql-tap/metatypes.test.lua | 19 ++++++++++-
2 files changed, 39 insertions(+), 38 deletions(-)
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index e2c85e2ea..e34f24c96 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1759,6 +1759,22 @@ mem_rem(const struct Mem *left, const struct Mem *right, struct Mem *result)
return 0;
}
+static inline int
+check_types_unsigned_bitwise(const struct Mem *a, const struct Mem *b)
+{
+ if (a->type != MEM_TYPE_UINT || mem_is_metatype(a)) {
+ diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(a),
+ "unsigned");
+ return -1;
+ }
+ if (b->type != MEM_TYPE_UINT || mem_is_metatype(b)) {
+ diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(b),
+ "unsigned");
+ return -1;
+ }
+ return 0;
+}
+
int
mem_bit_and(const struct Mem *left, const struct Mem *right, struct Mem *result)
{
@@ -1766,16 +1782,8 @@ mem_bit_and(const struct Mem *left, const struct Mem *right, struct Mem *result)
mem_set_null(result);
return 0;
}
- if (right->type != MEM_TYPE_UINT) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right),
- "unsigned");
- return -1;
- }
- if (left->type != MEM_TYPE_UINT) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left),
- "unsigned");
+ if (check_types_unsigned_bitwise(right, left) != 0)
return -1;
- }
mem_set_uint(result, left->u.u & right->u.u);
return 0;
}
@@ -1787,16 +1795,8 @@ mem_bit_or(const struct Mem *left, const struct Mem *right, struct Mem *result)
mem_set_null(result);
return 0;
}
- if (right->type != MEM_TYPE_UINT) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right),
- "unsigned");
- return -1;
- }
- if (left->type != MEM_TYPE_UINT) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left),
- "unsigned");
+ if (check_types_unsigned_bitwise(right, left) != 0)
return -1;
- }
mem_set_uint(result, left->u.u | right->u.u);
return 0;
}
@@ -1809,16 +1809,8 @@ mem_shift_left(const struct Mem *left, const struct Mem *right,
mem_set_null(result);
return 0;
}
- if (right->type != MEM_TYPE_UINT) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right),
- "unsigned");
- return -1;
- }
- if (left->type != MEM_TYPE_UINT) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left),
- "unsigned");
+ if (check_types_unsigned_bitwise(right, left) != 0)
return -1;
- }
mem_set_uint(result, right->u.u >= 64 ? 0 : left->u.u << right->u.u);
return 0;
}
@@ -1831,16 +1823,8 @@ mem_shift_right(const struct Mem *left, const struct Mem *right,
mem_set_null(result);
return 0;
}
- if (right->type != MEM_TYPE_UINT) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right),
- "unsigned");
- return -1;
- }
- if (left->type != MEM_TYPE_UINT) {
- diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left),
- "unsigned");
+ if (check_types_unsigned_bitwise(right, left) != 0)
return -1;
- }
mem_set_uint(result, right->u.u >= 64 ? 0 : left->u.u >> right->u.u);
return 0;
}
@@ -1852,7 +1836,7 @@ mem_bit_not(const struct Mem *mem, struct Mem *result)
mem_set_null(result);
return 0;
}
- if (mem->type != MEM_TYPE_UINT) {
+ if (mem->type != MEM_TYPE_UINT || mem_is_metatype(mem)) {
diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(mem),
"unsigned");
return -1;
diff --git a/test/sql-tap/metatypes.test.lua b/test/sql-tap/metatypes.test.lua
index c0b2a4d03..50d700cc4 100755
--- a/test/sql-tap/metatypes.test.lua
+++ b/test/sql-tap/metatypes.test.lua
@@ -1,6 +1,6 @@
#!/usr/bin/env tarantool
local test = require("sqltester")
-test:plan(10)
+test:plan(12)
-- Check that SCALAR and NUMBER meta-types works as intended.
box.execute([[CREATE TABLE t (i INT PRIMARY KEY, s SCALAR, n NUMBER);]])
@@ -107,6 +107,23 @@ test:do_catchsql_test(
1, "Type mismatch: can not convert scalar(1) to integer, unsigned or double"
})
+-- Check that bitwise operations are prohibited for NUMBER and SCALAR values.
+test:do_catchsql_test(
+ "metatypes-4.1",
+ [[
+ SELECT 1 & CAST(1 AS NUMBER);
+ ]], {
+ 1, "Type mismatch: can not convert number(1) to unsigned"
+ })
+
+test:do_catchsql_test(
+ "metatypes-4.2",
+ [[
+ SELECT CAST(1 AS SCALAR) >> 1;
+ ]], {
+ 1, "Type mismatch: can not convert scalar(1) to unsigned"
+ })
+
box.execute([[DROP TABLE t;]])
test:finish_test()
--
2.25.1
More information about the Tarantool-patches
mailing list