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 1F7C46B967; Wed, 14 Apr 2021 04:55:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1F7C46B967 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618365351; bh=Eqw3G/rfRa0016QNOUwHKLnwAv/n6H2x4DRQZ9YaiTE=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=TPNJlXFC201a8+DNqNyBJBqIp/fCobP1RaeMktKV5TU8e+1lkvDtjMbJG3Yd0Bbd0 el+0dY8E0uRhiw6sYwfnh+l9Z9x3Wc4Dh5BpRVTy6TQjL/h3BepTaONinRU9p+RGU8 FUqjG7EhsGA6yAzeJJ0I2BmqwRssC5qfVEFlsXqE= Received: from smtp34.i.mail.ru (smtp34.i.mail.ru [94.100.177.94]) (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 4EB4C6B962 for ; Wed, 14 Apr 2021 04:55:50 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4EB4C6B962 Received: by smtp34.i.mail.ru with esmtpa (envelope-from ) id 1lWUkv-0002u1-7x; Wed, 14 Apr 2021 04:55:49 +0300 Date: Wed, 14 Apr 2021 04:55:48 +0300 To: Vladislav Shpilevoy Message-ID: <20210414015548.GA185092@tarantool.org> References: <326fac6c97ae260b32be9987e48e3381c600ba9c.1618000037.git.imeevma@gmail.com> <90cf544d-5188-ed34-face-15c231de09d1@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <90cf544d-5188-ed34-face-15c231de09d1@tarantool.org> X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480257C85EA0BB7A95D0F00AE41BB9A5343182A05F53808504084118086AEFC8AB178A65865DEFBDFAC2B428C14C5D8871BC88F058A6CFAA0E3 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BA0D57D3459E5640EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006373BFF7CCD9F2968778638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B263367BC081CDBF21246EE42CCD7C15979414A38D98B987DBD2E47CDBA5A96583C09775C1D3CA48CFCA5A41EBD8A3A0199FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3CA5A41EBD8A3A0199FA2833FD35BB23DF004C90652538430302FCEF25BFAB3454AD6D5ED66289B5278DA827A17800CE7FA3D786573799A4ED32BA5DBAC0009BE395957E7521B51C20BC6067A898B09E4090A508E0FED6299176DF2183F8FC7C081E37034442B5F27CD04E86FAF290E2D7E9C4E3C761E06A71DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3727597FF642BA4D735872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2368A440D3B0F6089093C9A16E5BC824A2A04A2ABAA09D25379311020FFC8D4ADB200D727D4285EA247E6BBB77C382719 X-C1DE0DAB: 0D63561A33F958A52FEFF7861834727200C1A3DF0FC195C2415AEA39C28E36B5D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D341E2D05735FCBECD1A3F4C40D60CC0BAC8461C4AE28067D8D989F0DA64EEB24B6BB28E64FF1B51F711D7E09C32AA3244C9FF0CF14DB526B64C65736BEE2E097C9F26BFA4C8A6946B8FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojnA7/qPBUIXHDsCl4bxq2yQ== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306EDABBE595462461A6578A65865DEFBDFACBA0848438379BFCE5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v5 51/52] sql: introduce mem_get_bytes_len() 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: Mergen Imeev Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review! My answer, diff and new patch below. On Wed, Apr 14, 2021 at 01:06:53AM +0200, Vladislav Shpilevoy wrote: > Good job on the patch! > > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > > index 2896a5c31..746bda0f4 100644 > > --- a/src/box/sql/func.c > > +++ b/src/box/sql/func.c > > @@ -65,6 +65,14 @@ mem_as_bin(struct Mem *mem) > > return s; > > } > > > > +static int > > +mem_get_length(struct Mem *mem) > > +{ > > + uint32_t len; > > + mem_get_bytes_len(mem, &len); > > mem_get_bytes_len() is never used except here. I suggest you to > rename it to mem_len(), drop mem_get_length(), and use mem_len() > everywhere. > > Since you ignore the error always, it probably must has 'unsafe' > suffix to emphasize it is broken. I added new function mem_len_unsafe(). I decided to left mem_get_bytes_len() since I think that there should be safe version of the function. In this case we do not have to write a new function when we need it. Diff: diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 0ea46f01c..9c28d5122 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -65,14 +65,6 @@ mem_as_bin(struct Mem *mem) return s; } -static int -mem_get_length(struct Mem *mem) -{ - uint32_t len; - mem_get_bytes_len(mem, &len); - return len; -} - /* * Return the collating function associated with a function. */ @@ -196,14 +188,14 @@ lengthFunc(sql_context * context, int argc, sql_value ** argv) case MP_BOOL: case MP_DOUBLE:{ mem_as_bin(argv[0]); - sql_result_uint(context, mem_get_length(argv[0])); + sql_result_uint(context, mem_len_unsafe(argv[0])); break; } case MP_STR:{ const unsigned char *z = mem_as_ustr(argv[0]); if (z == 0) return; - len = sql_utf8_char_count(z, mem_get_length(argv[0])); + len = sql_utf8_char_count(z, mem_len_unsafe(argv[0])); sql_result_uint(context, len); break; } @@ -313,8 +305,8 @@ position_func(struct sql_context *context, int argc, struct Mem **argv) return; } - int n_needle_bytes = mem_get_length(needle); - int n_haystack_bytes = mem_get_length(haystack); + int n_needle_bytes = mem_len_unsafe(needle); + int n_haystack_bytes = mem_len_unsafe(haystack); int position = 1; if (n_needle_bytes > 0) { const unsigned char *haystack_str; @@ -460,17 +452,17 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) p1 = mem_get_int_unsafe(argv[1]); if (p0type == MP_BIN) { z = mem_as_bin(argv[0]); - len = mem_get_length(argv[0]); + len = mem_len_unsafe(argv[0]); if (z == 0) return; - assert(len == mem_get_length(argv[0])); + assert(len == mem_len_unsafe(argv[0])); } else { z = mem_as_ustr(argv[0]); if (z == 0) return; len = 0; if (p1 < 0) - len = sql_utf8_char_count(z, mem_get_length(argv[0])); + len = sql_utf8_char_count(z, mem_len_unsafe(argv[0])); } if (argc == 3) { p2 = mem_get_int_unsafe(argv[2]); @@ -509,7 +501,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) * used because '\0' is not supposed to be * end-of-string symbol. */ - int byte_size = mem_get_length(argv[0]); + int byte_size = mem_len_unsafe(argv[0]); int n_chars = sql_utf8_char_count(z, byte_size); int cnt = 0; int i = 0; @@ -628,7 +620,7 @@ case_type##ICUFunc(sql_context *context, int argc, sql_value **argv) \ return; \ } \ z2 = mem_as_str0(argv[0]); \ - n = mem_get_length(argv[0]); \ + n = mem_len_unsafe(argv[0]); \ /* \ * Verify that the call to _bytes() \ * does not invalidate the _text() pointer. \ @@ -965,15 +957,15 @@ likeFunc(sql_context *context, int argc, sql_value **argv) } const char *zB = mem_as_str0(argv[0]); const char *zA = mem_as_str0(argv[1]); - const char *zB_end = zB + mem_get_length(argv[0]); - const char *zA_end = zA + mem_get_length(argv[1]); + const char *zB_end = zB + mem_len_unsafe(argv[0]); + const char *zA_end = zA + mem_len_unsafe(argv[1]); /* * Limit the length of the LIKE pattern to avoid problems * of deep recursion and N*N behavior in * sql_utf8_pattern_compare(). */ - nPat = mem_get_length(argv[0]); + nPat = mem_len_unsafe(argv[0]); testcase(nPat == db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]); testcase(nPat == db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH] + 1); if (nPat > db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]) { @@ -994,7 +986,7 @@ likeFunc(sql_context *context, int argc, sql_value **argv) const unsigned char *zEsc = mem_as_ustr(argv[2]); if (zEsc == 0) return; - if (sql_utf8_char_count(zEsc, mem_get_length(argv[2])) != 1) { + if (sql_utf8_char_count(zEsc, mem_len_unsafe(argv[2])) != 1) { diag_set(ClientError, ER_SQL_EXECUTE, "ESCAPE "\ "expression must be a single character"); context->is_aborted = true; @@ -1094,7 +1086,7 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) case MP_MAP: { char *zText = 0; char const *zBlob = mem_as_bin(argv[0]); - int nBlob = mem_get_length(argv[0]); + int nBlob = mem_len_unsafe(argv[0]); assert(zBlob == mem_as_bin(argv[0])); /* No encoding change */ zText = (char *)contextMalloc(context, @@ -1229,7 +1221,7 @@ hexFunc(sql_context * context, int argc, sql_value ** argv) assert(argc == 1); UNUSED_PARAMETER(argc); pBlob = mem_as_bin(argv[0]); - n = mem_get_length(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) { @@ -1287,7 +1279,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) zStr = mem_as_ustr(argv[0]); if (zStr == 0) return; - nStr = mem_get_length(argv[0]); + nStr = mem_len_unsafe(argv[0]); assert(zStr == mem_as_ustr(argv[0])); /* No encoding change */ zPattern = mem_as_ustr(argv[1]); if (zPattern == 0) { @@ -1295,7 +1287,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) || sql_context_db_handle(context)->mallocFailed); return; } - nPattern = mem_get_length(argv[1]); + nPattern = mem_len_unsafe(argv[1]); if (nPattern == 0) { assert(!mem_is_null(argv[1])); sql_result_value(context, argv[0]); @@ -1305,7 +1297,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) zRep = mem_as_ustr(argv[2]); if (zRep == 0) return; - nRep = mem_get_length(argv[2]); + nRep = mem_len_unsafe(argv[2]); assert(zRep == mem_as_ustr(argv[2])); nOut = nStr + 1; assert(nOut < SQL_MAX_LENGTH); @@ -1467,7 +1459,7 @@ trim_func_one_arg(struct sql_context *context, sql_value *arg) default_trim = (const unsigned char *) "\0"; else default_trim = (const unsigned char *) " "; - int input_str_sz = mem_get_length(arg); + int input_str_sz = mem_len_unsafe(arg); const unsigned char *input_str = mem_as_ustr(arg); uint8_t trim_char_len[1] = { 1 }; trim_procedure(context, TRIM_BOTH, default_trim, trim_char_len, 1, @@ -1493,14 +1485,14 @@ trim_func_two_args(struct sql_context *context, sql_value *arg1, if ((input_str = mem_as_ustr(arg2)) == NULL) return; - int input_str_sz = mem_get_length(arg2); + int input_str_sz = mem_len_unsafe(arg2); if (sql_value_type(arg1) == MP_INT || sql_value_type(arg1) == MP_UINT) { uint8_t len_one = 1; trim_procedure(context, mem_get_int_unsafe(arg1), (const unsigned char *) " ", &len_one, 1, input_str, input_str_sz); } else if ((trim_set = mem_as_ustr(arg1)) != NULL) { - int trim_set_sz = mem_get_length(arg1); + int trim_set_sz = mem_len_unsafe(arg1); uint8_t *char_len; int char_cnt = trim_prepare_char_len(context, trim_set, trim_set_sz, &char_len); @@ -1529,8 +1521,8 @@ trim_func_three_args(struct sql_context *context, sql_value *arg1, (trim_set = mem_as_ustr(arg2)) == NULL) return; - int trim_set_sz = mem_get_length(arg2); - int input_str_sz = mem_get_length(arg3); + int trim_set_sz = mem_len_unsafe(arg2); + int input_str_sz = mem_len_unsafe(arg3); uint8_t *char_len; int char_cnt = trim_prepare_char_len(context, trim_set, trim_set_sz, &char_len); @@ -1844,7 +1836,7 @@ groupConcatStep(sql_context * context, int argc, sql_value ** argv) if (!firstTerm) { if (argc == 2) { zSep = mem_as_str0(argv[1]); - nSep = mem_get_length(argv[1]); + nSep = mem_len_unsafe(argv[1]); } else { zSep = ","; nSep = 1; @@ -1853,7 +1845,7 @@ groupConcatStep(sql_context * context, int argc, sql_value ** argv) sqlStrAccumAppend(pAccum, zSep, nSep); } zVal = mem_as_str0(argv[0]); - nVal = mem_get_length(argv[0]); + nVal = mem_len_unsafe(argv[0]); if (zVal) sqlStrAccumAppend(pAccum, zVal, nVal); } diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index e7612fe7e..a67345fbd 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -870,6 +870,20 @@ mem_get_bin(const struct Mem *mem, const char **s); int mem_get_bytes_len(const struct Mem *mem, uint32_t *len); +/** + * Return length of value for MEM of STRING or VARBINARY type. This function is + * not safe since there is no proper processing in case mem_get_bytes_len() + * return an error. In this case this function returns 0. + */ +static inline int +mem_len_unsafe(const struct Mem *mem) +{ + uint32_t len; + if (mem_get_bytes_len(mem, &len) != 0) + return 0; + return len; +} + /** * Simple type to str convertor. It is used to simplify * error reporting. New patch: commit 20f06a5f2c52149060938136273b173790b11b47 Author: Mergen Imeev Date: Mon Mar 22 12:27:33 2021 +0300 sql: introduce mem_get_bytes_len() This patch introduces mem_get_bytes_len(). This function is used to receive length of string or binary value of MEM. If MEM is not of STRING or VARBINARy type this function returns -1. Part of #5818 diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 8b4882b19..9c28d5122 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -187,14 +187,15 @@ lengthFunc(sql_context * context, int argc, sql_value ** argv) case MP_UINT: case MP_BOOL: case MP_DOUBLE:{ - sql_result_uint(context, sql_value_bytes(argv[0])); + mem_as_bin(argv[0]); + sql_result_uint(context, mem_len_unsafe(argv[0])); break; } case MP_STR:{ const unsigned char *z = mem_as_ustr(argv[0]); if (z == 0) return; - len = sql_utf8_char_count(z, sql_value_bytes(argv[0])); + len = sql_utf8_char_count(z, mem_len_unsafe(argv[0])); sql_result_uint(context, len); break; } @@ -304,8 +305,8 @@ position_func(struct sql_context *context, int argc, struct Mem **argv) return; } - int n_needle_bytes = sql_value_bytes(needle); - int n_haystack_bytes = sql_value_bytes(haystack); + int n_needle_bytes = mem_len_unsafe(needle); + int n_haystack_bytes = mem_len_unsafe(haystack); int position = 1; if (n_needle_bytes > 0) { const unsigned char *haystack_str; @@ -450,18 +451,18 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) p0type = sql_value_type(argv[0]); p1 = mem_get_int_unsafe(argv[1]); if (p0type == MP_BIN) { - len = sql_value_bytes(argv[0]); z = mem_as_bin(argv[0]); + len = mem_len_unsafe(argv[0]); if (z == 0) return; - assert(len == sql_value_bytes(argv[0])); + assert(len == mem_len_unsafe(argv[0])); } else { z = mem_as_ustr(argv[0]); if (z == 0) return; len = 0; if (p1 < 0) - len = sql_utf8_char_count(z, sql_value_bytes(argv[0])); + len = sql_utf8_char_count(z, mem_len_unsafe(argv[0])); } if (argc == 3) { p2 = mem_get_int_unsafe(argv[2]); @@ -500,7 +501,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) * used because '\0' is not supposed to be * end-of-string symbol. */ - int byte_size = sql_value_bytes(argv[0]); + int byte_size = mem_len_unsafe(argv[0]); int n_chars = sql_utf8_char_count(z, byte_size); int cnt = 0; int i = 0; @@ -619,7 +620,7 @@ case_type##ICUFunc(sql_context *context, int argc, sql_value **argv) \ return; \ } \ z2 = mem_as_str0(argv[0]); \ - n = sql_value_bytes(argv[0]); \ + n = mem_len_unsafe(argv[0]); \ /* \ * Verify that the call to _bytes() \ * does not invalidate the _text() pointer. \ @@ -956,15 +957,15 @@ likeFunc(sql_context *context, int argc, sql_value **argv) } const char *zB = mem_as_str0(argv[0]); const char *zA = mem_as_str0(argv[1]); - const char *zB_end = zB + sql_value_bytes(argv[0]); - const char *zA_end = zA + sql_value_bytes(argv[1]); + const char *zB_end = zB + mem_len_unsafe(argv[0]); + const char *zA_end = zA + mem_len_unsafe(argv[1]); /* * Limit the length of the LIKE pattern to avoid problems * of deep recursion and N*N behavior in * sql_utf8_pattern_compare(). */ - nPat = sql_value_bytes(argv[0]); + nPat = mem_len_unsafe(argv[0]); testcase(nPat == db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]); testcase(nPat == db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH] + 1); if (nPat > db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]) { @@ -985,7 +986,7 @@ likeFunc(sql_context *context, int argc, sql_value **argv) const unsigned char *zEsc = mem_as_ustr(argv[2]); if (zEsc == 0) return; - if (sql_utf8_char_count(zEsc, sql_value_bytes(argv[2])) != 1) { + if (sql_utf8_char_count(zEsc, mem_len_unsafe(argv[2])) != 1) { diag_set(ClientError, ER_SQL_EXECUTE, "ESCAPE "\ "expression must be a single character"); context->is_aborted = true; @@ -1085,7 +1086,7 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) case MP_MAP: { char *zText = 0; char const *zBlob = mem_as_bin(argv[0]); - int nBlob = sql_value_bytes(argv[0]); + int nBlob = mem_len_unsafe(argv[0]); assert(zBlob == mem_as_bin(argv[0])); /* No encoding change */ zText = (char *)contextMalloc(context, @@ -1220,7 +1221,7 @@ hexFunc(sql_context * context, int argc, sql_value ** argv) assert(argc == 1); UNUSED_PARAMETER(argc); pBlob = mem_as_bin(argv[0]); - n = sql_value_bytes(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) { @@ -1278,7 +1279,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) zStr = mem_as_ustr(argv[0]); if (zStr == 0) return; - nStr = sql_value_bytes(argv[0]); + nStr = mem_len_unsafe(argv[0]); assert(zStr == mem_as_ustr(argv[0])); /* No encoding change */ zPattern = mem_as_ustr(argv[1]); if (zPattern == 0) { @@ -1286,7 +1287,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) || sql_context_db_handle(context)->mallocFailed); return; } - nPattern = sql_value_bytes(argv[1]); + nPattern = mem_len_unsafe(argv[1]); if (nPattern == 0) { assert(!mem_is_null(argv[1])); sql_result_value(context, argv[0]); @@ -1296,7 +1297,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) zRep = mem_as_ustr(argv[2]); if (zRep == 0) return; - nRep = sql_value_bytes(argv[2]); + nRep = mem_len_unsafe(argv[2]); assert(zRep == mem_as_ustr(argv[2])); nOut = nStr + 1; assert(nOut < SQL_MAX_LENGTH); @@ -1458,7 +1459,7 @@ trim_func_one_arg(struct sql_context *context, sql_value *arg) default_trim = (const unsigned char *) "\0"; else default_trim = (const unsigned char *) " "; - int input_str_sz = sql_value_bytes(arg); + int input_str_sz = mem_len_unsafe(arg); const unsigned char *input_str = mem_as_ustr(arg); uint8_t trim_char_len[1] = { 1 }; trim_procedure(context, TRIM_BOTH, default_trim, trim_char_len, 1, @@ -1484,14 +1485,14 @@ trim_func_two_args(struct sql_context *context, sql_value *arg1, if ((input_str = mem_as_ustr(arg2)) == NULL) return; - int input_str_sz = sql_value_bytes(arg2); + int input_str_sz = mem_len_unsafe(arg2); if (sql_value_type(arg1) == MP_INT || sql_value_type(arg1) == MP_UINT) { uint8_t len_one = 1; trim_procedure(context, mem_get_int_unsafe(arg1), (const unsigned char *) " ", &len_one, 1, input_str, input_str_sz); } else if ((trim_set = mem_as_ustr(arg1)) != NULL) { - int trim_set_sz = sql_value_bytes(arg1); + int trim_set_sz = mem_len_unsafe(arg1); uint8_t *char_len; int char_cnt = trim_prepare_char_len(context, trim_set, trim_set_sz, &char_len); @@ -1520,8 +1521,8 @@ trim_func_three_args(struct sql_context *context, sql_value *arg1, (trim_set = mem_as_ustr(arg2)) == NULL) return; - int trim_set_sz = sql_value_bytes(arg2); - int input_str_sz = sql_value_bytes(arg3); + int trim_set_sz = mem_len_unsafe(arg2); + int input_str_sz = mem_len_unsafe(arg3); uint8_t *char_len; int char_cnt = trim_prepare_char_len(context, trim_set, trim_set_sz, &char_len); @@ -1835,7 +1836,7 @@ groupConcatStep(sql_context * context, int argc, sql_value ** argv) if (!firstTerm) { if (argc == 2) { zSep = mem_as_str0(argv[1]); - nSep = sql_value_bytes(argv[1]); + nSep = mem_len_unsafe(argv[1]); } else { zSep = ","; nSep = 1; @@ -1844,7 +1845,7 @@ groupConcatStep(sql_context * context, int argc, sql_value ** argv) sqlStrAccumAppend(pAccum, zSep, nSep); } zVal = mem_as_str0(argv[0]); - nVal = sql_value_bytes(argv[0]); + nVal = mem_len_unsafe(argv[0]); if (zVal) sqlStrAccumAppend(pAccum, zVal, nVal); } diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 2ad853a40..0addff161 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1156,6 +1156,18 @@ mem_get_bin(const struct Mem *mem, const char **s) return 0; } +int +mem_get_bytes_len(const struct Mem *mem, uint32_t *len) +{ + if ((mem->flags & (MEM_Str | MEM_Blob)) == 0) + return -1; + if ((mem->flags & MEM_Blob) !=0 && (mem->flags & MEM_Zero) != 0) + *len = mem->n + mem->u.nZero; + else + *len = mem->n; + return 0; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -1892,41 +1904,6 @@ sql_value_type(sql_value *pVal) return mem_mp_type(mem); } -/* - * The sqlValueBytes() routine returns the number of bytes in the - * sql_value object assuming that it uses the encoding "enc". - * The valueBytes() routine is a helper function. - */ -static SQL_NOINLINE int -valueBytes(sql_value * pVal) -{ - if (mem_to_str(pVal) != 0) - return 0; - return pVal->n; -} - -int -sqlValueBytes(sql_value * pVal) -{ - Mem *p = (Mem *) pVal; - assert((p->flags & MEM_Null) == 0 - || (p->flags & (MEM_Str | MEM_Blob)) == 0); - if ((p->flags & MEM_Str) != 0) { - return p->n; - } - if ((p->flags & MEM_Blob) != 0) { - if (p->flags & MEM_Zero) { - return p->n + p->u.nZero; - } else { - return p->n; - } - } - if (p->flags & MEM_Null) - return 0; - return valueBytes(pVal); -} - - #ifdef SQL_DEBUG /* * Check invariants on a Mem object. @@ -2233,53 +2210,6 @@ releaseMemArray(Mem * p, int N) } } -int -sql_value_bytes(sql_value * pVal) -{ - return sqlValueBytes(pVal); -} - -/* - * Return a pointer to static memory containing an SQL NULL value. - */ -const Mem * -columnNullValue(void) -{ - /* Even though the Mem structure contains an element - * of type i64, on certain architectures (x86) with certain compiler - * switches (-Os), gcc may align this Mem object on a 4-byte boundary - * instead of an 8-byte one. This all works fine, except that when - * running with SQL_DEBUG defined the sql code sometimes assert()s - * that a Mem structure is located on an 8-byte boundary. To prevent - * these assert()s from failing, when building with SQL_DEBUG defined - * using gcc, we force nullMem to be 8-byte aligned using the magical - * __attribute__((aligned(8))) macro. - */ - static const Mem nullMem -#if defined(SQL_DEBUG) && defined(__GNUC__) - __attribute__ ((aligned(8))) -#endif - = { - /* .u = */ { - 0}, - /* .flags = */ (u16) MEM_Null, - /* .eSubtype = */ (u8) 0, - /* .field_type = */ field_type_MAX, - /* .n = */ (int)0, - /* .z = */ (char *)0, - /* .zMalloc = */ (char *)0, - /* .szMalloc = */ (int)0, - /* .uTemp = */ (u32) 0, - /* .db = */ (sql *) 0, - /* .xDel = */ (void (*)(void *))0, -#ifdef SQL_DEBUG - /* .pScopyFrom = */ (Mem *) 0, - /* .pFiller = */ (void *)0, -#endif - }; - return &nullMem; -} - /* * Return true if the Mem object contains a TEXT or BLOB that is * too large - whose size exceeds SQL_MAX_LENGTH. diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 00c9bab36..a67345fbd 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -863,6 +863,27 @@ mem_as_str0(struct Mem *mem) int mem_get_bin(const struct Mem *mem, const char **s); +/** + * Return length of value for MEM of STRING or VARBINARY type. Original MEM is + * not changed. + */ +int +mem_get_bytes_len(const struct Mem *mem, uint32_t *len); + +/** + * Return length of value for MEM of STRING or VARBINARY type. This function is + * not safe since there is no proper processing in case mem_get_bytes_len() + * return an error. In this case this function returns 0. + */ +static inline int +mem_len_unsafe(const struct Mem *mem) +{ + uint32_t len; + if (mem_get_bytes_len(mem, &len) != 0) + return 0; + return len; +} + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -881,8 +902,6 @@ mem_mp_type(struct Mem *mem); enum mp_type sql_value_type(struct Mem *); -int sqlValueBytes(struct Mem *); - #ifdef SQL_DEBUG int sqlVdbeCheckMemInvariants(struct Mem *); void sqlVdbeMemPrettyPrint(Mem * pMem, char *zBuf); @@ -912,18 +931,8 @@ struct Mem *sqlValueNew(struct sql *); void releaseMemArray(Mem * p, int N); -/** Getters. */ - -int -sql_value_bytes(struct Mem *); - #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) -const Mem * -columnNullValue(void); - -/** Checkers. */ - int sqlVdbeMemTooBig(Mem *); /* Return TRUE if Mem X contains dynamically allocated content - anything diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 8a7a87b49..8feb112f5 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -433,9 +433,6 @@ sql_stmt_compile(const char *sql, int bytes_count, struct Vdbe *re_prepared, int sql_step(sql_stmt *); -int -sql_column_bytes(sql_stmt *, int iCol); - int sql_column_bytes16(sql_stmt *, int iCol); diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 5bb02e541..2bec2b186 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -422,41 +422,6 @@ sql_data_count(sql_stmt * pStmt) return pVm->nResColumn; } -/* - * Check to see if column iCol of the given statement is valid. If - * it is, return a pointer to the Mem for the value of that column. - * If iCol is not valid, return a pointer to a Mem which has a value - * of NULL. - */ -static Mem * -columnMem(sql_stmt * pStmt, int i) -{ - Vdbe *pVm; - Mem *pOut; - - pVm = (Vdbe *) pStmt; - if (pVm == 0) - return (Mem *) columnNullValue(); - assert(pVm->db); - if (pVm->pResultSet != 0 && i < pVm->nResColumn && i >= 0) { - pOut = &pVm->pResultSet[i]; - } else { - pOut = (Mem *) columnNullValue(); - } - return pOut; -} - -/**************************** sql_column_ ****************************** - * The following routines are used to access elements of the current row - * in the result set. - */ - -int -sql_column_bytes(sql_stmt * pStmt, int i) -{ - return sql_value_bytes(columnMem(pStmt, i)); -} - char * sql_result_to_msgpack(struct sql_stmt *stmt, uint32_t *tuple_size, struct region *region)