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
5 changes: 4 additions & 1 deletion rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ RCLCPP_LOCAL
bool
__are_doubles_equal(double x, double y, double ulp = 100.0)
{
if (!std::isfinite(x) || !std::isfinite(y)) {
return x == y;
}
return std::abs(x - y) <= std::numeric_limits<double>::epsilon() * std::abs(x + y) * ulp;
}

Expand Down Expand Up @@ -234,7 +237,7 @@ __check_double_range(
{
return result;
}
if ((value < fp_range.from_value) || (value > fp_range.to_value)) {
if (!(value >= fp_range.from_value && value <= fp_range.to_value)) {
result.successful = false;
result.reason = format_range_reason(descriptor.name, "floating point");
return result;
Expand Down
22 changes: 21 additions & 1 deletion rclcpp/test/rclcpp/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,26 @@ TEST_F(TestNode, set_parameter_undeclared_parameters_not_allowed) {
EXPECT_FALSE(node->set_parameter(rclcpp::Parameter(name, 9.999)).successful);
EXPECT_EQ(node->get_parameter(name).get_value<double>(), 11.0);
}
{
// floating point range must reject non-finite values (issue #2898).
auto name = "parameter"_unq;
rcl_interfaces::msg::ParameterDescriptor descriptor;
descriptor.floating_point_range.resize(1);
auto & floating_point_range = descriptor.floating_point_range.at(0);
floating_point_range.from_value = 0.0;
floating_point_range.to_value = 100.0;
floating_point_range.step = 0.0;
node->declare_parameter(name, 50.0, descriptor);

constexpr double inf = std::numeric_limits<double>::infinity();
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
EXPECT_FALSE(node->set_parameter(rclcpp::Parameter(name, inf)).successful);
EXPECT_EQ(node->get_parameter(name).get_value<double>(), 50.0);
EXPECT_FALSE(node->set_parameter(rclcpp::Parameter(name, -inf)).successful);
EXPECT_EQ(node->get_parameter(name).get_value<double>(), 50.0);
EXPECT_FALSE(node->set_parameter(rclcpp::Parameter(name, nan)).successful);
EXPECT_EQ(node->get_parameter(name).get_value<double>(), 50.0);
}
{
// setting an array parameter with floating point range descriptor
auto name = "parameter"_unq;
Expand Down Expand Up @@ -1969,7 +1989,7 @@ TEST_F(TestNode, set_parameter_undeclared_parameters_not_allowed) {
RCPPUTILS_SCOPE_EXIT(
{node->remove_pre_set_parameters_callback(handler.get());}); // always reset
}
}
} // NOLINT(readability/fn_size)

TEST_F(TestNode, set_parameter_undeclared_parameters_allowed) {
rclcpp::NodeOptions no;
Expand Down