Skip to content

Commit b45100f

Browse files
fix undefined float-to-integer cast at the numeric limit boundary (#1162)
1 parent 3167f77 commit b45100f

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎include/behaviortree_cpp/utils/convert_impl.hpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,15 @@ inline void checkTruncation(const From& from)
114114
// Handle floating point to integer
115115
else if constexpr(std::is_floating_point_v<From> && std::is_integral_v<To>)
116116
{
117-
if(from > static_cast<From>(std::numeric_limits<To>::max()) ||
118-
from < static_cast<From>(std::numeric_limits<To>::lowest()) ||
117+
// static_cast<From>(To::max) rounds up to an out-of-range value when To has
118+
// more significant bits than From's mantissa (e.g. double -> int64_t). In
119+
// that case a plain `>` lets the rounded boundary through and the cast to To
120+
// below overflows, so use a half-open upper bound to reject it first.
121+
constexpr bool max_rounds_up =
122+
std::numeric_limits<To>::digits > std::numeric_limits<From>::digits;
123+
const From max_limit = static_cast<From>(std::numeric_limits<To>::max());
124+
const bool over = max_rounds_up ? (from >= max_limit) : (from > max_limit);
125+
if(over || from < static_cast<From>(std::numeric_limits<To>::lowest()) ||
119126
from != std::nearbyint(from))
120127
{
121128
throw std::runtime_error("Invalid floating point to integer conversion");

‎include/behaviortree_cpp/utils/safe_any.hpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,16 @@ inline bool ValidCast(const SRC& val)
259259
// Handle conversion to integral
260260
else if constexpr(std::is_integral_v<TO>)
261261
{
262-
if(val > static_cast<SRC>(std::numeric_limits<TO>::max()) ||
263-
val < static_cast<SRC>(std::numeric_limits<TO>::lowest()))
262+
// static_cast<SRC>(TO::max) rounds up to an out-of-range value when TO has
263+
// more significant bits than a floating SRC's mantissa (e.g. double ->
264+
// int64_t). A plain `>` would let that rounded boundary through and the
265+
// cast to TO below overflows, so use a half-open upper bound in that case.
266+
constexpr bool max_rounds_up =
267+
std::is_floating_point_v<SRC> &&
268+
std::numeric_limits<TO>::digits > std::numeric_limits<SRC>::digits;
269+
const SRC max_limit = static_cast<SRC>(std::numeric_limits<TO>::max());
270+
const bool over = max_rounds_up ? (val >= max_limit) : (val > max_limit);
271+
if(over || val < static_cast<SRC>(std::numeric_limits<TO>::lowest()))
264272
{
265273
return false;
266274
}

‎tests/gtest_basic_types.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,31 @@ TEST(BasicTypes, ConvertFromString_Double)
123123
ASSERT_THROW((void)convertFromString<double>("not_a_number"), RuntimeError);
124124
}
125125

126+
TEST(BasicTypes, DoubleToIntBoundary)
127+
{
128+
// A double that lands exactly on 2^63 must be rejected before the cast, not
129+
// static_cast to int64_t (which is out of range and undefined behavior).
130+
// 2^63 = 9223372036854775808.0; INT64_MAX is 9223372036854775807.
131+
const double two_pow_63 = 9223372036854775808.0;
132+
EXPECT_ANY_THROW((void)BT::Any(two_pow_63).cast<int64_t>());
133+
134+
// 2^64 lands one past UINT64_MAX and must likewise be rejected.
135+
const double two_pow_64 = 18446744073709551616.0;
136+
EXPECT_ANY_THROW((void)BT::Any(two_pow_64).cast<uint64_t>());
137+
138+
// In-range integral doubles still convert cleanly.
139+
const double in_range = 4611686018427387904.0; // 2^62
140+
EXPECT_EQ(BT::Any(in_range).cast<int64_t>(), int64_t(4611686018427387904LL));
141+
EXPECT_EQ(BT::Any(1000.0).cast<int64_t>(), int64_t(1000));
142+
143+
// Same boundary reached through Blackboard::set, which routes the numeric
144+
// safe-cast check (isCastingSafe/ValidCast) instead of Any::convert.
145+
auto bb = BT::Blackboard::create();
146+
bb->set("k", int64_t(1)); // lock the entry to int64_t
147+
EXPECT_ANY_THROW(bb->set("k", two_pow_63));
148+
EXPECT_NO_THROW(bb->set("k", in_range));
149+
}
150+
126151
TEST(BasicTypes, ConvertFromString_Bool)
127152
{
128153
ASSERT_TRUE(convertFromString<bool>("true"));

0 commit comments

Comments
 (0)