Boost C++ Libraries: Ticket #12254: NULL reference to error_code passing to noexcept functions caused std::terminate https://svn.boost.org/trac10/ticket/12254 <p> With compilers that support noexcept, the following simple code may end up in std::terminate(): </p> <pre class="wiki">#include &lt;boost/filesystem.hpp&gt; int main(int argc, char *argv[]) { try { boost::filesystem::path p(argv[0]); copy(p, p); // EEXIST, then std::terminate } catch (...) {} return 0; } </pre><p> This is because the noexcept function copy_file() has received a NULL reference, which causes one of its subroutines throws an exception. Since copy_file() is noexcept and it throws exceptions, the std::terminate() is called. </p> <p> The call stack is (functions are called from bottom to top): </p> <pre class="wiki">noexcept? function ----------------------------------------------------- false error(unsigned long error_num, const boost::filesystem::path &amp; p1, const boost::filesystem::path &amp; p2, boost::system::error_code * ec, const char * message) false detail::copy_file(const boost::filesystem::path &amp; from, const boost::filesystem::path &amp; to, boost::filesystem::detail::copy_option option, boost::system::error_code * ec) true copy_file(const boost::filesystem::path &amp; from, const boost::filesystem::path &amp; to, boost::filesystem::copy_option option, boost::system::error_code &amp; ec) false detail::copy(const boost::filesystem::path &amp; from, const boost::filesystem::path &amp; to, boost::system::error_code * ec) false copy(const boost::filesystem::path &amp; from, const boost::filesystem::path &amp; to) false main(int argc, char * * argv) </pre><p> The function copy() calls detail::copy() without providing the ec parameter, so ec in detail::copy() is using the default value 0. detail::copy() then calls copy_file(), passing *ec as its parameter. Since ec in detail:copy() is NULL, copy_file() will receive a NULL reference. It then use &amp;ec(that is NULL) to call detail::copy_file(). Since the target file exists, detail::copy_file() will generates EEXIST. Then an exception will be thrown from error(). The exception will be passed through the call stack until it reaches copy_file(). copy_file() is noexcept so it cannot throw exceptions. Then std::terminate() is called. </p> <p> There may be other similar situations apart from this case in Boost.Filesystem. I think the current workaround is removing the BOOST_NOEXCEPT from these functions. For a complete solution, the functions may need careful reviews. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12254 Trac 1.4.3 aerisnju@… Tue, 07 Jun 2016 10:38:29 GMT attachment set https://svn.boost.org/trac10/ticket/12254 https://svn.boost.org/trac10/ticket/12254 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">12254.patch</span> </li> </ul> Ticket anonymous Tue, 07 Jun 2016 10:39:07 GMT <link>https://svn.boost.org/trac10/ticket/12254#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12254#comment:1</guid> <description> <p> Added a patch for fix this problem. </p> </description> <category>Ticket</category> </item> </channel> </rss>