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 memory leaks #16

Merged
merged 2 commits into from
Nov 13, 2023
Merged

Fix memory leaks #16

merged 2 commits into from
Nov 13, 2023

Conversation

mforthewin
Copy link
Contributor

@mforthewin mforthewin commented Nov 12, 2023

The program in its current state leaks memory from the formatStatContent function during its execution.

Instrumenting the binary with Valgrind's memcheck tool exposes the oversight:

$ valgrind -s --tool=memcheck --leak-check=full --show-leak-kinds=all $(which zps)
[...]
==30228== LEAK SUMMARY:
==30228==    definitely lost: 95,653 bytes in 382 blocks
==30228==    indirectly lost: 1,319,712 bytes in 8,369 blocks
==30228==      possibly lost: 0 bytes in 0 blocks
==30228==    still reachable: 11,136 bytes in 69 blocks
==30228==         suppressed: 0 bytes in 0 blocks
==30228== 
==30228== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

Running the program compiled using gcc with -fsanitize=address -fsanitize=leak -fsanitize=undefined also would have shown the presence of leaked memory.

The origins of the leaks are calls to regcomp and strdup without being followed by their appropriate deallocating calls (regfree and free, respectively).

The commit c11d771 fixes this by:

  • putting the free outside of the if statement's block, as it should be unconditional
  • inserting the regfree call before returning

With these patches applied, the program runs like this:

$ valgrind -s --tool=memcheck --leak-check=full --show-leak-kinds=all ./zps
[...]
==31351== HEAP SUMMARY:
==31351==     in use at exit: 0 bytes in 0 blocks
==31351==   total heap usage: 16,183 allocs, 16,183 frees, 105,154,201 bytes allocated
==31351== 
==31351== All heap blocks were freed -- no leaks are possible
==31351== 
==31351== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Commit cb61943 simply adds #include guards to the header to avoid redefinitions.

Copy link
Owner

@orhun orhun left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks for your contribution! 🐻

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Files Coverage Δ
src/zps.c 96.18% <100.00%> (+0.05%) ⬆️

📢 Thoughts on this report? Let us know!

@orhun orhun merged commit 83f38fd into orhun:master Nov 13, 2023
2 checks passed
@mforthewin mforthewin deleted the fixes branch November 13, 2023 01:36
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.

3 participants