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

memory leak issue #172

Merged
merged 5 commits into from
Nov 7, 2017
Merged
Changes from 2 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: 14 additions & 5 deletions rmw_fastrtps_cpp/src/rmw_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,23 @@ get_security_file_paths(

const char * file_prefix = "file://";

std::string tmpstr;
char * file_path = nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

Could you not reduce the scope of the variable to within the for loop?

for (size_t i = 0; i < num_files; i++) {
tmpstr = std::string(rcutils_join_path(node_secure_root, file_names[i]));
if (!rcutils_is_readable(tmpstr.c_str())) {
return false;
file_path = rcutils_join_path(node_secure_root, file_names[i]);
if (file_path) {
if (!rcutils_is_readable(file_path)) {
free(file_path);
return false;
}

security_files_paths[i] = std::string(file_prefix + file_path);
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, also, I'm pretty sure file_prefix + file_path is wrong here. That's going to add two pointers, not concatenate the two of them together.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

exactly, the std::string should keep for file_path here

free(file_path);
} else {
RMW_SET_ERROR_MSG("Failed to allocate memory to get security file path");
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I didn't notice this before, but I think we still have a leak here. Consider the case where we successfully find and allocate ca.cert.pem, but fail to find cert.pem. In that case, we'll have allocated memory for ca.cert.pem, failed to find cert.pem, freed the memory for cert.pem, then returned false. At which point create_node will assume the entire function failed, and we'll still leak the memory for ca.cert.pem. I think we'll probably want to make the contract for get_security_file_paths such that they all succeed, or none of them do. In that case, I think we need to unwind any allocations we did in previous iterations of the loop here. Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so, you can see the memory allocated for ca.cert.pem has been freed if it's found from the following snippet:

security_files_paths[i] = std::string(file_prefix + file_path);
free(file_path);

So even it fails to find cert.pem, it has nothing to do with ca.cert.pem, because they use their memory allocated respectively. right ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, you are correct. Sorry, I was thinking about it incorrectly. This part is fine.

return false;
}
security_files_paths[i] = std::string(file_prefix + tmpstr);
}

return true;
}

Expand Down