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 CEA3C6EC5E; Mon, 30 Aug 2021 09:30:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CEA3C6EC5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1630305039; bh=Eb+w2YW6X+Ylb6ihfLkIAx+BZZlFpVSNqzvlEFiLGXY=; h=To:Cc:Date:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=hoFofpt1PK0+vpCb7h0ayCZBIUGEREKYFOa9mzL2EdzJcUQJInPwGQJqm9wKb/6nL WQ6L5eqoDXHL8BqySJHn7cuX3kdLhnrsrqkJsi7OH+5asE+5iFJ0nl61moyKoCNMvA vDIACUijFgQLRLG4JzJPgLPMoKdw0HtIX57HFc04= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (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 60F096EC5E for ; Mon, 30 Aug 2021 09:30:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 60F096EC5E Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mKaoX-0005Gq-MJ; Mon, 30 Aug 2021 09:30:38 +0300 To: tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Mon, 30 Aug 2021 09:30:37 +0300 Message-Id: <9ec7b38b0979cb2e9ac6cb6b8f2e405c313a67f9.1630305008.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9D5AC6413C25DCF08CC98B8FCC5CD86F3182A05F538085040C3E8E18D6094F9ADD665D76F292B1FEB1E3C859FDE4C5439F4B315FE30512C6A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7F12ABE79F2AB44EAEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063736D1EC9E169E1F8C8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D869BB738E4FB3F86D720EA05ADF893E08117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BF1175FABE1C0F9B6A471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7CB1724D34C644744043847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C4DDEBE0CC47FE2695FCF1664BB9C0A573921D07FDC7DCACC9C2B6934AE262D3EE7EAB7254005DCEDFB29427B7C22B3881E0A4E2319210D9B64D260DF9561598F01A9E91200F654B06CE7B4E551862B828E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D4E96E2A5B1100E0747645CB683AD4195992032A1B7985C9DC668D249F9C9F5625415185CDD9D94C1D7E09C32AA3244C8B5A2479045F2B55C667CC43EA1E6FD7B4DF56057A86259F729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4DnN7V8kJ6vFci/2hddLdQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D80B16C6AEC94EBC2A9F3BC6D54289D0883D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 1/1] sql: fix a segfault in hex() on receiving zeroblob 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" 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.10 .../gh-6113-fix-segfault-in-hex-func.md | 5 ++ src/box/sql/func.c | 75 ++++++++++--------- test/sql-tap/engine.cfg | 1 + ...gh-6113-assert-in-hex-on-zeroblob.test.lua | 13 ++++ 4 files changed, 58 insertions(+), 36 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 c063552d6..fa2a2c245 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -53,6 +53,44 @@ static struct mh_strnptr_t *built_in_functions = NULL; static struct func_sql_builtin **functions; +/** Array for converting from half-bytes into ASCII hex digits. */ +static const char hexdigits[] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' +}; + +/** Implementation of the HEX() SQL built-in function. */ +static void +func_hex(struct sql_context *ctx, int argc, struct Mem **argv) +{ + assert(argc == 1); + (void)argc; + if (argv[0]->type == MEM_TYPE_NULL) + return mem_set_null(ctx->pOut); + + assert(argv[0]->type == MEM_TYPE_BIN && argv[0]->n >= 0); + assert((argv[0]->flags & MEM_Zero) == 0 || argv[0]->u.nZero >= 0); + uint32_t size = 2 * argv[0]->n; + if ((argv[0]->flags & MEM_Zero) != 0) + size += 2 * argv[0]->u.nZero; + if (size == 0) + return mem_set_str0_static(ctx->pOut, ""); + + char *str = sqlDbMallocRawNN(sql_get(), size); + if (str == NULL) { + ctx->is_aborted = true; + return; + } + for (int i = 0; i < argv[0]->n; ++i) { + char c = argv[0]->z[i]; + str[2 * i] = hexdigits[(c >> 4) & 0xf]; + str[2 * i + 1] = hexdigits[c & 0xf]; + } + if ((argv[0]->flags & MEM_Zero) != 0) + memset(&str[2 * argv[0]->n], '0', 2 * argv[0]->u.nZero); + mem_set_str_allocated(ctx->pOut, str, size); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -1072,14 +1110,6 @@ sql_func_version(struct sql_context *context, sql_result_text(context, tarantool_version(), -1, SQL_STATIC); } -/* Array for converting from half-bytes (nybbles) into ASCII hex - * digits. - */ -static const char hexdigits[] = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' -}; - /* * Implementation of the QUOTE() function. This function takes a single * argument. If the argument is numeric, the return value is the same as @@ -1233,33 +1263,6 @@ charFunc(sql_context * context, int argc, sql_value ** argv) sql_result_text64(context, (char *)z, zOut - z, sql_free); } -/* - * The hex() function. Interpret the argument as a blob. Return - * a hexadecimal rendering as text. - */ -static void -hexFunc(sql_context * context, int argc, sql_value ** argv) -{ - int i, n; - const unsigned char *pBlob; - char *zHex, *z; - assert(argc == 1); - UNUSED_PARAMETER(argc); - pBlob = mem_as_bin(argv[0]); - n = mem_len_unsafe(argv[0]); - 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++) { - unsigned char c = *pBlob; - *(z++) = hexdigits[(c >> 4) & 0xf]; - *(z++) = hexdigits[c & 0xf]; - } - *z = 0; - sql_result_text(context, zHex, n * 2, sql_free); - } -} - /* * The zeroblob(N) function returns a zero-filled blob of size N bytes. */ @@ -2034,7 +2037,7 @@ static struct sql_func_definition definitions[] = { {"GROUP_CONCAT", 2, {FIELD_TYPE_VARBINARY, FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, groupConcatStep, groupConcatFinalize}, - {"HEX", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_STRING, hexFunc, NULL}, + {"HEX", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_STRING, func_hex, NULL}, {"IFNULL", 2, {FIELD_TYPE_ANY, FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, sql_builtin_stub, NULL}, diff --git a/test/sql-tap/engine.cfg b/test/sql-tap/engine.cfg index 587adbed9..5ff0219fc 100644 --- a/test/sql-tap/engine.cfg +++ b/test/sql-tap/engine.cfg @@ -35,6 +35,7 @@ "built-in-functions.test.lua": { "memtx": {"engine": "memtx"} }, + "gh-6113-assert-in-hex-on-zeroblob.test.lua": {}, "gh-4077-iproto-execute-no-bind.test.lua": {}, "gh-6375-assert-on-unsupported-ext.test.lua": {}, "*": { 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