From: Yedidya Feldblum Date: Fri, 17 Feb 2017 05:07:49 +0000 (-0800) Subject: Fix exception_wrapper::throwException when called before main X-Git-Tag: v2017.03.06.00~28 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=05499631dc3e1160d673a7dd57e70fc797e532dc;p=folly.git 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 --- 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;