Bug 103630 - [9 Regression] std::make_exception_ptr<T&>(t) is ill-formed
Summary: [9 Regression] std::make_exception_ptr<T&>(t) is ill-formed
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 12.0
: P2 normal
Target Milestone: 9.5
Assignee: Jonathan Wakely
URL:
Keywords: rejects-valid, wrong-code
Depends on:
Blocks:
 
Reported: 2021-12-09 13:36 UTC by Jonathan Wakely
Modified: 2022-05-09 16:50 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 6.5.0
Known to fail: 10.3.0, 11.2.0, 12.0, 7.1.0, 9.4.0
Last reconfirmed: 2021-12-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2021-12-09 13:36:36 UTC
I'm not 100% this is valid, but it used to work before the changes for PR 68297:

#include <exception>
int i = 0;
auto p = std::make_exception_ptr<int&>(i);

It fails now because the new implementation uses the template argument as `_Ex*` and as `new _Ex` and those aren't valid for a reference type.

And if we fix that, there's another problem which is that it uses typeid(__ex) which means we create an exception using the static type of the argument, but which matches a handler of the dynamic type:

#include <exception>
#include <stdlib.h>

struct B { virtual ~B() = default; };
struct D : B { };

int main()
{
  try {
    D d;
    auto p = std::make_exception_ptr<B&>(d);
    std::rethrow_exception(p);
  } catch (const D&) {
    abort();
  } catch (const B&) {
  }
}

This throws a B but catches a D.
Comment 1 Jonathan Wakely 2021-12-09 13:53:04 UTC
Also, since the changes for PR 90295 <bits/exception_ptr.h> is no longer usable in C++98:

include/bits/exception_ptr.h:182: error: declaration of 'std::__exception_ptr::exception_ptr::exception_ptr()' has a different exception specifier
include/bits/exception_ptr.h:107: note: from previous declaration 'std::__exception_ptr::exception_ptr::exception_ptr() throw ()'
include/bits/exception_ptr.h:188: error: declaration of 'std::__exception_ptr::exception_ptr::exception_ptr(const std::__exception_ptr::exception_ptr&)' has a different exception specifier
include/bits/exception_ptr.h:109: note: from previous declaration 'std::__exception_ptr::exception_ptr::exception_ptr(const std::__exception_ptr::exception_ptr&) throw ()'
Comment 2 GCC Commits 2021-12-09 23:29:35 UTC
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:a1ca039fc0fe934ef36c25d8284e6e116bcaffa7

commit r12-5879-ga1ca039fc0fe934ef36c25d8284e6e116bcaffa7
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Dec 9 13:54:39 2021 +0000

    libstdc++: Fix std::exception_ptr regressions [PR103630]
    
    This restores support for std::make_exception_ptr<E&> and for using
    std::exception_ptr in C++98.
    
    Because the new non-throwing implementation needs to use std::decay to
    handle references the original throwing implementation is used for
    C++98.
    
    We also need to change the typeid expression so it doesn't yield the
    dynamic type when the function parameter is a reference to a polymorphic
    type. Otherwise the new exception object could be caught by any handler
    matching the dynamic type, even though the actual exception object is
    only a copy of the base class, sliced to the static type.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/103630
            * libsupc++/exception_ptr.h (exception_ptr): Fix exception
            specifications on inline definitions.
            (make_exception_ptr): Decay the template parameter. Use typeid
            of the static type.
            * testsuite/18_support/exception_ptr/103630.cc: New test.
Comment 3 Jonathan Wakely 2021-12-09 23:33:23 UTC
Fixed on trunk so far.
Comment 4 GCC Commits 2022-04-12 20:18:20 UTC
The releases/gcc-11 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:84e2410c8d10d7f3ca61c3063418ace8dd8f9e35

commit r11-9835-g84e2410c8d10d7f3ca61c3063418ace8dd8f9e35
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Dec 9 13:54:39 2021 +0000

    libstdc++: Fix std::exception_ptr regressions [PR103630]
    
    This restores support for std::make_exception_ptr<E&> and for using
    std::exception_ptr in C++98.
    
    Because the new non-throwing implementation needs to use std::decay to
    handle references the original throwing implementation is used for
    C++98.
    
    We also need to change the typeid expression so it doesn't yield the
    dynamic type when the function parameter is a reference to a polymorphic
    type. Otherwise the new exception object could be caught by any handler
    matching the dynamic type, even though the actual exception object is
    only a copy of the base class, sliced to the static type.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/103630
            * libsupc++/exception_ptr.h (exception_ptr): Fix exception
            specifications on inline definitions.
            (make_exception_ptr): Decay the template parameter. Use typeid
            of the static type.
            * testsuite/18_support/exception_ptr/103630.cc: New test.
    
    (cherry picked from commit a1ca039fc0fe934ef36c25d8284e6e116bcaffa7)
Comment 5 Jonathan Wakely 2022-04-12 20:30:26 UTC
Fixed for 11.3 too.
Comment 6 GCC Commits 2022-04-26 13:12:42 UTC
The releases/gcc-10 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:02b601ad7ea06460e7bc42d7251e895d9031bc6e

commit r10-10570-g02b601ad7ea06460e7bc42d7251e895d9031bc6e
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Dec 9 13:54:39 2021 +0000

    libstdc++: Fix std::exception_ptr regressions [PR103630]
    
    This restores support for std::make_exception_ptr<E&> and for using
    std::exception_ptr in C++98.
    
    Because the new non-throwing implementation needs to use std::decay to
    handle references the original throwing implementation is used for
    C++98.
    
    We also need to change the typeid expression so it doesn't yield the
    dynamic type when the function parameter is a reference to a polymorphic
    type. Otherwise the new exception object could be caught by any handler
    matching the dynamic type, even though the actual exception object is
    only a copy of the base class, sliced to the static type.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/103630
            * libsupc++/exception_ptr.h (make_exception_ptr): Decay the
            template parameter. Use typeid of the static type.
            * testsuite/18_support/exception_ptr/103630.cc: New test.
    
    (cherry picked from commit a1ca039fc0fe934ef36c25d8284e6e116bcaffa7)
Comment 7 Jonathan Wakely 2022-04-26 13:14:24 UTC
And 10.4 now.
Comment 8 GCC Commits 2022-05-09 16:40:37 UTC
The releases/gcc-9 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:93a621991615e943704ed1a957d934736bf8a3d3

commit r9-10063-g93a621991615e943704ed1a957d934736bf8a3d3
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Dec 9 13:54:39 2021 +0000

    libstdc++: Fix std::exception_ptr regressions [PR103630]
    
    This restores support for std::make_exception_ptr<E&> and for using
    std::exception_ptr in C++98.
    
    Because the new non-throwing implementation needs to use std::decay to
    handle references the original throwing implementation is used for
    C++98.
    
    We also need to change the typeid expression so it doesn't yield the
    dynamic type when the function parameter is a reference to a polymorphic
    type. Otherwise the new exception object could be caught by any handler
    matching the dynamic type, even though the actual exception object is
    only a copy of the base class, sliced to the static type.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/103630
            * libsupc++/exception_ptr.h (make_exception_ptr): Decay the
            template parameter. Use typeid of the static type.
            * testsuite/18_support/exception_ptr/103630.cc: New test.
    
    (cherry picked from commit a1ca039fc0fe934ef36c25d8284e6e116bcaffa7)
Comment 9 Jonathan Wakely 2022-05-09 16:50:20 UTC
And 9.5 too.