Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix yaml parser error when meets .nan #741

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
19 changes: 18 additions & 1 deletion rcl_yaml_param_parser/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <string.h>
#include <yaml.h>
#include <ctype.h>

#include "rcl_yaml_param_parser/parser.h"
#include "rcl_yaml_param_parser/types.h"
Expand Down Expand Up @@ -1105,7 +1106,23 @@ static void * get_value(
{
errno = 0;
endptr = NULL;
dval = strtod(value, &endptr);
char * tmp_value = NULL;
if ((0 == strcasecmp(value, ".nan")) ||
Ada-King marked this conversation as resolved.
Show resolved Hide resolved
(0 == strcasecmp(value, ".inf")) ||
(0 == strcasecmp(value, "+.inf")) ||
(0 == strcasecmp(value, "-.inf")) ||
(0 == strcasecmp(value, ".infinity")) ||
(0 == strcasecmp(value, "+.infinity")) ||
(0 == strcasecmp(value, "-.infinity")))
{
tmp_value = rcutils_strdup(value, allocator);
clalancette marked this conversation as resolved.
Show resolved Hide resolved
for ( ; !isalpha(*tmp_value); ) {
tmp_value += 1;
}
dval = strtod(tmp_value, &endptr);
} else {
dval = strtod(value, &endptr);
}
if ((0 == errno) && (NULL != endptr)) {
if ((NULL != endptr) && (endptr != value)) {
if (('\0' != *value) && ('\0' == *endptr)) {
Expand Down
8 changes: 8 additions & 0 deletions rcl_yaml_param_parser/test/special_float1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# config/test_yaml
---

lidar_ns:
lidar_1:
ros__parameters:
test_nan: [1.1, 2.2, .nan, .NAN]
test_inf: [4.4, 5.5, .inf, +.inf, -.INF, +.infinity, -.INFINITY]
8 changes: 8 additions & 0 deletions rcl_yaml_param_parser/test/special_float2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# config/test_yaml
---

lidar_ns:
lidar_1:
ros__parameters:
test_nan: [1.1, 2.2, nananana]
clalancette marked this conversation as resolved.
Show resolved Hide resolved
test_nan2: [3.3, nanananan]
clalancette marked this conversation as resolved.
Show resolved Hide resolved
48 changes: 48 additions & 0 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,54 @@ TEST(test_file_parser, maximum_number_parameters) {
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

TEST(test_file_parser, special_float_point) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)) << rcutils_get_error_string().str;
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
ASSERT_TRUE(NULL != test_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(test_path, allocator.state);
});

// Test special float point with correct value.
{
char * path = rcutils_join_path(test_path, "special_float1.yaml", allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcl_yaml_node_struct_fini(params_hdl);
});
bool res = rcl_parse_yaml_file(path, params_hdl);
EXPECT_TRUE(res) << rcutils_get_error_string().str;
clalancette marked this conversation as resolved.
Show resolved Hide resolved
}

// Test special float point with improperly value.
{
char * path = rcutils_join_path(test_path, "special_float2.yaml", allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string().str);
clalancette marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_FALSE(res);
clalancette marked this conversation as resolved.
Show resolved Hide resolved
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}
}

int32_t main(int32_t argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down