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

[tlib][net] fix the memory leaks #7

Merged
merged 2 commits into from
Sep 17, 2016

Conversation

bryonglodencissp
Copy link
Contributor

Greetings @wichtounet,

On line numbers 69, 168, & 189 of net.cpp there are memory leaks, which are bugs.

The reason we should free is that memory is a finite resource within our running programs. Sure in very short running simple programs, failing to free memory won't have a noticeable effect. However on long running programs, failing to free memory means we will be consuming a finite resource without replenishing it. Eventually it will run out and our program will abruptly crash. This is why we must free memory.

[tlib/src/net.cpp:69]: (error) Memory leak: buffer
[tlib/src/net.cpp:168]: (error) Memory leak: buffer
[tlib/src/net.cpp:189]: (error) Memory leak: buffer

Found by https://github.com/bryongloden/cppcheck
@@ -158,8 +159,8 @@ std::expected<tlib::packet> tlib::wait_for_packet(size_t socket_fd) {
: [socket] "g"(socket_fd), [buffer] "g"(reinterpret_cast<size_t>(buffer))
: "rax", "rbx", "rcx");

free(buffer);
Copy link
Owner

Choose a reason for hiding this comment

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

This is wrong, because buffer is returned by the system call aspayload and therefore is used then and returned by the function.

@@ -179,8 +180,8 @@ std::expected<tlib::packet> tlib::wait_for_packet(size_t socket_fd, size_t ms) {
: [socket] "g"(socket_fd), [buffer] "g"(reinterpret_cast<size_t>(buffer)), [ms] "g"(ms)
: "rax", "rbx", "rcx");

free(buffer);
Copy link
Owner

Choose a reason for hiding this comment

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

This is wrong too, for the same reason as line 162

@@ -66,6 +66,7 @@ std::expected<tlib::packet> tlib::prepare_packet(size_t socket_fd, void* desc) {
: "rax", "rbx", "rcx", "rdx");

if (fd < 0) {
free(buffer);
Copy link
Owner

Choose a reason for hiding this comment

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

This is absolutely correct here.

@wichtounet
Copy link
Owner

Hi @bryongloden

Thank you very much for the Pull Request 👍

Memory leak are important bug indeed. And there are plenty, especially in the kernel itself. I know it and I plan to work on it, but I have to admit that it is not my absolute priority :)

The first is indeed a crude memory leak. The two other are false positive, but it would have been difficult to find it since it comes from inside a system call.

If you could fix your commit for the indicated review, I'll merge it inside the branch.

Thanks

Baptiste

applied code review changes
@wichtounet wichtounet merged commit 19c9d44 into wichtounet:develop Sep 17, 2016
@wichtounet
Copy link
Owner

Thanks for the update, I merged your change :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants