[Tarantool-patches] [PATCH v2 3/3] sql: fix STRING to BOOLEAN explicit cast

imeevma at tarantool.org imeevma at tarantool.org
Wed Aug 4 11:32:50 MSK 2021


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 <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



More information about the Tarantool-patches mailing list