Skip to content

Commit

Permalink
Fix mem leaks in unit test from 776 (#779)
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Brawner <brawner@gmail.com>
  • Loading branch information
brawner authored Aug 31, 2020
1 parent eb8e3a6 commit 6040298
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
35 changes: 28 additions & 7 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,12 @@ TEST(test_file_parser, seq_map1) {
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_FALSE(res);
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

TEST(test_file_parser, seq_map2) {
Expand All @@ -336,9 +339,12 @@ TEST(test_file_parser, seq_map2) {
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_FALSE(res);
// No cleanup, rcl_parse_yaml_file takes care of that if it fails
}

TEST(test_file_parser, params_with_no_node) {
Expand All @@ -360,9 +366,12 @@ TEST(test_file_parser, params_with_no_node) {
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_FALSE(res);
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

TEST(test_file_parser, no_alias_support) {
Expand All @@ -384,9 +393,12 @@ TEST(test_file_parser, no_alias_support) {
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_FALSE(res);
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

TEST(test_file_parser, empty_string) {
Expand Down Expand Up @@ -436,9 +448,12 @@ TEST(test_file_parser, no_value1) {
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_FALSE(res);
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

TEST(test_file_parser, indented_ns) {
Expand All @@ -460,9 +475,12 @@ TEST(test_file_parser, indented_ns) {
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_FALSE(res);
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

// Regression test for https://github.com/ros2/rcl/issues/419
Expand All @@ -485,9 +503,12 @@ TEST(test_file_parser, maximum_number_parameters) {
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_FALSE(res);
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

int32_t main(int32_t argc, char ** argv)
Expand Down
16 changes: 6 additions & 10 deletions rcl_yaml_param_parser/test/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,9 @@ TEST(RclYamlParamParser, test_parse_file_with_bad_allocator) {
bool res = rcl_parse_yaml_file(path, params_hdl);
// Not verifying res is true or false here, because eventually it will come back with an ok
// result. We're just trying to make sure that bad allocations are properly handled
if (res) {
// This is already freed in the case of a non-ok error in rcl_parse_yaml_file
rcl_yaml_node_struct_fini(params_hdl);
params_hdl = NULL;
}
(void)res;
rcl_yaml_node_struct_fini(params_hdl);
params_hdl = NULL;
}

// Check sporadic failing calloc calls
Expand All @@ -347,11 +345,9 @@ TEST(RclYamlParamParser, test_parse_file_with_bad_allocator) {
bool res = rcl_parse_yaml_file(path, params_hdl);
// Not verifying res is true or false here, because eventually it will come back with an ok
// result. We're just trying to make sure that bad allocations are properly handled
if (res) {
// This is already freed in the case of a non-ok error in rcl_parse_yaml_file
rcl_yaml_node_struct_fini(params_hdl);
params_hdl = NULL;
}
(void)res;
rcl_yaml_node_struct_fini(params_hdl);
params_hdl = NULL;
}
}

Expand Down

0 comments on commit 6040298

Please sign in to comment.