From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> Cc: tarantool-patches@dev.tarantool.org Subject: Re: [Tarantool-patches] [PATCH v1 05/21] sql: refactor PRINTF() function Date: Mon, 25 Oct 2021 11:33:47 +0300 [thread overview] Message-ID: <20211025083347.GE36295@tarantool.org> (raw) In-Reply-To: <db97de15-6c9b-dc2b-ad14-14b5b42b3976@tarantool.org> Thank you for the review! My answer, diff and new patch below. I also simplified the code a bit. On Fri, Oct 15, 2021 at 12:44:08AM +0200, Vladislav Shpilevoy wrote: > Thanks for the patch! > > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > > index 863dbf1c4..f5040fb6e 100644 > > --- a/src/box/sql/func.c > > +++ b/src/box/sql/func.c > > @@ -846,6 +846,40 @@ func_octet_length(struct sql_context *ctx, int argc, struct Mem *argv) > > mem_set_uint(ctx->pOut, arg->n); > > } > > > > +/** Implementation of the PRINTF() function. */ > > +static void > > +func_printf(struct sql_context *ctx, int argc, struct Mem *argv) > > +{ > > + if (argc < 1 || mem_is_null(&argv[0])) > > + return; > > + if (argc == 1 || !mem_is_str(&argv[0])) { > > + struct Mem *mem = ctx->pOut; > > + if (mem_copy(mem, &argv[0]) != 0 || mem_to_str(mem) != 0) > > + ctx->is_aborted = true; > > + return; > > + } > > + struct PrintfArguments pargs; > > + struct StrAccum acc; > > + char *format = argv[0].z; > > + struct sql *db = sql_get(); > > + > > + pargs.nArg = argc - 1; > > + pargs.nUsed = 0; > > + pargs.apArg = sqlDbMallocRawNN(db, (argc - 1) * sizeof(*pargs.apArg)); > > + if (pargs.apArg == NULL) { > > + ctx->is_aborted = true; > > + return; > > + } > > + for (int i = 1; i < argc; ++i) > > + pargs.apArg[i - 1] = &argv[i]; > > + sqlStrAccumInit(&acc, db, 0, 0, db->aLimit[SQL_LIMIT_LENGTH]); > > + acc.printfFlags = SQL_PRINTF_SQLFUNC; > > + sqlXPrintf(&acc, format, &pargs); > > + sqlDbFree(db, pargs.apArg); > > + if (mem_copy_str(ctx->pOut, sqlStrAccumFinish(&acc), acc.nChar) != 0) > > It leaks now, because sqlStrAccumFinish is not destroyed. Previously it > was 'moved' into the mem via SQL_DYNAMIC. But now you copy it and the > original is not freed. Thanks, fixed. Diff: diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 48d248568..de2bbb20e 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -861,23 +861,14 @@ func_printf(struct sql_context *ctx, int argc, struct Mem *argv) struct PrintfArguments pargs; struct StrAccum acc; char *format = argv[0].z; - struct sql *db = sql_get(); - pargs.nArg = argc - 1; pargs.nUsed = 0; - pargs.apArg = sqlDbMallocRawNN(db, (argc - 1) * sizeof(*pargs.apArg)); - if (pargs.apArg == NULL) { - ctx->is_aborted = true; - return; - } - for (int i = 1; i < argc; ++i) - pargs.apArg[i - 1] = &argv[i]; + pargs.apArg = argv + 1; + struct sql *db = sql_get(); sqlStrAccumInit(&acc, db, 0, 0, db->aLimit[SQL_LIMIT_LENGTH]); acc.printfFlags = SQL_PRINTF_SQLFUNC; sqlXPrintf(&acc, format, &pargs); - sqlDbFree(db, pargs.apArg); - if (mem_copy_str(ctx->pOut, sqlStrAccumFinish(&acc), acc.nChar) != 0) - ctx->is_aborted = true; + mem_set_str_allocated(ctx->pOut, sqlStrAccumFinish(&acc), acc.nChar); } static const unsigned char * diff --git a/src/box/sql/printf.c b/src/box/sql/printf.c index b4ab0d0f9..5b61646e3 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 mem_get_int_unsafe(p->apArg[p->nUsed++]); + return mem_get_int_unsafe(&p->apArg[p->nUsed++]); } static double @@ -152,7 +152,7 @@ getDoubleArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0.0; - return mem_get_double_unsafe(p->apArg[p->nUsed++]); + return mem_get_double_unsafe(&p->apArg[p->nUsed++]); } static char * @@ -160,7 +160,7 @@ getTextArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0; - struct Mem *mem = p->apArg[p->nUsed++]; + struct Mem *mem = &p->apArg[p->nUsed++]; return (char *)mem_as_str0(mem); } diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index cfdf71f1f..9361775b1 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -2511,7 +2511,8 @@ int sqlIsNaN(double); struct PrintfArguments { int nArg; /* Total number of arguments */ int nUsed; /* Number of arguments used so far */ - sql_value **apArg; /* The argument values */ + /** The argument values. */ + struct Mem *apArg; }; void sqlVXPrintf(StrAccum *, const char *, va_list); New patch: commit 8d7ab164e7c69faf1a6f74b75deb769da0a0ee27 Author: Mergen Imeev <imeevma@gmail.com> Date: Tue Oct 5 18:25:55 2021 +0300 sql: refactor PRINTF() function Part of #4145 diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 8eb3400bf..de2bbb20e 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -846,6 +846,31 @@ func_octet_length(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_uint(ctx->pOut, arg->n); } +/** Implementation of the PRINTF() function. */ +static void +func_printf(struct sql_context *ctx, int argc, struct Mem *argv) +{ + if (argc < 1 || mem_is_null(&argv[0])) + return; + if (argc == 1 || !mem_is_str(&argv[0])) { + struct Mem *mem = ctx->pOut; + if (mem_copy(mem, &argv[0]) != 0 || mem_to_str(mem) != 0) + ctx->is_aborted = true; + return; + } + struct PrintfArguments pargs; + struct StrAccum acc; + char *format = argv[0].z; + pargs.nArg = argc - 1; + pargs.nUsed = 0; + pargs.apArg = argv + 1; + struct sql *db = sql_get(); + sqlStrAccumInit(&acc, db, 0, 0, db->aLimit[SQL_LIMIT_LENGTH]); + acc.printfFlags = SQL_PRINTF_SQLFUNC; + sqlXPrintf(&acc, format, &pargs); + mem_set_str_allocated(ctx->pOut, sqlStrAccumFinish(&acc), acc.nChar); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -940,40 +965,6 @@ typeofFunc(struct sql_context *context, int argc, struct Mem *argv) sql_result_text(context, z, -1, SQL_STATIC); } -/* - * Implementation of the printf() function. - */ -static void -printfFunc(struct sql_context *context, int argc, struct Mem *argv) -{ - PrintfArguments x; - StrAccum str; - const char *zFormat; - int n; - sql *db = sql_context_db_handle(context); - - if (argc >= 1 && (zFormat = mem_as_str0(&argv[0])) != NULL) { - x.nArg = argc - 1; - x.nUsed = 0; - x.apArg = sqlDbMallocRawNN(sql_get(), - (argc - 1) * sizeof(*x.apArg)); - if (x.apArg == NULL) { - context->is_aborted = true; - return; - } - for (int i = 1; i < argc; ++i) - x.apArg[i - 1] = &argv[i]; - sqlStrAccumInit(&str, db, 0, 0, - db->aLimit[SQL_LIMIT_LENGTH]); - str.printfFlags = SQL_PRINTF_SQLFUNC; - sqlXPrintf(&str, zFormat, &x); - sqlDbFree(sql_get(), x.apArg); - n = str.nChar; - sql_result_text(context, sqlStrAccumFinish(&str), n, - SQL_DYNAMIC); - } -} - /* * Implementation of the round() function */ @@ -1916,8 +1907,7 @@ static struct sql_func_definition definitions[] = { FIELD_TYPE_INTEGER, func_position_characters, NULL}, {"POSITION", 2, {FIELD_TYPE_VARBINARY, FIELD_TYPE_VARBINARY}, FIELD_TYPE_INTEGER, func_position_octets, NULL}, - {"PRINTF", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, printfFunc, - NULL}, + {"PRINTF", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, func_printf, NULL}, {"QUOTE", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, quoteFunc, NULL}, {"RANDOM", 0, {}, FIELD_TYPE_INTEGER, randomFunc, NULL}, {"RANDOMBLOB", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_VARBINARY, diff --git a/src/box/sql/printf.c b/src/box/sql/printf.c index b4ab0d0f9..5b61646e3 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 mem_get_int_unsafe(p->apArg[p->nUsed++]); + return mem_get_int_unsafe(&p->apArg[p->nUsed++]); } static double @@ -152,7 +152,7 @@ getDoubleArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0.0; - return mem_get_double_unsafe(p->apArg[p->nUsed++]); + return mem_get_double_unsafe(&p->apArg[p->nUsed++]); } static char * @@ -160,7 +160,7 @@ getTextArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0; - struct Mem *mem = p->apArg[p->nUsed++]; + struct Mem *mem = &p->apArg[p->nUsed++]; return (char *)mem_as_str0(mem); } diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index cfdf71f1f..9361775b1 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -2511,7 +2511,8 @@ int sqlIsNaN(double); struct PrintfArguments { int nArg; /* Total number of arguments */ int nUsed; /* Number of arguments used so far */ - sql_value **apArg; /* The argument values */ + /** The argument values. */ + struct Mem *apArg; }; void sqlVXPrintf(StrAccum *, const char *, va_list);
next prev parent reply other threads:[~2021-10-25 8:33 UTC|newest] Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-10-08 17:31 [Tarantool-patches] [PATCH v1 00/21] Refactor non-standard and non-aggragate functions Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 01/21] sql: refactor CHAR() function Mergen Imeev via Tarantool-patches 2021-10-14 22:42 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:02 ` Mergen Imeev via Tarantool-patches 2021-10-29 23:42 ` Vladislav Shpilevoy via Tarantool-patches 2021-11-02 11:35 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 02/21] sql: refactor GREATEST() and LEAST() functions Mergen Imeev via Tarantool-patches 2021-10-14 22:42 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:17 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 03/21] sql: refactor HEX() function Mergen Imeev via Tarantool-patches 2021-10-14 22:43 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:19 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 04/21] sql: refactor LENGTH() function Mergen Imeev via Tarantool-patches 2021-10-14 22:43 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:30 ` Mergen Imeev via Tarantool-patches 2021-10-29 23:42 ` Vladislav Shpilevoy via Tarantool-patches 2021-11-02 11:39 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 05/21] sql: refactor PRINTF() function Mergen Imeev via Tarantool-patches 2021-10-14 22:44 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:33 ` Mergen Imeev via Tarantool-patches [this message] 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 06/21] sql: refactor RANDOM() function Mergen Imeev via Tarantool-patches 2021-10-25 8:35 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 07/21] sql: rework RANDOMBLOB() function Mergen Imeev via Tarantool-patches 2021-10-25 8:36 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 08/21] sql: refactor ZEROBLOB() function Mergen Imeev via Tarantool-patches 2021-10-25 8:37 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 09/21] sql: refactor TYPEOF() function Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 10/21] sql: refactor ROUND() function Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 11/21] sql: refactor ROW_COUNT() function Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 12/21] sql: rework UUID() function Mergen Imeev via Tarantool-patches 2021-10-25 8:38 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 13/21] sql: refactor VERSION() function Mergen Imeev via Tarantool-patches 2021-10-08 17:31 ` [Tarantool-patches] [PATCH v1 14/21] sql: refactor UNICODE() function Mergen Imeev via Tarantool-patches 2021-10-14 22:44 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:40 ` Mergen Imeev via Tarantool-patches 2021-11-02 11:42 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 15/21] sql: refactor of SOUNDEX() function Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 16/21] sql: refactor REPLACE() function Mergen Imeev via Tarantool-patches 2021-10-14 22:45 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:45 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 17/21] sql: refactor QUOTE() function Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 18/21] sql: remove unused code Mergen Imeev via Tarantool-patches 2021-10-25 8:51 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 19/21] sql: remove MEM_Dyn flag Mergen Imeev via Tarantool-patches 2021-10-14 22:46 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 8:54 ` Mergen Imeev via Tarantool-patches 2021-10-29 23:43 ` Vladislav Shpilevoy via Tarantool-patches 2021-11-02 11:43 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 20/21] sql: remove MEM_Term flag Mergen Imeev via Tarantool-patches 2021-10-14 22:47 ` Vladislav Shpilevoy via Tarantool-patches 2021-10-25 9:57 ` Mergen Imeev via Tarantool-patches 2021-10-08 17:32 ` [Tarantool-patches] [PATCH v1 21/21] sql: make arguments to be const Mergen Imeev via Tarantool-patches 2021-11-02 22:15 ` [Tarantool-patches] [PATCH v1 00/21] Refactor non-standard and non-aggragate functions Vladislav Shpilevoy via Tarantool-patches 2021-11-11 10:48 Mergen Imeev via Tarantool-patches 2021-11-11 10:48 ` [Tarantool-patches] [PATCH v1 05/21] sql: refactor PRINTF() function Mergen Imeev via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20211025083347.GE36295@tarantool.org \ --to=tarantool-patches@dev.tarantool.org \ --cc=imeevma@tarantool.org \ --cc=v.shpilevoy@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v1 05/21] sql: refactor PRINTF() function' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox