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

Is it necessary to release resources? #108

Closed
rebeccalee97y opened this issue Apr 26, 2022 · 2 comments
Closed

Is it necessary to release resources? #108

rebeccalee97y opened this issue Apr 26, 2022 · 2 comments

Comments

@rebeccalee97y
Copy link

Hi, there exists a obvious memory leak in dma.c. See the following code, the allocated resource of calloc is not freed when strdup conducts error handling.

dma/dma.c

Lines 184 to 189 in 1b10f76

it = calloc(1, sizeof(*it));
if (it == NULL)
return (-1);
it->addr = strdup(str);
if (it->addr == NULL)
return (-1);

Moreover, I have noticed one possible memory leak in the following code. At line 218, when aliased < 0 , the allocated resource by function calloc and strdup should be freed, with adding free(it->addr); free(it);?

dma/dma.c

Lines 215 to 240 in 1b10f76

aliased = do_alias(queue, it->addr);
if (!aliased && expand == EXPAND_WILDCARD)
aliased = do_alias(queue, "*");
if (aliased < 0)
return (-1);
if (aliased) {
LIST_REMOVE(it, next);
} else {
/* Local destination, check */
pw = getpwnam(it->addr);
if (pw == NULL)
goto out;
/* XXX read .forward */
endpwent();
}
}
} else {
it->remote = 1;
}
return (0);
out:
free(it->addr);
free(it);
return (-1);

@corecode
Copy link
Owner

Hi, thanks for the code review.

The intent is that memory allocation failure is critical and we cannot recover from it. The code returns -1, which should signal to the caller a critical error, which should lead to exiting the process. Because we know we will exit immediately, there is no need to complicate code by free()ing structures.

If there is any code path where an error result does not lead to the process exiting, then we need to make sure that the allocations do not accumulate.

@rebeccalee97y
Copy link
Author

Thanks for the explanation :)

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

No branches or pull requests

2 participants