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 build log leak #2534

Merged
merged 5 commits into from
Dec 10, 2024
Merged

Conversation

digit-google
Copy link
Contributor

This patch fixes a memory leak in BuildLog::Recompact(), when calling entries_.erase() for dead outputs would not free the pointed-to LogEntry.

Rather than doing a local fix with an expliciti delete, this changes the Entries type to use std::unique_ptr<LogEntry> values instead of raw pointers, as this will prevent similar bugs to appear in the future. Other call sites are updated appropriately.

This also contains minor cleanups in separate commits:

  • Use default member initialization in build_log.h.
  • Remove using namespace std from build_log.cc and build_log_test.cc
  • Remove one unnecessary string copy per entry when reading the build log.

The erase() call to remove `dead_outputs` was leaking the
LogEntry value that the map pointed to.

This patch changes the type of `Entries` to store
`std::unique_ptr<LogEntry>` values, instead of raw pointers
to get rid of the problem, and adjust call sites accordingly.

Also use Entries::emplace() instead of Entries::insert() to
create the new value in-place when inserting new ones into
the map.
@jhasse jhasse merged commit b78335e into ninja-build:master Dec 10, 2024
11 checks passed
@jhasse
Copy link
Collaborator

jhasse commented Dec 10, 2024

In general I'm against cleanup changes (i.e. adding std:: or default member init) because they change line history and cause conflicts with other PRs. But I guess sometimes it's okay and the codebase should move forward at some point.

Thanks :)

@digit-google
Copy link
Contributor Author

I understand :-) That's one of the reasons why I decomposed this into a series of small commits, apart from making reviews easier, it minimizes the chances of conflicts, or make them much easier to resolve in practice. Thanks again.

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.

2 participants