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

wants to merge 2 commits into
base: master


None yet
4 participants

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 referenced this pull request Sep 17, 2018


Fix memory leak #2997

examples: Fix memory leaks from realloc errors
Make sure to not overwrite the reallocated pointer in realloc() calls
to avoid a memleak on memory errors.

This comment has been minimized.


CrazyHackGUT commented Sep 17, 2018



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 Sep 17, 2018


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


This comment has been minimized.


danielgustafsson commented Sep 17, 2018

darwinssl: Fix realloc memleak
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().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment