[PATCH] Fix std::uncaught_exception() to return true during exception initialization

Mark Seaborn mseaborn at chromium.org
Mon Dec 9 13:42:56 PST 2013


Hi howard.hinnant,

Fix std::uncaught_exception() to return true during exception initialization

std::uncaught_exception() should return true during the call to the
copy constructor that "throw X" does to initialize the exception
object after the expression X has been evaluated (or during the call
to the normal constructor if the compiler has done copy elision).

Fix this by incrementing uncaughtExceptions earlier, in
__cxa_allocate_exception() rather than in __cxa_throw().

This fixes PR18193.


http://llvm-reviews.chandlerc.com/D2366

Files:
  src/cxa_exception.cpp

Index: src/cxa_exception.cpp
===================================================================
--- src/cxa_exception.cpp
+++ src/cxa_exception.cpp
@@ -160,6 +160,11 @@
     if (NULL == exception_header)
         std::terminate();
     std::memset(exception_header, 0, actual_size);
+    // Increment uncaughtExceptions now so that
+    // std::uncaught_exception() returns true in the copy constructor
+    // that "throw" calls to initialize the exception.  The increment
+    // here is not atomic because the globals are thread-local.
+    __cxa_get_globals()->uncaughtExceptions += 1;
     return thrown_object_from_cxa_exception(exception_header);
 }
 
@@ -229,7 +234,6 @@
     exception_header->exceptionDestructor = dest;
     setExceptionClass(&exception_header->unwindHeader);
     exception_header->referenceCount = 1;  // This is a newly allocated exception, no need for thread safety.
-    globals->uncaughtExceptions += 1;   // Not atomically, since globals are thread-local
 
     exception_header->unwindHeader.exception_cleanup = exception_cleanup_func;
 #if __arm__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2366.1.patch
Type: text/x-patch
Size: 1085 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131209/052ed289/attachment.bin>


More information about the cfe-commits mailing list