From 05499631dc3e1160d673a7dd57e70fc797e532dc Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Thu, 16 Feb 2017 21:07:49 -0800 Subject: [PATCH] Fix exception_wrapper::throwException when called before main Summary: [Folly] Fix `exception_wrapper::throwException` when called before `main`. When the `exception_wrapper` is empty, then `throwException` emits a message to `std::cerr` and terminates. If called before `main` begins or after `main` ends, then `std::cerr` might not have been constructed yet or might have already been destructed. Bad. Segfault, etc. So we need to ensure that `std::cerr` is alive by holding an instance of `std::ios_base::Init`. Reviewed By: Orvid Differential Revision: D4576190 fbshipit-source-id: dd84b5e82eb6bd817d405baf530714e0f4c314c5 --- folly/ExceptionWrapper.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/folly/ExceptionWrapper.cpp b/folly/ExceptionWrapper.cpp index d819c603..c6ad1667 100644 --- a/folly/ExceptionWrapper.cpp +++ b/folly/ExceptionWrapper.cpp @@ -26,6 +26,7 @@ namespace folly { } else if (eptr_) { std::rethrow_exception(eptr_); } + std::ios_base::Init ioinit_; // ensure std::cerr is alive std::cerr << "Cannot use `throwException` with an empty folly::exception_wrapper" << std::endl; -- 2.34.1