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 D97736EC40; Fri, 13 Aug 2021 06:15:25 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D97736EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628824525; 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=gK0ZRbcu3Yzr4Z52dazClxyWiHJkkCuJ5BVryXUMx8qnNxCZO6Jt6WoSgCUvQzO2/ yfMzbc7s5Vlgv/klW3AY/MRSLCm7zfDgILw2pmZr61LGlfzhThzCSenBmGRDZvKMej ufz8ARwamxX6dVrWeAE8acCDBPBXXCPbDQQbC0u8= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (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 61E276EC45 for ; Fri, 13 Aug 2021 06:13:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 61E276EC45 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mENd1-00085a-Jz; Fri, 13 Aug 2021 06:13:03 +0300 To: vdavydov@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 13 Aug 2021 06:13:03 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD91BCCB18F2C129F87F36E61E9E4584E9D182A05F538085040F9687C6FB6521A24C0E74C450608B4C8339220F6E0937EA96FBDF4CBB7AC81FB X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A609AE8E79ADB41CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063706922F90966A37BA8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8D39817C52B4C97255191E748BB79B38F117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF80095D1E57F4578A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18E5D25F19253116ADD2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5059A91936EF7B6732D93A5EB6E46426FD2 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C69415AB31670C86CD58904310A662D39DE4CA9D59F3BBC5A9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFE37876E7723AB534DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D342B1F2AD168155B06B0FA34E5F50EA716C21379C6237762318C391FCFE91890A5DC23C5F7D6BFBD981D7E09C32AA3244CB6AC0A5CB4DDFED7317E46E077E37A308580396430872480729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkxKwkn2VOTbDQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D5C81ABEE79064A7D742E000392A4376C83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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