Bug 37477 - [Core 475] std::uncaught_exception() returns wrong value after entering terminate() in some cases
Summary: [Core 475] std::uncaught_exception() returns wrong value after entering termi...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-11 10:11 UTC by Andrey Tsyvarev
Modified: 2021-12-17 10:51 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-12-17 00:00:00


Attachments
test.tar (1.48 KB, text/plain)
2008-09-11 10:18 UTC, Andrey Tsyvarev
Details
Exhibits uncaught return values (224 bytes, text/x-c++src)
2011-06-15 14:15 UTC, Maxime van Noppen
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrey Tsyvarev 2008-09-11 10:11:25 UTC
The description of uncaught_exception() throw() function states (18.6.4):

Returns: true after completing evaluation of a throw-expression until either completing initialization of the exception-declaration in the matching handler or entering unexpected() due to the throw; or after entering terminate() for any reason other than an explicit call to terminate().

Apart from an explicit call to terminate() from the user's code, terminate() is called in the following situations (15.5.1):

1. When the exception handling mechanism, after completing evaluation of the expression to be thrown but before the exception is caught, calls a user function that exits via an uncaught exception.
2. When the exception handling mechanism cannot find a handler for a thrown exception.
3. When the destruction of an object during stack unwinding exits using an exception.
4. When construction or destruction of a non-local object with static storage duration exits using an exception.
5. When execution of a function registered with atexit exits using an exception.
6. When a throw-expression with no operand attempts to rethrow an exception and no exception is being handled.
7. When unexpected throws an exception which is not allowed by the previously violated exception specification, and std::bad_exception is not included in that exception-specification.

However std::uncaught_exception called within a terminate_handler returns false in the cases 2,4,5,7 and true in the other cases (terminate_handler is specified using std::set_terminate()).

The attached examples demonstrate the above 7 situations when terminate() is called "implicitly" and display the return value of std::uncaught_exception().

For example, reproducing situation 2:

andrew@Ubuntu:/mnt/hgfs/shared/temp/test$ g++ -Wall test2.cpp && ./a.out
Exception will be thrown and it won't be catched.
This situation meets the following condition when terminate should be called:
2.When the exception handling mechanism cannot find a handler
for a thrown exception.
Inside terminate_handler uncaught_exception() returns *false*.
terminate called after throwing an instance of 'char const*'
Aborted (core dumped)
andrew@Ubuntu:/mnt/hgfs/shared/temp/test$ g++ --version
g++ (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Comment 1 Andrey Tsyvarev 2008-09-11 10:18:05 UTC
Created attachment 16292 [details]
test.tar

Reproduce situations when terminate is called implicitly.
Comment 2 Paolo Carlini 2008-09-11 10:29:52 UTC
Note this is a libsupc++ issue, we don't have a specific Bugzilla component, but as a matter of fact the code has been mostly implemented and maintained by different people than the libstdc++ maintainers... I'm afraid it will take some time to review the issue and in case work on the code.
Comment 3 Paolo Carlini 2009-07-07 00:58:04 UTC
Richard, any chance you can look a bit into this? Many thanks in advance.
Comment 4 Paolo Carlini 2010-01-08 19:12:20 UTC
I'm trusting Jon here: in the audi trail of PR41174 he mentioned that this issue depends on the resolution of Core issue 475 (currently in drafting):

  http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#475

  
Comment 5 Paolo Carlini 2010-01-08 19:12:55 UTC
Suspending, waiting for [Ready].
Comment 6 Paolo Carlini 2010-04-21 20:46:01 UTC
The status is now [Ready], unsuspending.
Comment 7 Maxime van Noppen 2011-06-15 14:15:38 UTC
Created attachment 24538 [details]
Exhibits uncaught return values
Comment 8 Maxime van Noppen 2011-06-15 14:16:38 UTC
I just attached a testcase which seems likely related to this bug. Tested with g++ (GCC) 4.6.0 20110603 on x86_64 linux.