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
kernel: fix memory leaks in lantiq #2678
Conversation
To simplify, you could use GCC's cleanup attribute: http://echorand.me/site/notes/articles/c_cleanup/cleanup_attribute_c.html |
Oh that is nice, I didn't know about it. So, should I put a single free in the cleanup attribute and remove all the other ones? |
@neheb In kernel?
I would rather use common kernel if (!foo)
goto free_buf;
if (!bar)
goto free_buf_orig;
return 0;
free_buf_orig:
free(buf_orig);
free_buf:
free(buf);
return -1; |
This looks like a standalone utility. |
IIRC, you create a static function that takes a double pointer as an argument that just calls free. It makes all those if statements useless as GCC handles cleaning up automatically. It's a way to do C++ destructors in C (similar but not quite the same). systemd does this all over the codebase. |
e8033fc
to
4d4eca4
Compare
@@ -83,8 +88,13 @@ int main(int argc, char **argv) | |||
} | |||
|
|||
buf_orig = malloc(s.st_size); | |||
if (!buf_orig) { | |||
printf("Failed to alloc %d bytes\n", s.st_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be %zu I believe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original code used %d
but I can change that.
Also should I keep the new if I created? Or just keep it as it was originally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok 👍
I'm gonna change the string format
4d4eca4
to
f10bf20
Compare
@neheb Can you have another look at this? |
Seems fine. |
@ynezz Would you review this again? |
@ynezz Ping. |
ping @hauke |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commit title prefix should be "ltq-vdsl-fw:" ... |
@draane Ping... |
Add `cleanup` attribute for the variables `buf_orig` and `buf`. Those two variables were never freed, causing possible memory leaks. Now gcc will automatically free the two variables once they go out of scope. Signed-off-by: Andrea Dalla Costa <andrea@dallacosta.me>
f10bf20
to
4ba5e09
Compare
@adschm @CodeFetch I just changed the commit title |
I could be wrong, but I think this patch introduces undefined behaviour. If main returns before the buffers are allocated, then free_buffer will be call free with an uninitialised pointer, which could point anywhere. Running this in valgrind shows the problem: #include <stdlib.h>
void free_mem(void **mem) { free(*mem); }
int main(int argc, char **argv) {
void *mem __attribute__((__cleanup__(free_mem)));
return 0;
} This is fixable by either initialising the pointers to Personally I don't think this is a memory leak, as the memory is freed when the program exits. |
The author of the code seconds the last comment:
Unlikely that the code is required or will be ever merged. Closing here as we are talking about "possible memory leaks" without a proof. |
Add calls to
free
for the variablesbuf_orig
andbuf
.Those two variables were never freed, causing possible
memory leaks.