[Bug c++/52099] Incorrectly applying conversion when catching pointer-to-members

2021-08-09 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52099

--- Comment #4 from Jonathan Wakely  ---
Further discussion from PR 67772:

Consider:

  namespace std { struct type_info {}; }
  struct A {};
  auto x = typeid(void(A::*)() const);

Clang emits the type info as:

_ZTIM1AKFvvE:
.quad   _ZTVN10__cxxabiv129__pointer_to_member_type_infoE+16
.quad   _ZTSM1AKFvvE
.long   0   # 0x0
.zero   4
.quad   _ZTIKFvvE
.quad   _ZTI1A

GCC emits it as:

_ZTIM1AKFvvE:
.quad   _ZTVN10__cxxabiv129__pointer_to_member_type_infoE+16
.quad   _ZTSM1AKFvvE
.long   0
.zero   4
.quad   _ZTIFvvE
.quad   _ZTI1A

It appears that Clang is correct here; the 'const' in this case is not a
qualifier, so should not be removed when forming the pointee type_info. If GCC
really did think this was a const qualifier applied to a function type, it
would be emitting the wrong flags (should be .long 1, not .long 0 in that
case).


This translates into a wrong-code bug in a case like this:

struct A;
extern "C" void puts(const char*);
int main() {
  try {
throw (void(A::*)())0;
  } catch (void (A::*)() const) {
puts("bad catch");
  }
}

... where GCC erroneously catches a pointer to non-const member function as a
pointer to const member function.

[Bug c++/52099] Incorrectly applying conversion when catching pointer-to-members

2021-08-09 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52099

Andrew Pinski  changed:

   What|Removed |Added

 CC||richard-gccbugzilla@metafoo
   ||.co.uk

--- Comment #3 from Andrew Pinski  ---
*** Bug 67772 has been marked as a duplicate of this bug. ***

[Bug c++/52099] Incorrectly applying conversion when catching pointer-to-members

2021-07-25 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52099

--- Comment #2 from Jonathan Wakely  ---
>From the dup:


 Eric Fiselier 2016-01-20 03:50:56 UTC

Created attachment 37399 [details]
reproducer

I don't see where [except.handle] allows such a conversion.

Comment 1 Jonathan Wakely 2017-01-13 20:36:35 UTC

We're missing a check for cv-qualifiers in
__pointer_to_member_type_info::__pointer_catch that needs to be done before we
compare the pointees. Both pointees have type void() so we need to compare the
cv-quals before that info is lost.

Comment 2 Jonathan Wakely 2017-01-13 20:49:13 UTC

Hmm, we don't seem to have the cv-quals in __flags. That's a problem.

Comment 3 Jonathan Wakely 2017-01-13 21:08:10 UTC

When compiled with clang the pointees are different, so the match fails when
comparing them.

Using Clang:

(gdb) step
__cxxabiv1::__pbase_type_info::__pointer_catch (this=0x401cc0 , thrown_type=0x401d10 ,
thr_obj=0x7fffd220, outer=0)
at
/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../include/c++/6.3.1/cxxabi.h:309
(gdb) step
std::type_info::__do_catch (this=0x401c90 ,
thr_type=0x401cf8 ) at
../../../../libstdc++-v3/libsupc++/tinfo.cc:71
(gdb) p *this
$3 = {_vptr.type_info = 0x6030b0 , __name = 0x401c89  "KFvvE"}
(gdb) p *thr_type
$4 = {_vptr.type_info = 0x6030b0 , __name = 0x401cf0  "FvvE"}
(gdb) 


But using GCC the two pointee types are the same:

(gdb) p *this
$1 = {_vptr.type_info = 0x6030e8 , __name = 0x401c50  "FvvE"}
(gdb) p *thr_type
$2 = {_vptr.type_info = 0x6030e8 , __name = 0x401c50  "FvvE"}

So it looks like the problem is in the front-end where the typeinfo object for
a pointer to cv-qualified member function has the wrong pointee type.

Comment 4 Jonathan Wakely 2017-01-13 23:05:34 UTC

My front-end debugging skills are pitiful, but I've found something suspicious.
ptm_initializer uses TYPE_PTRMEM_POINTED_TO_TYPE to get that pointee type. For
this case that expands to TYPE_PTRMEMFUNC_FN_TYPE which is a call to
cp_build_qualified_type with the qualifiers from cp_type_quals.

But cp_type_quals tries pretty hard to ensure we never get cv-quals for a
function type. For the purposes of RTTI, where we really do care about the
difference between void() and void()const, do we want the memfn quals instead?

Comment 5 Jonathan Wakely 2017-01-13 23:20:33 UTC

For the attached reproducer this condition is never true in
cp_build_qualified_type_real

  /* But preserve any function-cv-quals on a FUNCTION_TYPE.  */
  if (TREE_CODE (type) == FUNCTION_TYPE)
type_quals |= type_memfn_quals (type);

As far as I can tell this is what's supposed to put the cv-quals back onto the
function type, so we'd have a pointee of type void() const not void().

[Bug c++/52099] Incorrectly applying conversion when catching pointer-to-members

2021-07-23 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52099

Andrew Pinski  changed:

   What|Removed |Added

 CC||eric at efcs dot ca

--- Comment #1 from Andrew Pinski  ---
*** Bug 69375 has been marked as a duplicate of this bug. ***

[Bug c++/52099] Incorrectly applying conversion when catching pointer-to-members

2015-03-25 Thread paolo.carlini at oracle dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52099

Paolo Carlini paolo.carlini at oracle dot com changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2015-03-25
 Ever confirmed|0   |1