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

Fix leaks on realloc() failures #3005

Closed

Conversation

@danielgustafsson
Copy link
Member

@danielgustafsson danielgustafsson commented Sep 17, 2018

Fix a few realloc() calls which overwrote the original pointer with the return value, causing a memory leak in case the reallocation failed.

This extends and replaces #2997 with additional fixes of the same kind in the examples as the one submitted there. The second commit fixes a memleak in darwinssl which was found by looking at this PR.

@CrazyHackGUT I've editorialized your commit a bit by fixing the other copy/paste realloc() issues in examples.

@CrazyHackGUT CrazyHackGUT mentioned this pull request Sep 17, 2018
Make sure to not overwrite the reallocated pointer in realloc() calls
to avoid a memleak on memory errors.
@danielgustafsson danielgustafsson force-pushed the danielgustafsson:dg-realloc_memleaks branch from 9f7bd83 to 3f26864 Sep 17, 2018
@CrazyHackGUT
Copy link
Contributor

@CrazyHackGUT CrazyHackGUT commented Sep 17, 2018

👍

@bagder
bagder approved these changes Sep 17, 2018
@@ -2056,11 +2056,14 @@ static int read_cert(const char *file, unsigned char **out, size_t *outlen)

if(len + n >= cap) {
cap *= 2;
data = realloc(data, cap);
if(!data) {
ptr = realloc(data, cap);

This comment has been minimized.

@bagder

bagder Sep 17, 2018
Member

Alternatively, use Curl_saferealloc there, which frees the memory on fail.

@danielgustafsson danielgustafsson force-pushed the danielgustafsson:dg-realloc_memleaks branch from 3f26864 to e56082f Sep 17, 2018
@danielgustafsson
Copy link
Member Author

@danielgustafsson danielgustafsson commented Sep 17, 2018

The reallocation was using the input pointer for the return value, which
leads to a memory leak on reallication failure. Fix by instead use the
safe internal API call Curl_saferealloc().
@danielgustafsson danielgustafsson force-pushed the danielgustafsson:dg-realloc_memleaks branch from e56082f to f828ef1 Sep 17, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Dec 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.