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 1DA5D6EC58; Mon, 2 Aug 2021 20:26:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1DA5D6EC58 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627925219; 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=HN3DvvZZ+C9VbDkSTFsrln48BPVFeA5TORE0idiJj4cK/A9X4fJMcLYH/dmIrVN0Y qUTeSnZq+bAcWPcIkD/5aoZf/T/4UP+mf2vJHns+lj4BakPYbjSiwcqca/G88Alwxa KXFAhdXwaIYt1gwxxgnZ4MvwtX1TG8DGeaiWdgQQ= Received: from smtp16.mail.ru (smtp16.mail.ru [94.100.176.153]) (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 CC4676E46E for ; Mon, 2 Aug 2021 20:25:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CC4676E46E Received: by smtp16.mail.ru with esmtpa (envelope-from ) id 1mAbgx-0002ht-Nn; Mon, 02 Aug 2021 20:25:32 +0300 To: imun@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Mon, 2 Aug 2021 20:25:31 +0300 Message-Id: <19c37d102802b46e71cece7a76e9d929455c6906.1627925076.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: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD941C43E597735A9C351B198F4576AC7B21928AAE70459C21B182A05F53808504065A60E50F39CE8644DDBAEC31A6F1655A9077D4DE0AEE02DEC28E03BEAE5B70F X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78C6616F30072131EEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637F97367C191A19EB28638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D89CB8521B6E8E975DD918868781656786117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18618001F51B5FD3F9D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE38D4DC57D478E6888F49F126DDB898E8D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3B5C78E0E843E24DA2D242C3BD2E3F4C6C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637AF8E4F18C523FAA9EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C85811CA530B504919958298B9EE262D81828EAEA6CCC51799C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF03D88DA7F5FEDC25699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34F804B400765FFFF9C0299B500222F220B7FABCCA740092E3FB248C6F57D2637FF00FDAE42048F2571D7E09C32AA3244C1321F3BF67094F7EE04978A2EE9673284DBEAD0ED6C55A80729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj9N286KAyvN5idSiyvTQxmw== X-Mailru-Sender: 83DC58D0E026EF5214980281B38C4557FFE7641E06D18C394DDBAEC31A6F16555F9045B74F665CC85105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 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