[Tarantool-patches] [PATCH v5 09/52] sql: introduce mem_str()

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Sun Apr 11 20:44:53 MSK 2021


I appreciate the work you did here!

> diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c
> index 79c3d60e0..b7e148422 100644
> --- a/src/box/sql/vdbeaux.c
> +++ b/src/box/sql/vdbeaux.c
> @@ -1108,21 +1108,10 @@ displayP4(Op * pOp, char *zTemp, int nTemp)
>  			break;
>  		}
>  	case P4_MEM:{
> -			Mem *pMem = pOp->p4.pMem;
> -			if (pMem->flags & MEM_Str) {
> -				zP4 = pMem->z;
> -			} else if (pMem->flags & MEM_Int) {
> -				sqlXPrintf(&x, "%lld", pMem->u.i);
> -			} else if (pMem->flags & MEM_UInt) {
> -				sqlXPrintf(&x, "%llu", pMem->u.u);
> -			} else if (pMem->flags & MEM_Real) {
> -				sqlXPrintf(&x, "%.16g", pMem->u.r);
> -			} else if (pMem->flags & MEM_Null) {
> -				zP4 = "NULL";
> -			} else {
> -				assert(pMem->flags & MEM_Blob);
> -				zP4 = "(binary string)";
> -			}
> +			const char *value = mem_str(pOp->p4.pMem);
> +			uint32_t size = MIN((int)strlen(value), nTemp - 1);
> +			memcpy(zP4, value, size);
> +			zP4[size] = '\0';

'x' stays in an invalid state now, because its counter nChar is
not updated. I would propose to use sqlStrAccumAppend/sqlStrAccumAppendAll
instead of memcpy and manual 0 termination.

The same below in sqlVdbeExpandSql().

>  			break;
>  		}
>  	case P4_INTARRAY:{
> diff --git a/src/box/sql/vdbetrace.c b/src/box/sql/vdbetrace.c
> index e84bb3192..4ca56865d 100644
> --- a/src/box/sql/vdbetrace.c
> +++ b/src/box/sql/vdbetrace.c
> @@ -147,33 +145,11 @@ sqlVdbeExpandSql(Vdbe * p,	/* The prepared statement being evaluated */
>  			zRawSql += nToken;
>  			nextIndex = idx + 1;
>  			assert(idx > 0 && idx <= p->nVar);
> -			pVar = &p->aVar[idx - 1];
> -			if (pVar->flags & MEM_Null) {
> -				sqlStrAccumAppend(&out, "NULL", 4);
> -			} else if (pVar->flags & MEM_Int) {
> -				sqlXPrintf(&out, "%lld", pVar->u.i);
> -			} else if (pVar->flags & MEM_UInt) {
> -				sqlXPrintf(&out, "%llu", pVar->u.u);
> -			} else if (pVar->flags & MEM_Real) {
> -				sqlXPrintf(&out, "%!.15g", pVar->u.r);
> -			} else if (pVar->flags & MEM_Str) {
> -				int nOut;	/* Number of bytes of the string text to include in output */
> -				nOut = pVar->n;
> -				sqlXPrintf(&out, "'%.*q'", nOut, pVar->z);
> -			} else if (pVar->flags & MEM_Zero) {
> -				sqlXPrintf(&out, "zeroblob(%d)",
> -					       pVar->u.nZero);
> -			} else {
> -				int nOut;	/* Number of bytes of the blob to include in output */
> -				assert(pVar->flags & MEM_Blob);
> -				sqlStrAccumAppend(&out, "x'", 2);
> -				nOut = pVar->n;
> -				for (i = 0; i < nOut; i++) {
> -					sqlXPrintf(&out, "%02x",
> -						       pVar->z[i] & 0xff);
> -				}
> -				sqlStrAccumAppend(&out, "'", 1);
> -			}
> +			const char *value = mem_str(&p->aVar[idx - 1]);
> +			uint32_t len = strlen(value);
> +			uint32_t size = MIN(len, sizeof(zBase) - 1);
> +			memcpy(zBase, value, size);
> +			zBase[size] = '\0';
>  		}
>  	}
>  	if (out.accError)
> 


More information about the Tarantool-patches mailing list