From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id C5906216BA for ; Wed, 20 Feb 2019 06:57:46 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fQdAYg_-aJoX for ; Wed, 20 Feb 2019 06:57:46 -0500 (EST) 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 turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 1DFEB213AE for ; Wed, 20 Feb 2019 06:57:46 -0500 (EST) From: Nikita Pettik Subject: [tarantool-patches] [PATCH 3/4] sql: refactor sqlVdbeMsgpackGet() Date: Wed, 20 Feb 2019 14:57:39 +0300 Message-Id: <326c19ce8abd923ae9d0ed4873abcfdcf06c13d6.1550663540.git.korablev@tarantool.org> In-Reply-To: References: In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: v.shpilevoy@tarantool.org, Nikita Pettik Tarantool allows to hold in INTEGER field values in range [INT64_MAX, UINT64_MAX], which is obviously larger than common int64_t range. In this regard, if value of integer field in range [INT64_MAX, UINT64_MAX] is presented in tuple (e.g. after insertion from Lua interface), then after decoding msgpack (during processing SQL query) its value won't fit into int64_t (which in turn is basic type used to hold integers inside VDBE memory). Now if this happens, instead of raising an overflow error, value is stored as floating point number (with precise loss, obviously). Such approach is considered to be messy and we are going to raise "integer overflow" error instead. To make this happen, lets firstly refactor sqlVdbeMsgpackGet() to make it return error code to indicate that something went wrong and move length of decoded part to output parameters. Codestyle fixes are included as well. Needed for #3735 --- src/box/sql/vdbe.c | 5 +- src/box/sql/vdbeInt.h | 13 ++++- src/box/sql/vdbeaux.c | 132 ++++++++++++++++++++++++-------------------------- src/box/sql/vdbemem.c | 4 +- 4 files changed, 81 insertions(+), 73 deletions(-) diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index d9797b22b..d38f61774 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -2737,8 +2737,9 @@ case OP_Column: { if (VdbeMemDynamic(pDest)) { sqlVdbeMemSetNull(pDest); } - - sqlVdbeMsgpackGet(zData+aOffset[p2], pDest); + uint32_t unused; + vdbe_decode_msgpack_into_mem((const char *)(zData + aOffset[p2]), + pDest, &unused); /* MsgPack map, array or extension (unsupported in sql). * Wrap it in a blob verbatim. */ diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h index d003d6bb3..49f665bb0 100644 --- a/src/box/sql/vdbeInt.h +++ b/src/box/sql/vdbeInt.h @@ -547,7 +547,18 @@ int sqlVdbeCompareMsgpack(const char **key1, */ int sqlVdbeRecordCompareMsgpack(const void *key1, struct UnpackedRecord *key2); -u32 sqlVdbeMsgpackGet(const unsigned char *buf, Mem * pMem); + +/** + * Decode msgpack and save value into VDBE memory cell. + * + * @param buf Buffer to deserialize msgpack from. + * @param mem Memory cell to write value into. + * @param len[out] Length of decoded part. + * @retval Return code: < 0 in case of error. + * @retval 0 on success. + */ +int +vdbe_decode_msgpack_into_mem(const char *buf, struct Mem *mem, uint32_t *len); struct mpstream; struct region; diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index b831b52ad..ba9b96645 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -3688,79 +3688,74 @@ sqlVdbeRecordCompareMsgpack(const void *key1, return key2->default_rc; } -u32 -sqlVdbeMsgpackGet(const unsigned char *buf, /* Buffer to deserialize from */ - Mem * pMem) /* Memory cell to write value into */ +int +vdbe_decode_msgpack_into_mem(const char *buf, struct Mem *mem, uint32_t *len) { - const char *zParse = (const char *)buf; - switch (mp_typeof(*zParse)) { + const char *start_buf = buf; + switch (mp_typeof(*buf)) { case MP_ARRAY: case MP_MAP: case MP_EXT: - default:{ - pMem->flags = 0; - return 0; - } - case MP_NIL:{ - mp_decode_nil((const char **)&zParse); /* Still need to promote zParse. */ - pMem->flags = MEM_Null; - break; - } - case MP_BOOL:{ - assert((unsigned char)*zParse == 0xc2 - || (unsigned char)*zParse == 0xc3); - pMem->u.i = (unsigned char)*zParse - 0xc2; - pMem->flags = MEM_Int; - break; - } - case MP_UINT:{ - uint64_t v = mp_decode_uint(&zParse); - if (v > INT64_MAX) { - /* - * If the value exceeds i64 range, convert to double (lossy). - */ - pMem->u.r = v; - pMem->flags = MEM_Real; - } else { - pMem->u.i = v; - pMem->flags = MEM_Int; - } - break; - } - case MP_INT:{ - pMem->u.i = mp_decode_int(&zParse); - pMem->flags = MEM_Int; - break; - } - case MP_STR:{ - /* XXX u32->int */ - pMem->n = (int)mp_decode_strl((const char **)&zParse); - pMem->flags = MEM_Str | MEM_Ephem; - install_blob: - pMem->z = (char *)zParse; - zParse += pMem->n; - break; - } - case MP_BIN:{ - /* XXX u32->int */ - pMem->n = (int)mp_decode_binl((const char **)&zParse); - pMem->flags = MEM_Blob | MEM_Ephem; - goto install_blob; - } - case MP_FLOAT:{ - pMem->u.r = mp_decode_float(&zParse); - pMem->flags = - sqlIsNaN(pMem->u.r) ? MEM_Null : MEM_Real; - break; - } - case MP_DOUBLE:{ - pMem->u.r = mp_decode_double(&zParse); - pMem->flags = - sqlIsNaN(pMem->u.r) ? MEM_Null : MEM_Real; - break; + default: { + mem->flags = 0; + break; + } + case MP_NIL: { + mp_decode_nil(&buf); + mem->flags = MEM_Null; + break; + } + case MP_BOOL: { + assert((unsigned char)*buf == 0xc2 || + (unsigned char)*buf == 0xc3); + mem->u.i = (unsigned char)*buf - 0xc2; + mem->flags = MEM_Int; + break; + } + case MP_UINT: { + uint64_t v = mp_decode_uint(&buf); + if (v > INT64_MAX) { + mem->u.r = v; + mem->flags = MEM_Real; + } else { + mem->u.i = v; + mem->flags = MEM_Int; } + break; + } + case MP_INT: { + mem->u.i = mp_decode_int(&buf); + mem->flags = MEM_Int; + break; + } + case MP_STR: { + /* XXX u32->int */ + mem->n = (int) mp_decode_strl(&buf); + mem->flags = MEM_Str | MEM_Ephem; +install_blob: + mem->z = (char *)buf; + buf += mem->n; + break; + } + case MP_BIN: { + /* XXX u32->int */ + mem->n = (int) mp_decode_binl(&buf); + mem->flags = MEM_Blob | MEM_Ephem; + goto install_blob; } - return (u32) (zParse - (const char *)buf); + case MP_FLOAT: { + mem->u.r = mp_decode_float(&buf); + mem->flags = sqlIsNaN(mem->u.r) ? MEM_Null : MEM_Real; + break; + } + case MP_DOUBLE: { + mem->u.r = mp_decode_double(&buf); + mem->flags = sqlIsNaN(mem->u.r) ? MEM_Null : MEM_Real; + break; + } + } + *len = (uint32_t)(buf - start_buf); + return 0; } void @@ -3778,7 +3773,8 @@ sqlVdbeRecordUnpackMsgpack(struct key_def *key_def, /* Information about the rec while (n--) { pMem->szMalloc = 0; pMem->z = 0; - u32 sz = sqlVdbeMsgpackGet((u8 *) zParse, pMem); + uint32_t sz = 0; + vdbe_decode_msgpack_into_mem(zParse, pMem, &sz); if (sz == 0) { /* MsgPack array, map or ext. Treat as blob. */ pMem->z = (char *)zParse; diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c index a816936f0..19802f746 100644 --- a/src/box/sql/vdbemem.c +++ b/src/box/sql/vdbemem.c @@ -1634,8 +1634,8 @@ sql_stat4_column(struct sql *db, const char *record, uint32_t col_num, return -1; } } - sqlVdbeMsgpackGet((const unsigned char *) a, mem); - return 0; + uint32_t unused; + return vdbe_decode_msgpack_into_mem(a, mem, &unused); } /* -- 2.15.1