[Tarantool-patches] [PATCH v1 1/1] sql: fix a segfault in hex() on receiving zeroblob
imeevma at tarantool.org
imeevma at tarantool.org
Mon Aug 30 09:20:36 MSK 2021
This patch fixes a segmentation fault when zeroblob is received by the
SQL built-in HEX() function.
Closes #6113
---
https://github.com/tarantool/tarantool/issues/6113
https://github.com/tarantool/tarantool/tree/imeevma/gh-6113-fix-hex-segfault-2.8
.../unreleased/gh-6113-fix-segfault-in-hex-func.md | 5 +++++
src/box/sql/func.c | 10 ++++++++--
test/sql-tap/engine.cfg | 1 +
.../gh-6113-assert-in-hex-on-zeroblob.test.lua | 13 +++++++++++++
4 files changed, 27 insertions(+), 2 deletions(-)
create mode 100644 changelogs/unreleased/gh-6113-fix-segfault-in-hex-func.md
create mode 100755 test/sql-tap/gh-6113-assert-in-hex-on-zeroblob.test.lua
diff --git a/changelogs/unreleased/gh-6113-fix-segfault-in-hex-func.md b/changelogs/unreleased/gh-6113-fix-segfault-in-hex-func.md
new file mode 100644
index 000000000..c59be4d96
--- /dev/null
+++ b/changelogs/unreleased/gh-6113-fix-segfault-in-hex-func.md
@@ -0,0 +1,5 @@
+## bugfix/sql
+
+* The HEX() SQL built-in function now does not throw an assert on receiving
+ varbinary values that consist of zero-bytes (gh-6113).
+
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index b137c6125..3ef31705e 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1221,15 +1221,21 @@ hexFunc(sql_context * context, int argc, sql_value ** argv)
UNUSED_PARAMETER(argc);
pBlob = mem_as_bin(argv[0]);
n = mem_len_unsafe(argv[0]);
+ assert((argv[0]->flags & MEM_Zero) == 0 ||
+ argv[0]->type == MEM_TYPE_BIN);
+ int zero_len = (argv[0]->flags & MEM_Zero) == 0 ? 0 : argv[0]->u.nZero;
assert(pBlob == mem_as_bin(argv[0])); /* No encoding change */
z = zHex = contextMalloc(context, ((i64) n) * 2 + 1);
if (zHex) {
- for (i = 0; i < n; i++, pBlob++) {
+ for (i = 0; i < n - zero_len; i++, pBlob++) {
unsigned char c = *pBlob;
*(z++) = hexdigits[(c >> 4) & 0xf];
*(z++) = hexdigits[c & 0xf];
}
- *z = 0;
+ assert(i == n || (argv[0]->flags & MEM_Zero) != 0);
+ assert(n == zero_len + i);
+ memset(z, '0', 2 * zero_len);
+ z[2 * zero_len] = '\0';
sql_result_text(context, zHex, n * 2, sql_free);
}
}
diff --git a/test/sql-tap/engine.cfg b/test/sql-tap/engine.cfg
index 693a477b7..ddee8c328 100644
--- a/test/sql-tap/engine.cfg
+++ b/test/sql-tap/engine.cfg
@@ -21,6 +21,7 @@
"memtx": {"engine": "memtx"}
},
"gh-4077-iproto-execute-no-bind.test.lua": {},
+ "gh-6113-assert-in-hex-on-zeroblob.test.lua": {},
"*": {
"memtx": {"engine": "memtx"},
"vinyl": {"engine": "vinyl"}
diff --git a/test/sql-tap/gh-6113-assert-in-hex-on-zeroblob.test.lua b/test/sql-tap/gh-6113-assert-in-hex-on-zeroblob.test.lua
new file mode 100755
index 000000000..91a29a5b4
--- /dev/null
+++ b/test/sql-tap/gh-6113-assert-in-hex-on-zeroblob.test.lua
@@ -0,0 +1,13 @@
+#!/usr/bin/env tarantool
+local test = require("sqltester")
+test:plan(1)
+
+test:do_execsql_test(
+ "gh-6113",
+ [[
+ SELECT hex(zeroblob(0)), hex(zeroblob(10));
+ ]], {
+ '', '00000000000000000000'
+ })
+
+test:finish_test()
--
2.25.1
More information about the Tarantool-patches
mailing list