From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> To: tarantool-patches@dev.tarantool.org, tsafin@tarantool.org, alyapunov@tarantool.org Subject: [Tarantool-patches] [PATCH 11/11] sql: fix mem_apply_type double type truncation Date: Fri, 5 Jun 2020 01:43:08 +0200 [thread overview] Message-ID: <2f9b11e05fd155605435cd4bf32ffdf10a5f91cf.1591313754.git.v.shpilevoy@tarantool.org> (raw) In-Reply-To: <cover.1591313754.git.v.shpilevoy@tarantool.org> mem_apply_type(), when tried to cast a double value to an integer, used the expressions: int64_t i = (int64_t) d; uint64_t u = (uint64_t) d; To obtain integer versions of the double value, cast them back to double, and see if they are equal. Assuming that if they are, the double can be safely cast to one of them. But this is undefined behaviour. Double can't be cast to int64_t, if it is > INT64_MAX or < INT64_MIN. And can't be cast to uint64_t, if it is < 0 or > UINT64_MAX. The patch adds explicit checks for these borders before doing the cast. Part of #4609 --- src/box/sql/vdbe.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 5bc106b5d..6b769805c 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -324,12 +324,18 @@ mem_apply_type(struct Mem *record, enum field_type type) return 0; if ((record->flags & MEM_Real) == MEM_Real) { double d = record->u.r; - int64_t i = (int64_t) d; - uint64_t u = (uint64_t) d; - if (i == d) - mem_set_int(record, i, i <= -1); - else if (u == d) - mem_set_u64(record, u); + if (d >= 0) { + if (double_compare_uint64(d, UINT64_MAX, + 1) > 0) + return 0; + if ((double)(uint64_t)d == d) + mem_set_u64(record, (uint64_t)d); + } else { + if (double_compare_nint64(d, INT64_MIN, 1) < 0) + return 0; + if ((double)(int64_t)d == d) + mem_set_int(record, (int64_t)d, true); + } return 0; } if ((record->flags & MEM_Str) != 0) { -- 2.21.1 (Apple Git-122.3)
next prev parent reply other threads:[~2020-06-04 23:43 UTC|newest] Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-06-04 23:43 [Tarantool-patches] [PATCH 00/11] Enable miscelaneous sanitations Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 01/11] cmake: enable misc types of UB detection in clang Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 10/11] sql: fix usage of not initialized index_stat Vladislav Shpilevoy 2020-06-04 23:43 ` Vladislav Shpilevoy [this message] 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 02/11] util: introduce double_compare_nint64() Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 03/11] test: avoid usleep() usage for error injections Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 04/11] vinyl: fix 0 division in case of canceled dump Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 05/11] xrow: don't cast double to float unconditionally Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 06/11] swim: fix zero division Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 07/11] test: fix signed integer overflow in vclock test Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 08/11] digest: eliminate UBs from guava() Vladislav Shpilevoy 2020-06-04 23:43 ` [Tarantool-patches] [PATCH 09/11] salad: fix UB pointer arithmetics in bps_tree Vladislav Shpilevoy 2020-06-05 22:09 ` [Tarantool-patches] [PATCH 00/11] Enable miscelaneous sanitations Timur Safin 2020-06-09 8:19 ` Cyrill Gorcunov 2020-06-09 8:28 ` Kirill Yukhin
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=2f9b11e05fd155605435cd4bf32ffdf10a5f91cf.1591313754.git.v.shpilevoy@tarantool.org \ --to=v.shpilevoy@tarantool.org \ --cc=alyapunov@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --cc=tsafin@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH 11/11] sql: fix mem_apply_type double type truncation' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox