Fix default reporter use after free#2248
Open
devtejasx wants to merge 2 commits into
Open
Conversation
Collaborator
CreateDefaultDisplayReporter() cached its reporter in a function-local static and returned that pointer. But the returned pointer is owned by the caller: RunSpecifiedBenchmarks() wraps it in a std::unique_ptr that frees it when the call returns. The first call therefore freed the static reporter, and the next call returned the same, now-dangling pointer, which was dereferenced when setting up the output streams -- a heap-use-after-free reachable by simply calling RunSpecifiedBenchmarks more than once (reported via fuzzing on the no-match filter path). Return a fresh, caller-owned reporter on every call. Add default_display_reporter_test, which holds two reporters from two calls alive at once and asserts they are distinct objects; it fails on the old static implementation and passes with this fix. Verified locally with g++ 15.2; benchmark_test and memory_manager_test still pass. Signed-off-by: devtejasx <tejasnagmote520@gmail.com>
63a6bf9 to
f1f1485
Compare
Contributor
Author
|
Rebased — the branch is now a single commit (f1f1485) on top of current main containing only the 4-file fix. On the |
dmah42
reviewed
Jul 16, 2026
|
|
||
| BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity(); | ||
|
|
||
| // Creates the display reporter selected by --benchmark_format. Returns a |
Member
There was a problem hiding this comment.
if the caller is meant to own the returned type, then we should be returning a unique_ptr.
https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2240.
Root cause: CreateDefaultDisplayReporter() cached the reporter it creates in a function-local static, while every caller owns and frees the returned pointer (RunSpecifiedBenchmarks() wraps it in a std::unique_ptr). The first call therefore freed the cached object, and every later call returned a dangling pointer, producing the heap-use-after-free reported in #2240.
Fix: return a fresh, caller-owned reporter on every call, and document the ownership contract at the public declaration in benchmark_api.h.
Testing:
Note: the branch was rebased onto current main to remove an unrelated commit that was previously included by mistake.
AI disclosure (per AGENTS.md): this change was developed with AI assistance (Anthropic's Claude).