Bug 59224 - [4.7/4.8/4.9 Regression] std::uncaught_exception returns true while constructing exception.
Summary: [4.7/4.8/4.9 Regression] std::uncaught_exception returns true while construct...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.1
: P2 normal
Target Milestone: 4.7.4
Assignee: Jason Merrill
URL:
Keywords:
Depends on: 41174
Blocks:
  Show dependency treegraph
 
Reported: 2013-11-21 02:48 UTC by man2gm
Modified: 2015-05-12 10:54 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.5.1
Known to fail: 4.6.0, 4.7.4, 4.8.2, 4.9.0
Last reconfirmed: 2013-11-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description man2gm 2013-11-21 02:48:32 UTC
This code prints (erroneously)

Hello, world!
1

in g++ 4.8.1, g++ 4.6.3, but works correctly in g++ 4.5.2.

cat main.cpp 
#include <string>
#include <iostream>


struct Exception
{
        std::string what;
        Exception(const std::string & what_) : what(what_) {}
};


std::string f()
{
        throw Exception("Hello, world!");
}

void g()
{
        try
        {
                f();
        }
        catch (...)
        {
                throw Exception(f());
        }
}


int main(int argc, char ** argv)
{
        try
        {
                g();
        }
        catch (const Exception & e)
        {
                std::cerr << e.what << std::endl;
        }

        /// returns 1 in gcc 4.6.3, 4.8.1 - bug
        std::cerr << std::uncaught_exception() << std::endl;

        return 0;
}


# g++-4.8 --version
g++-4.8 (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
Copyright (C) 2013 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.

# g++-4.8 main.cpp
# ./a.out 
Hello, world!
1

# g++-4.6 --version
g++-4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 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.

# g++-4.6 main.cpp
# ./a.out 
Hello, world!
1

# g++-4.5 --version
g++-4.5 (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 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.

# g++-4.5 main.cpp
# ./a.out 
Hello, world!
0


http://ideone.com/lx2ao2
Comment 1 Jonathan Wakely 2013-11-21 14:02:53 UTC
Confirmed. uncaught_exception becomes true while evaluating the operand of the throw statement, but should only be true after initialization of the exception object is complete.

#include <exception>

int f()
{
  if (std::uncaught_exception())
    std::terminate();
  return 1;
}

int main()
{
  try
  {
    throw f();
  }
  catch (...)
  {
  }
}
Comment 2 Jonathan Wakely 2013-11-21 14:11:54 UTC
Possibly a dup of PR 41174 (although that was wrong prior to 4.6.0)
Comment 3 Jason Merrill 2014-01-27 13:58:11 UTC
Author: jason
Date: Mon Jan 27 13:57:39 2014
New Revision: 207129

URL: http://gcc.gnu.org/viewcvs?rev=207129&root=gcc&view=rev
Log:
	Core DR 475
	PR c++/41174
	PR c++/59224
	* libsupc++/eh_throw.cc (__cxa_throw): Set uncaughtExceptions.
	* libsupc++/eh_alloc.cc (__cxa_allocate_dependent_exception)
	(__cxa_allocate_exception): Don't set it here.

Added:
    trunk/gcc/testsuite/g++.dg/eh/uncaught4.C
Modified:
    trunk/gcc/testsuite/g++.dg/eh/uncaught1.C
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/libsupc++/eh_alloc.cc
    trunk/libstdc++-v3/libsupc++/eh_throw.cc
Comment 4 Jason Merrill 2014-01-27 13:59:21 UTC
Author: jason
Date: Mon Jan 27 13:58:48 2014
New Revision: 207131

URL: http://gcc.gnu.org/viewcvs?rev=207131&root=gcc&view=rev
Log:
	Core DR 475
	PR c++/41174
	PR c++/59224
	* libsupc++/eh_throw.cc (__cxa_throw): Set uncaughtExceptions.
	* libsupc++/eh_alloc.cc (__cxa_allocate_dependent_exception)
	(__cxa_allocate_exception): Don't set it here.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/g++.dg/eh/uncaught4.C
Modified:
    branches/gcc-4_8-branch/gcc/testsuite/g++.dg/eh/uncaught1.C
    branches/gcc-4_8-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_8-branch/libstdc++-v3/libsupc++/eh_alloc.cc
    branches/gcc-4_8-branch/libstdc++-v3/libsupc++/eh_throw.cc
Comment 5 Jason Merrill 2014-01-27 14:03:24 UTC
Fixed for 4.8.3/4.9.  Not fixing on 4.7 branch.
Comment 6 Jason Merrill 2014-04-01 17:29:01 UTC
Author: jason
Date: Tue Apr  1 17:28:29 2014
New Revision: 208991

URL: http://gcc.gnu.org/viewcvs?rev=208991&root=gcc&view=rev
Log:
	Core DR 475
	PR c++/41174
	PR c++/59224
	* libsupc++/eh_throw.cc (__cxa_throw): Set uncaughtExceptions.
	* libsupc++/eh_alloc.cc (__cxa_allocate_dependent_exception)
	(__cxa_allocate_exception): Don't set it here.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/g++.dg/eh/uncaught4.C
Modified:
    branches/gcc-4_7-branch/gcc/testsuite/g++.dg/eh/uncaught1.C
    branches/gcc-4_7-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_7-branch/libstdc++-v3/libsupc++/eh_alloc.cc
    branches/gcc-4_7-branch/libstdc++-v3/libsupc++/eh_throw.cc
Comment 7 Jason Merrill 2014-04-01 17:30:11 UTC
OK, fixed for 4.7.4 as well.
Comment 8 li xin 2015-05-12 08:13:23 UTC
(In reply to Jason Merrill from comment #5)
> Fixed for 4.8.3/4.9.  Not fixing on 4.7 branch.

It will lead to the lsb test caes /libstdcxx-t2c/tests/LanguageSupport/LanguageSupport FAIL.
So I want to know the right return value of std::uncaught_exception() (inside exception-constructor).

The LSB test code is as follows:

cat main.cpp
#include <string.h>
#include <iostream>

using namespace std;
class exception_test : public exception
{
        public:
                bool result;
                exception_test(): exception(){result = uncaught_exception();}
                ~exception_test() throw(){};
};

int main(int argc, char ** argv)
{
        bool result;

        try
        {
                throw exception_test();
        }
        catch(exception_test et)
        {
                result = et.result;
        }
        cerr << result << endl;
        return 0;
}

# g++ ./main.cpp
# ./a.out
0

My GCC version is 4.9.2, the return value of result is 0,and the test is FAIL.
If the the return value of result is 1,and the test will be PASS.
Comment 9 Jonathan Wakely 2015-05-12 10:54:57 UTC
(In reply to li xin from comment #8)
> It will lead to the lsb test caes
> /libstdcxx-t2c/tests/LanguageSupport/LanguageSupport FAIL.
> So I want to know the right return value of std::uncaught_exception()
> (inside exception-constructor).

What isn't clear about the bug report? The right value is the one G++ gives now.

The LSB test is wrong.