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.
This commit is contained in:
Peter Johnson
2018-11-01 10:48:50 -07:00
committed by GitHub
parent 2e3e3a47b9
commit 9a38a3e188

View File

@@ -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;
}
//===----------------------------------------------------------------------===//