From 9a38a3e188754ab50e593a281ffffa47b9c14a5f Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 1 Nov 2018 10:48:50 -0700 Subject: [PATCH] Don't use static for raw_ostream outs/errs (#1401) Static destruction order is not well defined, so it was possible for outs() or errs() return value to be destroyed even while other code was running, resulting in a crash. Instead dynamically allocate the static so the destructor never runs. While this technically leaks, valgrind generally supresses such leaks as the data is still "reachable" from the static pointer. --- wpiutil/src/main/native/cpp/llvm/raw_ostream.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/wpiutil/src/main/native/cpp/llvm/raw_ostream.cpp b/wpiutil/src/main/native/cpp/llvm/raw_ostream.cpp index 83d5eeedc9..97cd83d889 100644 --- a/wpiutil/src/main/native/cpp/llvm/raw_ostream.cpp +++ b/wpiutil/src/main/native/cpp/llvm/raw_ostream.cpp @@ -675,23 +675,23 @@ void raw_fd_ostream::anchor() {} raw_ostream &wpi::outs() { // Set buffer settings to model stdout behavior. std::error_code EC; - static raw_fd_ostream S("-", EC, sys::fs::F_None); + static raw_fd_ostream* S = new raw_fd_ostream("-", EC, sys::fs::F_None); assert(!EC); - return S; + return *S; } /// errs() - This returns a reference to a raw_ostream for standard error. /// Use it like: errs() << "foo" << "bar"; raw_ostream &wpi::errs() { // Set standard error to be unbuffered by default. - static raw_fd_ostream S(STDERR_FILENO, false, true); - return S; + static raw_fd_ostream* S = new raw_fd_ostream(STDERR_FILENO, false, true); + return *S; } /// nulls() - This returns a reference to a raw_ostream which discards output. raw_ostream &wpi::nulls() { - static raw_null_ostream S; - return S; + static raw_null_ostream* S = new raw_null_ostream; + return *S; } //===----------------------------------------------------------------------===//