From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 2435F6EC40; Wed, 11 Aug 2021 19:04:13 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2435F6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628697853; bh=dUpzo6FWQ2crX7ynhZyZVFUkvmoNIclGNF67iF7WgEg=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Ca4RJlvowRf6yBB3s4HOoYjZZ7JTh46rQMRqM2w5tqJhvRBjMquuRu7YzUweNtuPF iYY45THwuggVp64mAuMmKOwI29XXxLglELUF2G9cMvfmtEg0Fmv9rqbll4eF2+Ggwa 2bGxucjSwhxUZoYpfxYoPuav/oLprC+yVWvTMnsM= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [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 9A62C6EC42 for ; Wed, 11 Aug 2021 19:01:49 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9A62C6EC42 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mDqfs-0005jJ-Uk; Wed, 11 Aug 2021 19:01:49 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Wed, 11 Aug 2021 19:01:48 +0300 Message-Id: <6be1d973021d1bf65537394604414c3800299349.1628697448.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD906AB4890CDABF0C5CB76CEE71D3E4007182A05F538085040CC902C82B643F77197976D8BE378F4143513275EE473DB704A62FF2858136CC9 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A609AE8E79ADB41CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063706922F90966A37BA8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D89E17CE936E43E30117FB191794C4321B117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF80095D1E57F4578A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18BDFBBEFFF4125B51D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C6B668463F4F89F86F3B48D4D273A1E10008E58983BC912999C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFA183FDCE24978B01699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D342B1F2AD168155B06B0FA34E5F50EA716C21379C6237762318C391FCFE91890A5DC23C5F7D6BFBD981D7E09C32AA3244C8A75E9E95E5E5F744AF77E53C133DB3D259227199D06760A729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj6qlzQV0oSZMi1urangInYA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D394305A2B69A45715F24889914306A1683D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 5/7] sql: disallow bitwise for NUMBER and SCALAR X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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