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 DA37C6EC55; Fri, 30 Jul 2021 10:13:23 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DA37C6EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627629203; bh=sIKKeD6p4OJyHeaRTUrLWdxE3zHqMnRyUw5qK8DyObo=; 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=HY+PQaTMNelbiM6qLrEE0cU3MwuaTDtu3X6jmcvBI7xgXyy1fNdnaFCI1TIAFhK+7 XKxLPXAhbP5fRgTc/VV9fDo25UJYheIklB4ed3lsNRX4pXz8n38WWBQgX5g2r6yLeC DuJP0p5eeUfbkh/fXeOqnzu9qkkGqbJkvdiNlelQ= 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 C5FFA6EC5E for ; Fri, 30 Jul 2021 10:11:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C5FFA6EC5E Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1m9MgW-0006P9-7V; Fri, 30 Jul 2021 10:11:56 +0300 To: tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 30 Jul 2021 10:11:55 +0300 Message-Id: <3749877330639008b0166a39393518660dff7d1e.1627628984.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: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD941C43E597735A9C30A5AB0699C09BB51E5FD76225F0C99C3182A05F538085040A713B36647A1EE29666B87F1A6C034E31BAB563980312B83A694DC3C83F97BDE X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78C6616F30072131EEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637F97367C191A19EB28638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8C6C408DBC2F2FFD55FCB35281E35552B117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18618001F51B5FD3F9D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C82A4B7DEF783988141FDEE5B717F166845BF1CCA62D8212C9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFA50270D34413AC6EDC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34F804B400765FFFF9C0299B500222F220B7FABCCA740092E3FB248C6F57D2637FF00FDAE42048F2571D7E09C32AA3244C2568B4D12D93E515D9C984F256D72A62F2F5F14F68F1805B729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWBddABnKmoI+Wn/GVc6cIg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DB39AB77FCCCEDCBC7085BFA239766D1083D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 3/3] sql: fix STRING to BOOLEAN explicit cast 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" Prior to this patch, if a non-NULL-terminated string was cast to BOOLEAN, the conversion always failed. Casting to BOOLEAN is now independent of NULL termination. Part of #4470 --- .../unreleased/gh-4470-explicit-cast.md | 8 +++++++ src/box/sql/mem.c | 21 ++++++++++--------- test/sql-tap/cast.test.lua | 11 +++++++++- 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 changelogs/unreleased/gh-4470-explicit-cast.md diff --git a/changelogs/unreleased/gh-4470-explicit-cast.md b/changelogs/unreleased/gh-4470-explicit-cast.md new file mode 100644 index 000000000..bc7f13212 --- /dev/null +++ b/changelogs/unreleased/gh-4470-explicit-cast.md @@ -0,0 +1,8 @@ +## feature/sql + +* Removed explicit cast from BOOLEAN to numeric types and vice versa (gh-4770). +* Removed explicit cast from VARBINARY to numeric types and vice versa (gh-4772, + gh-5852). +* Fixed a bug due to which a string that is not NULL terminated could not be + cast to BOOLEAN, even if the conversion should be successful according to the + rules. diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 985438663..351d80b76 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -28,6 +28,8 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include + #include "sqlInt.h" #include "mem.h" #include "vdbeInt.h" @@ -672,24 +674,23 @@ str_to_bool(struct Mem *mem) { assert(mem->type == MEM_TYPE_STR); char *str = mem->z; + uint32_t len = mem->n; bool b; const char *str_true = "TRUE"; const char *str_false = "FALSE"; uint32_t len_true = strlen(str_true); uint32_t len_false = strlen(str_false); - for (; str[0] == ' '; str++); - if (strncasecmp(str, str_true, len_true) == 0) { + for (; isspace(str[0]); str++, len--); + for (; isspace(str[len - 1]); len--); + if (len != len_true && len != len_false) + return -1; + + if (len == len_true && strncasecmp(str, str_true, len) == 0) b = true; - str += len_true; - } else if (strncasecmp(str, str_false, len_false) == 0) { + else if (len == len_false && strncasecmp(str, str_false, len) == 0) b = false; - str += len_false; - } else { - return -1; - } - for (; str[0] == ' '; str++); - if (str[0] != '\0') + else return -1; mem_set_bool(mem, b); return 0; diff --git a/test/sql-tap/cast.test.lua b/test/sql-tap/cast.test.lua index 997298693..799bcc1a8 100755 --- a/test/sql-tap/cast.test.lua +++ b/test/sql-tap/cast.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(94) +test:plan(95) --!./tcltestrunner.lua -- 2005 June 25 @@ -1008,4 +1008,13 @@ test:do_catchsql_test( 1, "Type mismatch: can not convert varbinary(x'31') to number" }) +-- Make sure that not NULL-terminated can be cast to BOOLEAN. +test:do_execsql_test( + "cast-8", + [[ + SELECT CAST(substr('true ', 0, 6) AS BOOLEAN); + ]], { + true + }) + test:finish_test() -- 2.25.1