From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: kyukhin@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v2 3/3] sql: fix STRING to BOOLEAN explicit cast
Date: Wed, 4 Aug 2021 11:32:50 +0300 [thread overview]
Message-ID: <72ad8bc02b770e483747350f72375682d630faad.1628065890.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1628065890.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 | 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
next prev parent reply other threads:[~2021-08-04 8:34 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
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 1/3] sql: disallow explicit cast of BOOLEAN to number Mergen Imeev via Tarantool-patches
2021-08-04 8:32 ` [Tarantool-patches] [PATCH v2 2/3] sql: disallow explicit cast of VARBINARY " Mergen Imeev via Tarantool-patches
2021-08-04 8:32 ` Mergen Imeev via Tarantool-patches [this message]
2021-08-04 14:16 ` [Tarantool-patches] [PATCH v2 0/3] Fix explicit casts Kirill Yukhin via Tarantool-patches
2021-08-04 15:21 ` Vitaliia Ioffe via Tarantool-patches
2021-08-05 9:48 ` Kirill Yukhin via Tarantool-patches
-- strict thread matches above, loose matches on Subject: below --
2021-08-02 17:25 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-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-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 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=72ad8bc02b770e483747350f72375682d630faad.1628065890.git.imeevma@gmail.com \
--to=tarantool-patches@dev.tarantool.org \
--cc=imeevma@tarantool.org \
--cc=kyukhin@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