tree: fix mem leaks in multiple nvme ctrl routines#1028
tree: fix mem leaks in multiple nvme ctrl routines#1028martin-gpy wants to merge 1 commit intolinux-nvme:masterfrom
Conversation
Valgrind revealed memory leaks in multiple nvme ctrl routines including nvme_configure_ctrl(), nvme_ctrl_alloc() & nvme_scan_ctrl(). This was because previously allocated memory was not freed before assigning new attr values to the respective nvme_ctrl struct members in these routines. Fix the same. Signed-off-by: Martin George <[email protected]>
| void nvme_ctrl_set_addr(nvme_ctrl_t c, const char *addr) | ||
| { | ||
| if (c->address) { | ||
| free(c->address); |
There was a problem hiding this comment.
free() is NULL-safe. So you could just call it w/o first cheking that the pointer in non-NULL.
There was a problem hiding this comment.
Ok. Guess this should be cleaned up in other parts of the code too.
| * @c: Controller whose address to set | ||
| * @addr: Address | ||
| */ | ||
| void nvme_ctrl_set_addr(nvme_ctrl_t c, const char *addr); |
There was a problem hiding this comment.
I don't think these function should be exposed at all.
There was a problem hiding this comment.
Are you saying these pointers should be freed within nvme_configure_ctrl() & nvme_ctrl_alloc() itself? That would make the code ugly to read though...
There was a problem hiding this comment.
I am still reading through the code. Not sure what is best, but I really don't want these setters in the public API.
|
|
So how should we proceed here? Do you want me to resubmit after addressing @martin-belanger's comment above? Or avoid exposing these new ctrl functions altogether and free the individual ctrl poiners within |
I haven't had enough time to look into it yet. The issue I have is that all those function have been written with the assumption that we only initialize a ctrl object once. But doesn't seem the case. Thus I want to figure out if there is a fundamental problem which we need to address. I don't want to paper over design issues. |
| if (!c) | ||
| return NULL; | ||
|
|
||
| path = NULL; |
There was a problem hiding this comment.
Why unrelated? path already gets freed and set to NULL in the helper function in my patch. So there was no need to separately set it to NULL here to avoid getting it freed through the cleanup macro. Otherwise this would have resulted in a double free.
There was a problem hiding this comment.
Ah didn't realize there is the cleanup hook. Hmm I think we should avoid passing owner ship like this around, it leads to more problems in the long run. If nvme_ctrl_alloc needs the path variable it should duplicate it. Let me look into it.
|
We already have In |
|
urgh, this doesn't work either, because |
Valgrind revealed memory leaks in multiple nvme ctrl routines including nvme_configure_ctrl(), nvme_ctrl_alloc() & nvme_scan_ctrl(). This was because previously allocated memory was not freed before assigning new attr values to the respective nvme_ctrl struct members in these routines. Fix the same.