[Tarantool-patches] [PATCH v1 05/21] sql: refactor PRINTF() function

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Fri Oct 15 01:44:08 MSK 2021


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.


More information about the Tarantool-patches mailing list