Bug 14365 - gdb crashes when checking ctor of lambdas (in is_ctor_or_dtor)
Summary: gdb crashes when checking ctor of lambdas (in is_ctor_or_dtor)
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.4
: P2 critical
Target Milestone: 7.6
Assignee: Keith Seitz
URL: https://bugs.launchpad.net/ubuntu/+so...
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-14 13:29 UTC by Marco Trevisan
Modified: 2012-08-19 19:40 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
gdb-fix-crash-when-checking-for-ctor-of-lambda by Thomas Voß (260 bytes, patch)
2012-07-14 13:29 UTC, Marco Trevisan
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Marco Trevisan 2012-07-14 13:29:50 UTC
Created attachment 6531 [details]
gdb-fix-crash-when-checking-for-ctor-of-lambda by Thomas Voß

Using GDB to debug this test case: http://pastebin.ubuntu.com/1062024/ causes it to crash in is_ctor_or_dtor.

Attached here patch by Thomas Voß that fixes it.

This is description:
«It turns out that gdb crashes as it tries to determine whether a field of a lambda is a or ctor/dtor, given the fields name. Apparently, the field's name is NULL and I added a check for that, bailing out and returning 0 ("no, this is neither a dtor nor a ctor"). GDB behaves normally when being run on the test program mentioned before. Patch is attached, generated with quilt.»

See launchpad bug http://pad.lv/1006860 for reference.
Comment 1 Jan Kratochvil 2012-07-15 12:55:24 UTC
Please post any patches to gdb-patches at sourceware.org.
Comment 2 Tom Tromey 2012-08-01 18:55:14 UTC
I reproduced the bug.

I don't think this is the correct fix.

What seems to be happening here is that we are trying to
compute the physname for a type that is a pointer to a member
function of an unnamed structure type.
Because it is unnamed, c_type_print_base falls through to
printing the body of the structure, instead of its name.
This then crashes.

Maybe NULL checks in is_constructor_name and is_destructor_name
would be ok; though I am not sure.  But even if those were there
we would still be seeing very weird results here.
Comment 3 Tom Tromey 2012-08-01 18:57:50 UTC
BTW I reproduced using the reported test case:

#include <sigc++/sigc++.h>

#if __cplusplus >= 201100L || defined (__GXX_EXPERIMENTAL_CXX0X__)
#include <type_traits>

namespace sigc
{
    template <typename Functor>
      struct functor_trait<Functor, false>
      {
        typedef decltype (::sigc::mem_fun(std::declval<Functor&>(), &Functor::operator())) _intermediate;
        typedef typename _intermediate::result_type result_type;
        typedef Functor functor_type;
      };
}
#endif


int main()
{
  sigc::slot <bool> slot3 = [] () -> bool { return true; };
  sigc::slot <bool, int> slot4 = [] (int) -> bool { return true; };
  while (1);
}


and building it on Fedora 16 with

g++ -std=c++0x -g -o pr pr.cc $(pkg-config --cflags sigc++-2.0) $(pkg-config --libs sigc++-2.0)

Simplest way to see it is "gdb -readnow pr"
Comment 4 Keith Seitz 2012-08-03 17:22:08 UTC
I think Tom is correct. IMO the questionable thing done here is asking c_type_print_base to print details of the type when we have a pointer member/method whose containing class/struct is anonymous (TYPE_NAME (type) == NULL). That just doesn't seem right.

I believe the proper fix is simply for c_type_print_varspec_prefix to honor the SHOW parameter when it is calling other c_type_print_* functions.

In this function, the only cases which do not pass SHOW to the other type printing functions are MEMBERPTR and METHODPTR, exactly where we are seeing problems.

I'm working on a test case for this now, and will submit a patch when it is finished. [It requires hand-written DWARF.]
Comment 5 Sourceware Commits 2012-08-19 19:37:57 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	kseitz@sourceware.org	2012-08-19 19:37:51

Modified files:
	gdb            : ChangeLog c-typeprint.c 
	gdb/testsuite  : ChangeLog 
Added files:
	gdb/testsuite/gdb.dwarf2: dw2-anon-mptr.exp dw2-anon-mptr.S 

Log message:
	PR c++/14365
	* c-typeprint.c (c_type_print_varspec_prefix): Pass
	-1 for SHOW to c_type_print_base for METHODPTR and MEMBERPTR.
	
	* gdb.dwarf2/dw2-anon-mptr.exp: New file.
	* gdb.dwarf2/dw2-anon-mptr.S: New file.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/ChangeLog.diff?cvsroot=src&r1=1.14602&r2=1.14603
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/c-typeprint.c.diff?cvsroot=src&r1=1.75&r2=1.76
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/ChangeLog.diff?cvsroot=src&r1=1.3343&r2=1.3344
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.dwarf2/dw2-anon-mptr.exp.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.dwarf2/dw2-anon-mptr.S.diff?cvsroot=src&r1=NONE&r2=1.1
Comment 6 Keith Seitz 2012-08-19 19:40:00 UTC
I have committed a patch to fix this. If there are any further problems, please let me know.