Skip to content

Commit

Permalink
cql3: castas_fcts: workaround boost bug casting multiprecision intege…
Browse files Browse the repository at this point in the history
…rs 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 scylladb#18508

[1] boostorg/multiprecision#553
  • Loading branch information
avikivity committed May 6, 2024
1 parent 5ca9a46 commit efccd82
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cql3/functions/castas_fcts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ using bytes_opt = std::optional<bytes>;
template<typename ToType, typename FromType>
static data_value castas_fctn_simple(data_value from) {
auto val_from = value_cast<FromType>(from);
// Workaround for https://github.com/boostorg/multiprecision/issues/553 (the additional bug discovered post-closing)
if constexpr (std::is_floating_point_v<ToType> && std::is_same_v<FromType, utils::multiprecision_int>) {
static auto min = utils::multiprecision_int(std::numeric_limits<ToType>::lowest());
static auto max = utils::multiprecision_int(std::numeric_limits<ToType>::max());
if (val_from < min) {
return -std::numeric_limits<ToType>::infinity();
} else if (val_from > max) {
return std::numeric_limits<ToType>::infinity();
}
}
return static_cast<ToType>(val_from);
}

Expand Down

0 comments on commit efccd82

Please sign in to comment.