From efccd82fb555d86b57ca5424861808ab38dc48b4 Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Mon, 6 May 2024 12:17:14 +0300 Subject: [PATCH] cql3: castas_fcts: workaround boost bug casting multiprecision integers to floats In [1] a bug casting large multiprecision integers to floats is documented. Until the fix propagates, work around it by detecting the conditions that trigger the bug and returning the expected result. Fixes https://github.com/scylladb/scylladb/issues/18508 [1] https://github.com/boostorg/multiprecision/issues/553 --- cql3/functions/castas_fcts.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cql3/functions/castas_fcts.cc b/cql3/functions/castas_fcts.cc index eddfd44ddabe..74e824b47ed4 100644 --- a/cql3/functions/castas_fcts.cc +++ b/cql3/functions/castas_fcts.cc @@ -69,6 +69,16 @@ using bytes_opt = std::optional; template static data_value castas_fctn_simple(data_value from) { auto val_from = value_cast(from); + // Workaround for https://github.com/boostorg/multiprecision/issues/553 (the additional bug discovered post-closing) + if constexpr (std::is_floating_point_v && std::is_same_v) { + static auto min = utils::multiprecision_int(std::numeric_limits::lowest()); + static auto max = utils::multiprecision_int(std::numeric_limits::max()); + if (val_from < min) { + return -std::numeric_limits::infinity(); + } else if (val_from > max) { + return std::numeric_limits::infinity(); + } + } return static_cast(val_from); }