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:
Eric Fiselier 2016-08-28 22:48:48 -06:00
parent 78e22f10de
commit cbcd7b656e
2 changed files with 9 additions and 0 deletions

View File

@ -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

View File

@ -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) {