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

[programs] fix the memory leak #368

Closed
wants to merge 1 commit into from

Conversation

bryonglodencissp
Copy link

Passing one pointer into realloc() and assigning the result directly into that same pointer variable can cause a memory leak if the reallocation fails, because the original allocation will still exist. The correct way to do this is to use a temporary pointer variable.

[programs/util.h:359]: (error) Common realloc mistake: 'buf' nulled but not freed upon failure

Found by https://github.com/bryongloden/cppcheck
buf = (char*)realloc(buf, newListSize);
bufend = buf + newListSize;
if (!buf) return NULL;
buftmp = (char*)realloc(buf, newListSize);
Copy link
Contributor

@Cyan4973 Cyan4973 Sep 14, 2016

Choose a reason for hiding this comment

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

prefer declaring variables in narrowest scope.
buftmp should be declared here, instead of line 347.
It can be made const too.
It also does not need to be char* since it's not accessed anyway.
So I would lean for :
void* const buftmp = realloc(buf, newListSize);
and corresponding cast at line 361

if (!buf) return NULL;
} else {
free(buf);
/* And handle error */
Copy link
Contributor

Choose a reason for hiding this comment

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

Which error should be handled here ?
return NULL; ? ( ping @inikep)
The patch seems incomplete here.

if (buftmp != NULL) {
buf = buftmp;
bufend = buf + newListSize;
if (!buf) return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

if buf = buftmp and buftmp != NULL, then !buf is never true.
This test should be removed

@Cyan4973
Copy link
Contributor

Good catch @bryongloden !
see comments in code

@ghost ghost added the CLA Signed label Sep 15, 2016
@inikep
Copy link
Contributor

inikep commented Sep 15, 2016

Yes, a good catch. I forgot about If realloc() fails the original block is left untouched; it is not freed or moved.. But I would prefer a solution with a new function e.g. UTIL_realloc() that will free an original buffer even if new it's not allocated by realloc(). This function can be useful for the future and will fix 3 realloc() calls in util.h.

@inikep
Copy link
Contributor

inikep commented Sep 15, 2016

The path is implemented at inikep@6173931

@Cyan4973
Copy link
Contributor

Fixed by #370

@Cyan4973 Cyan4973 closed this Sep 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants