From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id BF9B36EC5E; Fri, 9 Apr 2021 22:46:37 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BF9B36EC5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1617997597; bh=oPVFTo3RHurVgQAP+pLv24Pu2Gq9dBxBKdN/Ki0emkk=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=cIqo5b8VSjWCKK4a1g2G/TrXgGcVIWE+PHhexKavncmWQK1vP6ohvrwadG2xdxTUk Zc12W/F+MxJ4IgVV9CGsxG8oPs3BMcIzSf2pZf5WCqvnFm6tm5+vKgqVMEmcixerJO LxGA63smIon+BCVds3ORCgPJJyQuoaWMry7WI8Zg= 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 dev.tarantool.org (Postfix) with ESMTPS id 18DF96EC5E for ; Fri, 9 Apr 2021 22:45:40 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 18DF96EC5E Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1lUx4V-0006dM-A4; Fri, 09 Apr 2021 22:45:39 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 9 Apr 2021 22:45:39 +0300 Message-Id: <2a22e2c47ad16b38bb7dcfb6b35a6a61b8b94e5e.1617984948.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480D608FE24BC85426BB1B55F651FED8C70182A05F538085040D71A42B7D9D0841A5A913BB4F212CEBC957229D26FEF6650D2C7E797372F6C31 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE745229D52CF30560CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006377A06FA5CCF78F9BC8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2EA2A0CE7B565850F46C5574DD335093A618001F51B5FD3F9D2E47CDBA5A96583C09775C1D3CA48CF4C82C86BFC697D19117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE73A9324DDA23307489FA2833FD35BB23DF004C906525384302BEBFE083D3B9BA73A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E735C6EABA9B74D0DA47B5C8C57E37DE458BEDA766A37F9254B7 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CD0035DD76F8A8A4F6547D786F4F962E56EA5973AE4178DDE9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34987A0E8D235BB53460B9E2184DD82225CA1336B6CBB77AD2C088313095983613850F2FD0ED568EDF1D7E09C32AA3244CDC6332C22987946F12138F3766B023957101BF96129E4011FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojyO2lHpuZu4TBGMKNCGxp2A== X-Mailru-Sender: 689FA8AB762F73936BC43F508A063822263CCCA6F1DF518934D10E6E3834C3CB83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v5 28/52] sql: introduce mem_copy_str() and mem_copy_str0() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This patch introduces mem_copy_str() and mem_copy_str0() functions. These functions copy given string to newly allocated by MEM memory. String given to mem_copy_str0() should be NULL-terminated. This is not necessary for mem_copy_str(). Part of #5818 --- src/box/sql/mem.c | 40 +++++++++++++++++++++++++++++++++------- src/box/sql/mem.h | 11 +++++++++++ src/box/sql/vdbeapi.c | 17 ++++++++++++++--- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 87e1bcfd1..045c44e8f 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -394,6 +394,34 @@ mem_set_str0_allocated(struct Mem *mem, char *value) mem->flags |= MEM_Term; } +int +mem_copy_str(struct Mem *mem, const char *value, uint32_t len) +{ + if ((mem->flags & (MEM_Agg | MEM_Frame)) != 0) + mem_clear(mem); + bool is_own_value = (mem->flags & (MEM_Str | MEM_Blob)) != 0 && + mem->z == value; + if (sqlVdbeMemGrow(mem, len, is_own_value) != 0) + return -1; + if (!is_own_value) + memcpy(mem->z, value, len); + mem->n = len; + mem->flags = MEM_Str; + mem->field_type = FIELD_TYPE_STRING; + return 0; +} + +int +mem_copy_str0(struct Mem *mem, const char *value) +{ + uint32_t len = strlen(value); + if (mem_copy_str(mem, value, len + 1) != 0) + return -1; + mem->n = len; + mem->flags |= MEM_Term; + return 0; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -1618,8 +1646,8 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type) assert(MEM_Str == (MEM_Blob >> 3)); if ((pMem->flags & MEM_Bool) != 0) { const char *str_bool = SQL_TOKEN_BOOLEAN(pMem->u.b); - sqlVdbeMemSetStr(pMem, str_bool, strlen(str_bool), 1, - SQL_TRANSIENT); + if (mem_copy_str0(pMem, str_bool) != 0) + return -1; return 0; } pMem->flags |= (pMem->flags & MEM_Blob) >> 3; @@ -3080,9 +3108,8 @@ port_lua_get_vdbemem(struct port *base, uint32_t *size) val[i].u.i = field.ival; break; case MP_STR: - if (sqlVdbeMemSetStr(&val[i], field.sval.data, - field.sval.len, 1, - SQL_TRANSIENT) != 0) + if (mem_copy_str(&val[i], field.sval.data, + field.sval.len) != 0) goto error; break; case MP_NIL: @@ -3157,8 +3184,7 @@ port_c_get_vdbemem(struct port *base, uint32_t *size) break; case MP_STR: str = mp_decode_str(&data, &len); - if (sqlVdbeMemSetStr(&val[i], str, len, - 1, SQL_TRANSIENT) != 0) + if (mem_copy_str(&val[i], str, len) != 0) goto error; break; case MP_NIL: diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index ecaa1edb8..d03651192 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -235,6 +235,17 @@ mem_set_str0_dynamic(struct Mem *mem, char *value); void mem_set_str0_allocated(struct Mem *mem, char *value); +/** Copy string to a newly allocated memory. The MEM type becomes STRING. */ +int +mem_copy_str(struct Mem *mem, const char *value, uint32_t len); + +/** + * Copy NULL-terminated string to a newly allocated memory. The MEM type becomes + * STRING. + */ +int +mem_copy_str0(struct Mem *mem, const char *value); + /** * Copy content of MEM from one MEM to another. In case source MEM contains * string or binary and allocation type is not STATIC, this value is copied to diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index e0903e3b0..ee095f36e 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -146,7 +146,12 @@ setResultStrOrError(sql_context * pCtx, /* Function context */ mem_set_str_dynamic(pCtx->pOut, (char *)z, n); return; } - if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 1, xDel) != 0) + if (n < 0) { + if (mem_copy_str0(pCtx->pOut, z) != 0) + pCtx->is_aborted = true; + return; + } + if (mem_copy_str(pCtx->pOut, z, n) != 0) pCtx->is_aborted = true; } @@ -798,8 +803,14 @@ bindText(sql_stmt * pStmt, /* The statement to bind against */ mem_set_str0_dynamic(pVar, (char *)zData); else mem_set_str_dynamic(pVar, (char *)zData, nData); - } else if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0) { - return -1; + } else { + if (nData < 0) { + if (mem_copy_str0(pVar, zData) != 0) + return -1; + } else { + if (mem_copy_str(pVar, zData, nData) != 0) + return -1; + } } return sql_bind_type(p, i, "text"); } -- 2.25.1