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.
@CrazyHackGUT
Copy link
Contributor

👍

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

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

@danielgustafsson
Copy link
Member Author

danielgustafsson commented Sep 17, 2018 via email

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().
@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.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants