mirror of
https://github.com/google/benchmark.git
synced 2025-03-10 01:00:08 +08:00
Fix std::cout/std::cerr static initialization order fiasco.
The benchmark library internals write to std::cout/std::cerr during program startup. This can cause segfaults when the user doesn't include <iostream> in the benchmark (which init's the streams). This patch fixes this by emitting a dynamic initializer in every TU which initializes the streams.
This commit is contained in:
parent
78e22f10de
commit
cbcd7b656e
@ -214,6 +214,10 @@ void UseCharPointer(char const volatile*);
|
||||
// registered benchmark.
|
||||
Benchmark* RegisterBenchmarkInternal(Benchmark*);
|
||||
|
||||
// Ensure that the standard streams are properly initialized in every TU.
|
||||
int InitializeStreams();
|
||||
static int stream_init_anchor = InitializeStreams();
|
||||
|
||||
} // end namespace internal
|
||||
|
||||
|
||||
|
@ -1199,6 +1199,11 @@ Benchmark* RegisterBenchmarkInternal(Benchmark* bench) {
|
||||
return bench;
|
||||
}
|
||||
|
||||
int InitializeStreams() {
|
||||
static std::ios_base::Init init;
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // end namespace internal
|
||||
|
||||
void Initialize(int* argc, char** argv) {
|
||||
|
Loading…
Reference in New Issue
Block a user