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

changes to support memory audit #261

Merged
merged 4 commits into from
Jun 17, 2018
Merged
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
8 changes: 4 additions & 4 deletions rcl/src/rcl/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const char * rcl_create_node_logger_name(
return node_logger_name;
}

const char * rcl_get_secure_root(const char * node_name)
const char * rcl_get_secure_root(const char * node_name, const rcl_allocator_t * allocator)
{
const char * ros_secure_root_env = NULL;
if (NULL == node_name) {
Expand All @@ -115,9 +115,9 @@ const char * rcl_get_secure_root(const char * node_name)
if (!ros_secure_root_size) {
return NULL; // environment variable was empty
}
char * node_secure_root = rcutils_join_path(ros_secure_root_env, node_name);
char * node_secure_root = rcutils_join_path(ros_secure_root_env, node_name, *allocator);
if (!rcutils_is_directory(node_secure_root)) {
free(node_secure_root);
allocator->deallocate(node_secure_root, allocator->state);
return NULL;
}
return node_secure_root;
Expand Down Expand Up @@ -315,7 +315,7 @@ rcl_node_init(
node_security_options.enforce_security = RMW_SECURITY_ENFORCEMENT_PERMISSIVE;
} else { // if use_security
// File discovery magic here
const char * node_secure_root = rcl_get_secure_root(name);
const char * node_secure_root = rcl_get_secure_root(name, allocator);
if (node_secure_root) {
node_security_options.security_root_path = node_secure_root;
} else {
Expand Down
8 changes: 4 additions & 4 deletions rcl/test/rcl/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ TEST(CLASSNAME(rcl_time, RMW_IMPLEMENTATION), default_clock_instanciation) {
EXPECT_EQ(retval, RCL_RET_OK) << rcl_get_error_string_safe();
ASSERT_TRUE(rcl_clock_valid(&ros_clock));

rcl_clock_t * steady_clock =
reinterpret_cast<rcl_clock_t *>(calloc(1, sizeof(rcl_clock_t)));
rcl_clock_t * steady_clock = reinterpret_cast<rcl_clock_t *>(
allocator.zero_allocate(1, sizeof(rcl_clock_t), allocator.state));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way around reinterpret_cast here? MISRA C++ guide on this is:
Approval required. Rationale: Reinterpreting bits into a different type should not be
necessary in a high level programming language. Exceptions could be made when a wrapper
to a third party library working with opaque data types needs to be created.

Also SEI Cert C++ is not fond of it: https://wiki.sei.cmu.edu/confluence/display/cplusplus/VOID+EXP13-CPP.+Prefer+dynamic_cast+over+static_cast+over+reinterpret_cast.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I believe it is required and I don't know any other way to do this when calling a C function. Malloc returns void * which must be cast to the pointer type you're using it as.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example they use the C-style cast in the example:

http://en.cppreference.com/w/cpp/memory/c/malloc

But we have a rule to not use the C-style cast when we can use reinterpret_cast or static_cast instead.

Copy link
Member

@dirk-thomas dirk-thomas Jun 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting from a void* should be done with a static_cast.

retval = rcl_steady_clock_init(steady_clock, &allocator);
EXPECT_EQ(retval, RCL_RET_OK) << rcl_get_error_string_safe();
ASSERT_TRUE(rcl_clock_valid(steady_clock));

rcl_clock_t * system_clock =
reinterpret_cast<rcl_clock_t *>(calloc(1, sizeof(rcl_clock_t)));
rcl_clock_t * system_clock = reinterpret_cast<rcl_clock_t *>(
allocator.zero_allocate(1, sizeof(rcl_clock_t), allocator.state));
retval = rcl_system_clock_init(system_clock, &allocator);
EXPECT_EQ(retval, RCL_RET_OK) << rcl_get_error_string_safe();
ASSERT_TRUE(rcl_clock_valid(system_clock));
Expand Down
82 changes: 46 additions & 36 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "rcl_yaml_param_parser/parser.h"

#include "rcutils/allocator.h"
#include "rcutils/error_handling.h"
#include "rcutils/filesystem.h"

Expand All @@ -26,8 +27,9 @@ rcutils_allocator_t allocator = rcutils_get_default_allocator();
TEST(test_file_parser, correct_syntax) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "correct_config.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "correct_config.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
Expand All @@ -37,15 +39,16 @@ TEST(test_file_parser, correct_syntax) {
EXPECT_TRUE(res);
rcl_yaml_node_struct_print(params_hdl);
rcl_yaml_node_struct_fini(params_hdl);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

TEST(test_file_parser, multi_ns_correct_syntax) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "multi_ns_correct.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "multi_ns_correct.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
Expand All @@ -55,120 +58,127 @@ TEST(test_file_parser, multi_ns_correct_syntax) {
EXPECT_TRUE(res);
rcl_yaml_node_struct_print(params_hdl);
rcl_yaml_node_struct_fini(params_hdl);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

TEST(test_file_parser, seq_map1) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "seq_map1.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "seq_map1.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
EXPECT_FALSE(NULL == params_hdl);
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string_safe());
EXPECT_FALSE(res);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

TEST(test_file_parser, seq_map2) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "seq_map2.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "seq_map2.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
EXPECT_FALSE(NULL == params_hdl);
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string_safe());
EXPECT_FALSE(res);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

TEST(test_file_parser, params_with_no_node) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "params_with_no_node.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "params_with_no_node.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
EXPECT_FALSE(NULL == params_hdl);
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string_safe());
EXPECT_FALSE(res);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

TEST(test_file_parser, no_alias_support) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "no_alias_support.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "no_alias_support.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
EXPECT_FALSE(NULL == params_hdl);
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string_safe());
EXPECT_FALSE(res);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

TEST(test_file_parser, max_string_sz) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "max_string_sz.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "max_string_sz.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
EXPECT_FALSE(NULL == params_hdl);
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string_safe());
EXPECT_FALSE(res);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

TEST(test_file_parser, no_value1) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "no_value1.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "no_value1.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
EXPECT_FALSE(NULL == params_hdl);
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string_safe());
EXPECT_FALSE(res);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

TEST(test_file_parser, indented_ns) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
char * test_path = rcutils_join_path(cur_dir, "test");
char * path = rcutils_join_path(test_path, "indented_name_space.yaml");
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
char * path = rcutils_join_path(test_path, "indented_name_space.yaml", allocator);
fprintf(stderr, "cur_path: %s\n", path);
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
EXPECT_FALSE(NULL == params_hdl);
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string_safe());
EXPECT_FALSE(res);
free(test_path);
free(path);
allocator.deallocate(test_path, allocator.state);
allocator.deallocate(path, allocator.state);
}

int32_t main(int32_t argc, char ** argv)
Expand Down