Boost C++ Libraries: Ticket #13189: copy throws exception from a function defined as noexcept https://svn.boost.org/trac10/ticket/13189 <p> When <code>copy</code> fails because access to the file specified by <code>from</code> is denied, the corresponding exception reaches a function defined as <code>noexcept</code>. Stack trace: </p> <pre class="wiki">bool &lt;anon. namespace&gt;::error(err_t error_num, const path&amp; p1, const path&amp; p2, error_code* ec, const char* message) void detail::copy_file(const path&amp; from, const path&amp; to, copy_option option, error_code* ec) void copy_file(const path&amp; from, const path&amp; to, BOOST_SCOPED_ENUM(copy_option) option, system::error_code&amp; ec) BOOST_NOEXCEPT void detail::copy(const path&amp; from, const path&amp; to, system::error_code* ec) void copy(const path&amp; from, const path&amp; to) </pre><p> The exception is thrown from <code>error</code>, and isn't caught anywhere along the calling path. The problem is that <code>copy_file</code> is defined as <code>noexcept</code> (<code>BOOST_NOEXCEPT</code> to be precise, but it is resolved as <code>noexcept</code> under a C++11 compliant compiler), and doesn't handle the exception either, which causes program termination by implicitly calling <code>std::terminate</code>. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13189 Trac 1.4.3 charan <harisaicharan111.challa@…> Sun, 15 Oct 2017 02:14:24 GMT <link>https://svn.boost.org/trac10/ticket/13189#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13189#comment:1</guid> <description> <p> i want to work on this bug </p> </description> <category>Ticket</category> </item> <item> <author>charan <harisaicharan111.challa@…></author> <pubDate>Mon, 16 Oct 2017 13:20:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13189#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13189#comment:2</guid> <description> <p> can i get more information regarding this bug? </p> </description> <category>Ticket</category> </item> <item> <author>kukkerman@…</author> <pubDate>Thu, 09 Nov 2017 23:27:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13189#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13189#comment:3</guid> <description> <p> The root cause of the problem is that <code>copy(from, to)</code> calls <code>detail::copy(from, to)</code> but the declaration of <code>detail::copy</code> is: </p> <pre class="wiki">void copy(const path&amp; from, const path&amp; to, system::error_code* ec=0); </pre><p> That way <code>ec</code> will be a null pointer. The next call in the chain in case of a regular file will be <code>copy_file(from, to, fs::copy_option::fail_if_exists, *ec)</code> Looking at the declaration of <code>copy_file</code> </p> <pre class="wiki">void copy_file(const path&amp; from, const path&amp; to, // See ticket #2925 BOOST_SCOPED_ENUM(copy_option) option, system::error_code&amp; ec) BOOST_NOEXCEPT </pre><p> we can see that <code>ec</code> will be a null reference. The next call will be <code>detail::copy_file(from, to, static_cast&lt;detail::copy_option&gt;(option), &amp;ec)</code> which finally calls the function <code>error</code>: </p> <pre class="wiki">bool error(err_t error_num, error_code* ec, const char* message) { if (!error_num) { if (ec != 0) ec-&gt;clear(); } else { // error if (ec == 0) BOOST_FILESYSTEM_THROW(filesystem_error(message, error_code(error_num, system_category()))); else ec-&gt;assign(error_num, system_category()); } return error_num != 0; } </pre><p> which will throw an exception because <code>ec</code> is a null pointer. This exception will propagate up to </p> <pre class="wiki">void copy_file(const path&amp; from, const path&amp; to, // See ticket #2925 BOOST_SCOPED_ENUM(copy_option) option, system::error_code&amp; ec) BOOST_NOEXCEPT </pre><p> which is a <code>noexcept</code> function. </p> </description> <category>Ticket</category> </item> <item> <author>Gábor Szuromi <kukkerman@…></author> <pubDate>Sat, 11 Nov 2017 15:07:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13189#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13189#comment:4</guid> <description> <p> After running a few tests not just <code>filesystem::copy_file</code> but <code>filesystem::copy_symlink</code> and <code>filesystem::copy_directory</code> functions are affected depending on what kind of file <code>from</code> points to. All of these errors can easily be fixed by rewriting <code>filesystem::detail::copy</code> to call functions defined in <code>filesystem::detail</code> namespace instead of <code>filesystem</code> namespace. Because I was unable to attach a patch file to the ticket I'm pasting the patched <code>filesystem::detail::copy</code> function here: </p> <pre class="wiki"> BOOST_FILESYSTEM_DECL void copy(const path&amp; from, const path&amp; to, system::error_code* ec) { file_status s(symlink_status(from, *ec)); if (ec != 0 &amp;&amp; *ec) return; if(is_symlink(s)) { copy_symlink(from, to, ec); } else if(is_directory(s)) { copy_directory(from, to, ec); } else if(is_regular_file(s)) { copy_file(from, to, fs::detail::copy_option::fail_if_exists, ec); } else { if (ec == 0) BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::copy", from, to, error_code(BOOST_ERROR_NOT_SUPPORTED, system_category()))); ec-&gt;assign(BOOST_ERROR_NOT_SUPPORTED, system_category()); } } </pre> </description> <category>Ticket</category> </item> </channel> </rss>