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 A3FB76B962; Wed, 14 Apr 2021 04:18:02 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A3FB76B962 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618363082; bh=Q9UAt4JoY5/zWD23uA6XKUGw0HdTxHHZTDfuS2p2d84=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Kytg34E0Nog65pMxwVVG0eVt4jbPiZ8jwSBfn7x7D4Si83A8srXhDXl2KhLpcw45V k/HE4sAsjeJooTZM6rdCnJzrndIAa9pJt74vdRY1D0k+WD5cH3jzt9xwuBLbakqSyO 3l7W1pUfpWoy8N44LF/fxzoSIDStN/Mvfkue949M= Received: from smtp29.i.mail.ru (smtp29.i.mail.ru [94.100.177.89]) (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 6CBF76EC5B for ; Wed, 14 Apr 2021 04:18:01 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6CBF76EC5B Received: by smtp29.i.mail.ru with esmtpa (envelope-from ) id 1lWUAK-0003SZ-KI; Wed, 14 Apr 2021 04:18:01 +0300 Date: Wed, 14 Apr 2021 04:17:59 +0300 To: Vladislav Shpilevoy , tarantool-patches@dev.tarantool.org Message-ID: <20210414011759.GA155783@tarantool.org> References: <921ae5a9b533bd897100dc2e7923347638719b13.1618000037.git.imeevma@gmail.com> <57fdcb76-1935-ff3d-ca70-9edf3a4bd640@tarantool.org> <20210414002832.GA130721@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210414002832.GA130721@tarantool.org> X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480BE79914FF86F9151AC38CC435EA4A654182A05F538085040189903CDB2B20E4FE30A4975C80D01279DB1C86DE6863739346FA979EE9305A8 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE701173C01F417A2A6EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C5BAFDCCAC60DA968638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B250F38E0AA74AB267B06EB96BAC48DD0CE8578C74B6F99271D2E47CDBA5A96583C09775C1D3CA48CFCA5A41EBD8A3A0199FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3CA5A41EBD8A3A0199FA2833FD35BB23DF004C90652538430302FCEF25BFAB3454AD6D5ED66289B5278DA827A17800CE7711DDB4AE5B425DBD32BA5DBAC0009BE395957E7521B51C20BC6067A898B09E4090A508E0FED6299176DF2183F8FC7C0B01F8B705E65F4D2B3661434B16C20AC78D18283394535A9E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B677084C381D43A98D089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2368A440D3B0F6089093C9A16E5BC824A2A04A2ABAA09D2538DECB10C4E1BE3BCB8CCF60F6B7FD3948E1CD14B953EB46DDF8FFCB394E3844F355D89D7DBCDD132 X-C1DE0DAB: 0D63561A33F958A5ECDD6A53621D6BFEE9899DC522485D1F95A705393E3E0CB4D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34FF70CCB59A6ED870AA309ECF23BB69DC4ED63D6FC802A4DEEE97C0C4FA088767AACE6C33081217BC1D7E09C32AA3244C8E010D38A7BF2060786CAD7AFEC65E3081560E2432555DBB729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojnA7/qPBUIXGiBhi7lFFpYg== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED714C8DADB9701F42E30A4975C80D0127D776CA2DFBE07C795105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v5 45/52] sql: introduce mem_get_int() 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 Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" I reworker mem_get_int_unsafe() a bit. Now it return 0 in case mem_get_int() fails. I included only diff. On Wed, Apr 14, 2021 at 03:28:32AM +0300, Mergen Imeev via Tarantool-patches wrote: > Thank you for the review! My answers, diff and new patch below. > > On Wed, Apr 14, 2021 at 01:01:02AM +0200, Vladislav Shpilevoy wrote: > > Nice fixes! > > > > On 09.04.2021 22:53, Mergen Imeev via Tarantool-patches wrote: > > > Thank you for the review! My answers and new patch below. > > > > > > > > > On 30.03.2021 02:08, Vladislav Shpilevoy wrote: > > >> Thanks for the patch! > > >> > > >>> diff --git a/src/box/sql/func.c b/src/box/sql/func.c > > >>> index b644c39d8..0fa0f6ac7 100644 > > >>> --- a/src/box/sql/func.c > > >>> +++ b/src/box/sql/func.c > > >>> @@ -1532,10 +1543,11 @@ hexFunc(sql_context * context, int argc, sql_value ** argv) > > >>> static void > > >>> zeroblobFunc(sql_context * context, int argc, sql_value ** argv) > > >>> { > > >>> - i64 n; > > >>> + int64_t n; > > >>> assert(argc == 1); > > >>> UNUSED_PARAMETER(argc); > > >>> - n = sql_value_int64(argv[0]); > > >>> + bool unused; > > >>> + mem_get_integer(argv[0], &n, &unused); > > >> > > >> The flag is never used anywhere except one assertion where you can > > >> check the integer value instead. I think you can drop this out > > >> parameter. In future we could add mem_get_int_with_sign() or something > > >> like that if necessary. > > > I think the problem here mostly because most of built-in functions and bitwise > > > operations cannot work with our INTEGER. They can only work with int64. I > > > believe, if we fix this problem, there will be no problems with having this > > > flag. > > > > My complaint is about the flag. The third argument which is almost never > > used. It makes the code ugly, and does not give a clue it is broken in fact. > > When uint64_t is > INT64_MAX and is returned as int64_t and the flag is > > ignored. > > > > What about mem_get_int_unsafe()? It would return int64_t truncated like > > before. Return as 'return', not out parameter. Because we also never check > > for fail as I see. And no 'unused' flag. But we would clearly see that these > > places are broken and need attention. > I agree that function with such name would be a good indicator that something > may go wrong here. I created new function, mem_get_int_unfase(). > > > Diff: > > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > index 701e77d49..0db698174 100644 > --- a/src/box/sql/func.c > +++ b/src/box/sql/func.c > @@ -205,9 +205,7 @@ absFunc(sql_context * context, int argc, sql_value ** argv) > break; > } > case MP_INT: { > - bool unused; > - int64_t value; > - mem_get_int(argv[0], &value, &unused); > + int64_t value = mem_get_int_unsafe(argv[0]); > assert(value < 0); > sql_result_uint(context, -value); > break; > @@ -435,8 +433,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) > if (mem_is_null(argv[1]) || (argc == 3 && mem_is_null(argv[2]))) > return; > p0type = sql_value_type(argv[0]); > - bool unused; > - mem_get_int(argv[1], &p1, &unused); > + p1 = mem_get_int_unsafe(argv[1]); > if (p0type == MP_BIN) { > len = sql_value_bytes(argv[0]); > z = sql_value_blob(argv[0]); > @@ -452,7 +449,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) > len = sql_utf8_char_count(z, sql_value_bytes(argv[0])); > } > if (argc == 3) { > - mem_get_int(argv[2], &p2, &unused); > + p2 = mem_get_int_unsafe(argv[2]); > if (p2 < 0) { > p2 = -p2; > negP2 = 1; > @@ -534,8 +531,7 @@ roundFunc(sql_context * context, int argc, sql_value ** argv) > if (argc == 2) { > if (mem_is_null(argv[1])) > return; > - bool unused; > - mem_get_int(argv[1], &n, &unused); > + n = mem_get_int_unsafe(argv[1]); > if (n < 0) > n = 0; > } > @@ -688,8 +684,7 @@ randomBlob(sql_context * context, int argc, sql_value ** argv) > context->is_aborted = true; > return; > } > - bool unused; > - mem_get_int(argv[0], &n, &unused); > + n = mem_get_int_unsafe(argv[0]); > if (n < 1) > return; > p = contextMalloc(context, n); > @@ -1233,8 +1228,7 @@ zeroblobFunc(sql_context * context, int argc, sql_value ** argv) > int64_t n; > assert(argc == 1); > UNUSED_PARAMETER(argc); > - bool unused; > - mem_get_int(argv[0], &n, &unused); > + n = mem_get_int_unsafe(argv[0]); > if (n < 0) > n = 0; > if (sql_result_zeroblob64(context, n) != 0) { > @@ -1478,9 +1472,9 @@ trim_func_two_args(struct sql_context *context, sql_value *arg1, > int input_str_sz = sql_value_bytes(arg2); > if (sql_value_type(arg1) == MP_INT || sql_value_type(arg1) == MP_UINT) { > uint8_t len_one = 1; > - uint64_t n = sql_value_uint64(arg1); > - trim_procedure(context, n, (const unsigned char *) " ", > - &len_one, 1, input_str, input_str_sz); > + trim_procedure(context, mem_get_int_unsafe(arg1), > + (const unsigned char *) " ", &len_one, 1, > + input_str, input_str_sz); > } else if ((trim_set = sql_value_text(arg1)) != NULL) { > int trim_set_sz = sql_value_bytes(arg1); > uint8_t *char_len; > @@ -1518,8 +1512,7 @@ trim_func_three_args(struct sql_context *context, sql_value *arg1, > &char_len); > if (char_cnt == -1) > return; > - uint64_t n = sql_value_uint64(arg1); > - trim_procedure(context, n, trim_set, char_len, > + trim_procedure(context, mem_get_int_unsafe(arg1), trim_set, char_len, > char_cnt, input_str, input_str_sz); > sql_free(char_len); > } > @@ -1658,9 +1651,7 @@ sum_step(struct sql_context *context, int argc, sql_value **argv) > } > p->cnt++; > if (type == MP_INT || type == MP_UINT) { > - bool unused; > - int64_t v; > - mem_get_int(argv[0], &v, &unused); > + int64_t v = mem_get_int_unsafe(argv[0]); > if (type == MP_INT) > p->rSum += v; > else > diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h > index 313ca0ab2..f3d9043e5 100644 > --- a/src/box/sql/mem.h > +++ b/src/box/sql/mem.h > @@ -754,6 +754,20 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type type); > int > mem_get_int(const struct Mem *mem, int64_t *i, bool *is_neg); > > +/** > + * Return value of MEM converted to int64_t. This function is not safe, since it > + * works incorrectly with integer values that are more than INT64_MAX. Also, its > + * behaviour is undefined if mem_get_int() returned an error. > + */ > +static inline int64_t > +mem_get_int_unsafe(const struct Mem *mem) > +{ > + int64_t i; > + bool is_neg; > + mem_get_int(mem, &i, &is_neg); > + return i; > +} > + > /** > * Simple type to str convertor. It is used to simplify > * error reporting. > diff --git a/src/box/sql/printf.c b/src/box/sql/printf.c > index 09da39e81..eb8413f9c 100644 > --- a/src/box/sql/printf.c > +++ b/src/box/sql/printf.c > @@ -144,10 +144,7 @@ getIntArg(PrintfArguments * p) > { > if (p->nArg <= p->nUsed) > return 0; > - int64_t i; > - bool unused; > - mem_get_int(p->apArg[p->nUsed++], &i, &unused); > - return (sql_int64)i; > + return mem_get_int_unsafe(p->apArg[p->nUsed++]); > } > > static double > > > New patch: > > > commit 9472eeb564bd0caa039fe49c8fef4c1a7775ce80 > Author: Mergen Imeev > Date: Wed Mar 17 13:20:37 2021 +0300 > > sql: introduce mem_get_int() > > This patch introduces mem_get_int() function. This function is used to > receive integer value from MEM. If value of MEM is not integer, it is > converted to integer if possible. MEM is not changed. > > Part of #5818 > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > index 0282aec74..0db698174 100644 > --- a/src/box/sql/func.c > +++ b/src/box/sql/func.c > @@ -205,7 +205,7 @@ absFunc(sql_context * context, int argc, sql_value ** argv) > break; > } > case MP_INT: { > - int64_t value = sql_value_int64(argv[0]); > + int64_t value = mem_get_int_unsafe(argv[0]); > assert(value < 0); > sql_result_uint(context, -value); > break; > @@ -421,7 +421,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) > const unsigned char *z2; > int len; > int p0type; > - i64 p1, p2; > + int64_t p1, p2; > int negP2 = 0; > > if (argc != 2 && argc != 3) { > @@ -433,7 +433,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) > if (mem_is_null(argv[1]) || (argc == 3 && mem_is_null(argv[2]))) > return; > p0type = sql_value_type(argv[0]); > - p1 = sql_value_int(argv[1]); > + p1 = mem_get_int_unsafe(argv[1]); > if (p0type == MP_BIN) { > len = sql_value_bytes(argv[0]); > z = sql_value_blob(argv[0]); > @@ -449,7 +449,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) > len = sql_utf8_char_count(z, sql_value_bytes(argv[0])); > } > if (argc == 3) { > - p2 = sql_value_int(argv[2]); > + p2 = mem_get_int_unsafe(argv[2]); > if (p2 < 0) { > p2 = -p2; > negP2 = 1; > @@ -520,7 +520,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) > static void > roundFunc(sql_context * context, int argc, sql_value ** argv) > { > - int n = 0; > + int64_t n = 0; > double r; > if (argc != 1 && argc != 2) { > diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, "ROUND", > @@ -531,7 +531,7 @@ roundFunc(sql_context * context, int argc, sql_value ** argv) > if (argc == 2) { > if (mem_is_null(argv[1])) > return; > - n = sql_value_int(argv[1]); > + n = mem_get_int_unsafe(argv[1]); > if (n < 0) > n = 0; > } > @@ -674,7 +674,7 @@ randomFunc(sql_context * context, int NotUsed, sql_value ** NotUsed2) > static void > randomBlob(sql_context * context, int argc, sql_value ** argv) > { > - int n; > + int64_t n; > unsigned char *p; > assert(argc == 1); > UNUSED_PARAMETER(argc); > @@ -684,7 +684,7 @@ randomBlob(sql_context * context, int argc, sql_value ** argv) > context->is_aborted = true; > return; > } > - n = sql_value_int(argv[0]); > + n = mem_get_int_unsafe(argv[0]); > if (n < 1) > return; > p = contextMalloc(context, n); > @@ -1225,10 +1225,10 @@ hexFunc(sql_context * context, int argc, sql_value ** argv) > static void > zeroblobFunc(sql_context * context, int argc, sql_value ** argv) > { > - i64 n; > + int64_t n; > assert(argc == 1); > UNUSED_PARAMETER(argc); > - n = sql_value_int64(argv[0]); > + n = mem_get_int_unsafe(argv[0]); > if (n < 0) > n = 0; > if (sql_result_zeroblob64(context, n) != 0) { > @@ -1472,7 +1472,7 @@ trim_func_two_args(struct sql_context *context, sql_value *arg1, > int input_str_sz = sql_value_bytes(arg2); > if (sql_value_type(arg1) == MP_INT || sql_value_type(arg1) == MP_UINT) { > uint8_t len_one = 1; > - trim_procedure(context, sql_value_int(arg1), > + trim_procedure(context, mem_get_int_unsafe(arg1), > (const unsigned char *) " ", &len_one, 1, > input_str, input_str_sz); > } else if ((trim_set = sql_value_text(arg1)) != NULL) { > @@ -1512,7 +1512,7 @@ trim_func_three_args(struct sql_context *context, sql_value *arg1, > &char_len); > if (char_cnt == -1) > return; > - trim_procedure(context, sql_value_int(arg1), trim_set, char_len, > + trim_procedure(context, mem_get_int_unsafe(arg1), trim_set, char_len, > char_cnt, input_str, input_str_sz); > sql_free(char_len); > } > @@ -1651,7 +1651,7 @@ sum_step(struct sql_context *context, int argc, sql_value **argv) > } > p->cnt++; > if (type == MP_INT || type == MP_UINT) { > - int64_t v = sql_value_int64(argv[0]); > + int64_t v = mem_get_int_unsafe(argv[0]); > if (type == MP_INT) > p->rSum += v; > else > diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c > index 537288c14..adf5e236b 100644 > --- a/src/box/sql/mem.c > +++ b/src/box/sql/mem.c > @@ -1042,6 +1042,38 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type type) > return -1; > } > > +int > +mem_get_int(const struct Mem *mem, int64_t *i, bool *is_neg) > +{ > + if ((mem->flags & MEM_Int) != 0) { > + *i = mem->u.i; > + *is_neg = true; > + return 0; > + } > + if ((mem->flags & MEM_UInt) != 0) { > + *i = mem->u.i; > + *is_neg = false; > + return 0; > + } > + if ((mem->flags & (MEM_Str | MEM_Blob)) != 0) > + return sql_atoi64(mem->z, i, is_neg, mem->n); > + if ((mem->flags & MEM_Real) != 0) { > + double d = mem->u.r; > + if (d < 0 && d >= (double)INT64_MIN) { > + *i = (int64_t)d; > + *is_neg = true; > + return 0; > + } > + if (d >= 0 && d < (double)UINT64_MAX) { > + *i = (int64_t)(uint64_t)d; > + *is_neg = false; > + return 0; > + } > + return -1; > + } > + return -1; > +} > + > int > mem_copy(struct Mem *to, const struct Mem *from) > { > @@ -1400,12 +1432,12 @@ bitwise_prepare(const struct Mem *left, const struct Mem *right, > int64_t *a, int64_t *b) > { > bool unused; > - if (sqlVdbeIntValue(left, a, &unused) != 0) { > + if (mem_get_int(left, a, &unused) != 0) { > diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left), > "integer"); > return -1; > } > - if (sqlVdbeIntValue(right, b, &unused) != 0) { > + if (mem_get_int(right, b, &unused) != 0) { > diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right), > "integer"); > return -1; > @@ -1494,7 +1526,7 @@ mem_bit_not(const struct Mem *mem, struct Mem *result) > return 0; > int64_t i; > bool unused; > - if (sqlVdbeIntValue(mem, &i, &unused) != 0) { > + if (mem_get_int(mem, &i, &unused) != 0) { > diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(mem), > "integer"); > return -1; > @@ -1708,35 +1740,6 @@ valueToText(sql_value * pVal) > return pVal->z; > } > > -/* > - * Convert a 64-bit IEEE double into a 64-bit signed integer. > - * If the double is out of range of a 64-bit signed integer then > - * return the closest available 64-bit signed integer. > - */ > -static int > -doubleToInt64(double r, int64_t *i) > -{ > - /* > - * Many compilers we encounter do not define constants for the > - * minimum and maximum 64-bit integers, or they define them > - * inconsistently. And many do not understand the "LL" notation. > - * So we define our own static constants here using nothing > - * larger than a 32-bit integer constant. > - */ > - static const int64_t maxInt = LARGEST_INT64; > - static const int64_t minInt = SMALLEST_INT64; > - if (r <= (double)minInt) { > - *i = minInt; > - return -1; > - } else if (r >= (double)maxInt) { > - *i = maxInt; > - return -1; > - } else { > - *i = (int64_t) r; > - return *i != r; > - } > -} > - > /* > * It is already known that pMem contains an unterminated string. > * Add the zero terminator. > @@ -2210,42 +2213,6 @@ mem_value_bool(const struct Mem *mem, bool *b) > return -1; > } > > -/* > - * Return some kind of integer value which is the best we can do > - * at representing the value that *pMem describes as an integer. > - * If pMem is an integer, then the value is exact. If pMem is > - * a floating-point then the value returned is the integer part. > - * If pMem is a string or blob, then we make an attempt to convert > - * it into an integer and return that. If pMem represents an > - * an SQL-NULL value, return 0. > - * > - * If pMem represents a string value, its encoding might be changed. > - */ > -int > -sqlVdbeIntValue(const struct Mem *pMem, int64_t *i, bool *is_neg) > -{ > - int flags; > - assert(EIGHT_BYTE_ALIGNMENT(pMem)); > - flags = pMem->flags; > - if (flags & MEM_Int) { > - *i = pMem->u.i; > - *is_neg = true; > - return 0; > - } else if (flags & MEM_UInt) { > - *i = pMem->u.u; > - *is_neg = false; > - return 0; > - } else if (flags & MEM_Real) { > - *is_neg = pMem->u.r < 0; > - return doubleToInt64(pMem->u.r, i); > - } else if (flags & (MEM_Str)) { > - assert(pMem->z || pMem->n == 0); > - if (sql_atoi64(pMem->z, i, is_neg, pMem->n) == 0) > - return 0; > - } > - return -1; > -} > - > /* > * Return the best representation of pMem that we can get into a > * double. If pMem is already a double or an integer, return its > @@ -2316,30 +2283,12 @@ sql_value_boolean(sql_value *val) > return b; > } > > -int > -sql_value_int(sql_value * pVal) > -{ > - int64_t i = 0; > - bool is_neg; > - sqlVdbeIntValue((Mem *) pVal, &i, &is_neg); > - return (int)i; > -} > - > -sql_int64 > -sql_value_int64(sql_value * pVal) > -{ > - int64_t i = 0; > - bool unused; > - sqlVdbeIntValue((Mem *) pVal, &i, &unused); > - return i; > -} > - > uint64_t > sql_value_uint64(sql_value *val) > { > int64_t i = 0; > bool is_neg; > - sqlVdbeIntValue((struct Mem *) val, &i, &is_neg); > + mem_get_int((struct Mem *) val, &i, &is_neg); > assert(!is_neg); > return i; > } > diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h > index 91c1c464f..f3d9043e5 100644 > --- a/src/box/sql/mem.h > +++ b/src/box/sql/mem.h > @@ -746,6 +746,28 @@ mem_cast_implicit(struct Mem *mem, enum field_type type); > int > mem_cast_implicit_old(struct Mem *mem, enum field_type type); > > +/** > + * Return value for MEM of INTEGER type. For MEM of all other types convert > + * value of the MEM to INTEGER if possible and return converted value. Original > + * MEM is not changed. > + */ > +int > +mem_get_int(const struct Mem *mem, int64_t *i, bool *is_neg); > + > +/** > + * Return value of MEM converted to int64_t. This function is not safe, since it > + * works incorrectly with integer values that are more than INT64_MAX. Also, its > + * behaviour is undefined if mem_get_int() returned an error. > + */ > +static inline int64_t > +mem_get_int_unsafe(const struct Mem *mem) > +{ > + int64_t i; > + bool is_neg; > + mem_get_int(mem, &i, &is_neg); > + return i; > +} > + > /** > * Simple type to str convertor. It is used to simplify > * error reporting. > @@ -800,7 +822,6 @@ releaseMemArray(Mem * p, int N); > > int > mem_value_bool(const struct Mem *mem, bool *b); > -int sqlVdbeIntValue(const struct Mem *, int64_t *, bool *is_neg); > int sqlVdbeRealValue(struct Mem *, double *); > const void * > sql_value_blob(struct Mem *); > @@ -814,12 +835,6 @@ sql_value_double(struct Mem *); > bool > sql_value_boolean(struct Mem *val); > > -int > -sql_value_int(struct Mem *); > - > -sql_int64 > -sql_value_int64(struct Mem *); > - > uint64_t > sql_value_uint64(struct Mem *val); > > diff --git a/src/box/sql/printf.c b/src/box/sql/printf.c > index cf32ba3f3..eb8413f9c 100644 > --- a/src/box/sql/printf.c > +++ b/src/box/sql/printf.c > @@ -144,7 +144,7 @@ getIntArg(PrintfArguments * p) > { > if (p->nArg <= p->nUsed) > return 0; > - return sql_value_int64(p->apArg[p->nUsed++]); > + return mem_get_int_unsafe(p->apArg[p->nUsed++]); > } > > static double > diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h > index 7a026d21b..0af247ebf 100644 > --- a/src/box/sql/sqlInt.h > +++ b/src/box/sql/sqlInt.h > @@ -445,15 +445,9 @@ sql_column_bytes16(sql_stmt *, int iCol); > double > sql_column_double(sql_stmt *, int iCol); > > -int > -sql_column_int(sql_stmt *, int iCol); > - > bool > sql_column_boolean(struct sql_stmt *stmt, int column); > > -sql_int64 > -sql_column_int64(sql_stmt *, int iCol); > - > uint64_t > sql_column_uint64(struct sql_stmt *stmt, int column); > > diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c > index af1174d0a..5e5957496 100644 > --- a/src/box/sql/vdbeapi.c > +++ b/src/box/sql/vdbeapi.c > @@ -470,24 +470,12 @@ sql_column_double(sql_stmt * pStmt, int i) > return sql_value_double(columnMem(pStmt, i)); > } > > -int > -sql_column_int(sql_stmt * pStmt, int i) > -{ > - return sql_value_int(columnMem(pStmt, i)); > -} > - > bool > sql_column_boolean(struct sql_stmt *stmt, int i) > { > return sql_value_boolean(columnMem(stmt, i)); > } > > -sql_int64 > -sql_column_int64(sql_stmt * pStmt, int i) > -{ > - return sql_value_int64(columnMem(pStmt, i)); > -} > - > uint64_t > sql_column_uint64(sql_stmt * pStmt, int i) > { Diff: diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index f3d9043e5..8670664c9 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -756,15 +756,16 @@ mem_get_int(const struct Mem *mem, int64_t *i, bool *is_neg); /** * Return value of MEM converted to int64_t. This function is not safe, since it - * works incorrectly with integer values that are more than INT64_MAX. Also, its - * behaviour is undefined if mem_get_int() returned an error. + * returns 0 if mem_get_int() fails. There is no proper handling for this case. + * Also it works incorrectly with integer values that are more than INT64_MAX. */ static inline int64_t mem_get_int_unsafe(const struct Mem *mem) { int64_t i; bool is_neg; - mem_get_int(mem, &i, &is_neg); + if (mem_get_int(mem, &i, &is_neg) != 0) + return 0; return i; }