[Tarantool-patches] [PATCH v2 1/4] sql: truncate values in type mismatch error

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Thu Jul 8 01:09:12 MSK 2021


Hi! Thanks for the patch!

See 2 comments below.

> diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
> index 6f3bf52e5..630f1a135 100644
> --- a/src/box/sql/mem.c
> +++ b/src/box/sql/mem.c
> @@ -71,26 +72,35 @@ mem_is_field_compatible(const struct Mem *mem, enum field_type type)
>  const char *
>  mem_str(const struct Mem *mem)
>  {
> -	char buf[BUF_SIZE];
> +	char buf[STR_VALUE_MAX_LEN + 1];
>  	switch (mem->type) {
>  	case MEM_TYPE_NULL:
>  		return "NULL";
>  	case MEM_TYPE_STR:
> -		if ((mem->flags & MEM_Term) != 0)
> -			return mem->z;
> +		if (mem->n > STR_VALUE_MAX_LEN) {
> +			memcpy(buf, mem->z, STR_VALUE_MAX_LEN);
> +			buf[STR_VALUE_MAX_LEN] = '\0';
> +			return tt_sprintf("%s...", buf);

1. Can be done simpler using %.*s in tt_sprintf() with the length
limited by STR_VALUE_MAX_LEN.

> +		}
>  		return tt_cstr(mem->z, mem->n);
>  	case MEM_TYPE_INT:
>  		return tt_sprintf("%lld", mem->u.i);
>  	case MEM_TYPE_UINT:
>  		return tt_sprintf("%llu", mem->u.u);
>  	case MEM_TYPE_DOUBLE:
> -		sql_snprintf(BUF_SIZE, &buf[0], "%!.15g", mem->u.r);
> +		sql_snprintf(STR_VALUE_MAX_LEN + 1, buf, "%!.15g", mem->u.r);
>  		return tt_sprintf("%s", buf);
>  	case MEM_TYPE_BIN:
>  		return "varbinary";
>  	case MEM_TYPE_MAP:
> -	case MEM_TYPE_ARRAY:
> -		return mp_str(mem->z);
> +	case MEM_TYPE_ARRAY: {
> +		const char *str = mp_str(mem->z);
> +		if (strlen(str) <= STR_VALUE_MAX_LEN)
> +			return str;
> +		memcpy(buf, str, STR_VALUE_MAX_LEN);
> +		buf[STR_VALUE_MAX_LEN] = '\0';
> +		return tt_sprintf("%s...", buf);

2. Ditto.


More information about the Tarantool-patches mailing list