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

Don't leak memory on realloc failing. #232

Merged
merged 1 commit into from
Apr 10, 2020
Merged

Conversation

clalancette
Copy link
Contributor

When reallocing a memory region, it is possible for the allocation
to fail. If it does, and you are directly assigning to the
"original" pointer, you'll lose the pointer and hence leak
the memory. Store the result of the reallocation in a temporary,
do error checking, and then only reassign char_array->buffer
once we are confident it points to real memory.

Signed-off-by: Chris Lalancette clalancette@openrobotics.org

This should fix #231

When reallocing a memory region, it is possible for the allocation
to fail.  If it does, and you are directly assigning to the
"original" pointer, you'll lose the pointer and hence leak
the memory.  Store the result of the reallocation in a temporary,
do error checking, and then only reassign char_array->buffer
once we are confident it points to real memory.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Copy link
Contributor

@piraka9011 piraka9011 left a comment

Choose a reason for hiding this comment

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

LGTM pending green CI. Thanks for the fix!

@clalancette
Copy link
Contributor Author

After looking further, this will solve the leaks in test_char_array.c, but not in test_logging_custom_env.cpp. That will need another fix (which I have ready). I still think we should proceed with this simple change, as it is a step forward, and then I'll open another PR with the other fixes.

@clalancette
Copy link
Contributor Author

CI:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@clalancette clalancette merged commit cdc0631 into master Apr 10, 2020
@clalancette clalancette deleted the dont-leak-on-realloc-fail branch April 10, 2020 17:02
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

Successfully merging this pull request may close these issues.

char_array is leaking memory
4 participants