Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions include/behaviortree_cpp/utils/safe_any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,10 @@ inline nonstd::expected<T, std::string> Any::stringToNumber() const
#else
try
{
if constexpr(std::is_same_v<T, uint16_t>)
{
return std::stoul(str.toStdString());
}
if constexpr(std::is_integral_v<T>)
{
// convert<T>() rejects values that don't fit in T, matching the
// std::from_chars branch above.
const int64_t val = std::stol(str.toStdString());
Any temp_any(val);
return temp_any.convert<T>();
Expand Down
13 changes: 13 additions & 0 deletions tests/gtest_any.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,16 @@ TEST(Any, StringToNumberIgnoresLocale)
EXPECT_EQ(Any(std::string("42")).cast<int>(), 42);
EXPECT_ANY_THROW(auto res = Any(std::string("fifty")).cast<double>());
}

TEST(Any, StringToUint16IsRangeChecked)
{
// A string that doesn't fit in uint16_t must be rejected, not wrapped.
EXPECT_EQ(Any(std::string("65535")).cast<uint16_t>(), 65535);
EXPECT_ANY_THROW(auto res = Any(std::string("65536")).cast<uint16_t>());
EXPECT_ANY_THROW(auto res = Any(std::string("70000")).cast<uint16_t>());
EXPECT_ANY_THROW(auto res = Any(std::string("-1")).cast<uint16_t>());

auto bb = Blackboard::create();
bb->set("value", std::string("70000"));
EXPECT_ANY_THROW(auto res = bb->get<uint16_t>("value"));
}
Loading