From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 20BA629485 for ; Mon, 22 Apr 2019 14:02:26 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vXv1-FA2oasu for ; Mon, 22 Apr 2019 14:02:26 -0400 (EDT) Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id C191329446 for ; Mon, 22 Apr 2019 14:02:25 -0400 (EDT) Subject: [tarantool-patches] Re: [PATCH 2/9] sql: disallow text values participate in sum() aggregate References: <829e1d67-9335-65a0-714f-a5684dc3aab5@tarantool.org> From: Vladislav Shpilevoy Message-ID: <6484927f-90fc-666f-7516-f11005f09d2c@tarantool.org> Date: Mon, 22 Apr 2019 21:02:22 +0300 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: "n.pettik" , tarantool-patches@freelists.org Hi! Thanks for the fixes! > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > index b86a95d9a..9adfeec67 100644 > --- a/src/box/sql/func.c > +++ b/src/box/sql/func.c > @@ -1495,24 +1495,34 @@ static void > sumStep(sql_context * context, int argc, sql_value ** argv) > { > SumCtx *p; > - int type; > assert(argc == 1); > UNUSED_PARAMETER(argc); > p = sql_aggregate_context(context, sizeof(*p)); > - type = sql_value_numeric_type(argv[0]); > - if (p && type != SQL_NULL) { > - p->cnt++; > - if (type == SQL_INTEGER) { > - i64 v = sql_value_int64(argv[0]); > - p->rSum += v; > - if ((p->approx | p->overflow) == 0 > - && sqlAddInt64(&p->iSum, v)) { > - p->overflow = 1; > - } > - } else { > - p->rSum += sql_value_double(argv[0]); > - p->approx = 1; > + assert(p != NULL); Why are you sure, that p != NULL? sql_aggregate_context() on first invocation allocates memory, and it can fail. > + int type = sql_value_type(argv[0]); > + if (type == SQL_NULL) > + return; I've fixed the comment above and also I see, that sumStep is rewritten almost completely, so it is time to convert it to Tarantool style. See the diff below and on the branch in a separate commit. (Note, 'sql_value' below is not prepended with 'struct' because it is a typedef - compiler curses). ================================================================== diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 9adfeec67..0f9e228dc 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1492,15 +1492,13 @@ struct SumCtx { * it overflows an integer. */ static void -sumStep(sql_context * context, int argc, sql_value ** argv) +sum_step(struct sql_context *context, int argc, sql_value **argv) { - SumCtx *p; assert(argc == 1); UNUSED_PARAMETER(argc); - p = sql_aggregate_context(context, sizeof(*p)); - assert(p != NULL); + struct SumCtx *p = sql_aggregate_context(context, sizeof(*p)); int type = sql_value_type(argv[0]); - if (type == SQL_NULL) + if (type == SQL_NULL || p == NULL) return; if (type != SQL_FLOAT && type != SQL_INTEGER) { if (mem_apply_numeric_type(argv[0]) != 0) { @@ -1514,10 +1512,10 @@ sumStep(sql_context * context, int argc, sql_value ** argv) } p->cnt++; if (type == SQL_INTEGER) { - i64 v = sql_value_int64(argv[0]); + int64_t v = sql_value_int64(argv[0]); p->rSum += v; if ((p->approx | p->overflow) == 0 && - sqlAddInt64(&p->iSum, v)) { + sqlAddInt64(&p->iSum, v) != 0) { p->overflow = 1; } } else { @@ -1870,9 +1868,12 @@ sqlRegisterBuiltinFunctions(void) FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc, FIELD_TYPE_SCALAR), FUNCTION_COLL(substr, 2, 0, 0, substrFunc), FUNCTION_COLL(substr, 3, 0, 0, substrFunc), - AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize, FIELD_TYPE_NUMBER), - AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize, FIELD_TYPE_NUMBER), - AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize, FIELD_TYPE_NUMBER), + AGGREGATE(sum, 1, 0, 0, sum_step, sumFinalize, + FIELD_TYPE_NUMBER), + AGGREGATE(total, 1, 0, 0, sum_step, totalFinalize, + FIELD_TYPE_NUMBER), + AGGREGATE(avg, 1, 0, 0, sum_step, avgFinalize, + FIELD_TYPE_NUMBER), AGGREGATE2(count, 0, 0, 0, countStep, countFinalize, SQL_FUNC_COUNT, FIELD_TYPE_INTEGER), AGGREGATE(count, 1, 0, 0, countStep, countFinalize,