Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v2 3/3] sql: fix STRING to BOOLEAN explicit cast
Date: Tue, 27 Jul 2021 14:31:35 +0300	[thread overview]
Message-ID: <0ea98ece93976b67a0f815f9f1946770f2e4e807.1627384910.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1627384910.git.imeevma@gmail.com>

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       |  3 +++
 src/box/sql/mem.c                             | 21 ++++++++++---------
 test/sql-tap/cast.test.lua                    | 11 +++++++++-
 3 files changed, 24 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..8d291fa9b
--- /dev/null
+++ b/changelogs/unreleased/gh-4470-explicit-cast.md
@@ -0,0 +1,3 @@
+## feature/sql
+
+* Explicit cast now works according to defined rules (gh-4470).
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index d8296faa5..9b160cbc5 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 <ctype.h>
+
 #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


  parent reply	other threads:[~2021-07-27 11:33 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-27 11:31 [Tarantool-patches] [PATCH v2 0/3] Fix explicit casts Mergen Imeev via Tarantool-patches
2021-07-27 11:31 ` [Tarantool-patches] [PATCH v2 1/3] sql: disallow explicit cast of BOOLEAN to number Mergen Imeev via Tarantool-patches
2021-07-27 11:31 ` [Tarantool-patches] [PATCH v2 2/3] sql: disallow explicit cast of VARBINARY " Mergen Imeev via Tarantool-patches
2021-07-29 20:58   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-27 11:31 ` Mergen Imeev via Tarantool-patches [this message]
2021-07-30  7:11 [Tarantool-patches] [PATCH v2 0/3] Fix explicit casts Mergen Imeev via Tarantool-patches
2021-07-30  7:11 ` [Tarantool-patches] [PATCH v2 3/3] sql: fix STRING to BOOLEAN explicit cast Mergen Imeev via Tarantool-patches
2021-08-02 17:25 [Tarantool-patches] [PATCH v2 0/3] Fix explicit casts Mergen Imeev via Tarantool-patches
2021-08-02 17:25 ` [Tarantool-patches] [PATCH v2 3/3] sql: fix STRING to BOOLEAN explicit cast Mergen Imeev via Tarantool-patches
2021-08-04  8:32 [Tarantool-patches] [PATCH v2 0/3] Fix explicit casts Mergen Imeev via Tarantool-patches
2021-08-04  8:32 ` [Tarantool-patches] [PATCH v2 3/3] sql: fix STRING to BOOLEAN explicit cast Mergen Imeev via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0ea98ece93976b67a0f815f9f1946770f2e4e807.1627384910.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v2 3/3] sql: fix STRING to BOOLEAN explicit cast' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox