Bug 17450

Summary: resolve_dynamic_type_internal misunderstands check_typedef -> internal error
Product: gdb Reporter: dje
Component: gdbAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: jan
Priority: P2    
Version: HEAD   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description dje 2014-10-01 17:57:24 UTC
Clang is emitting no bounds info for the "x" in
class c { char x[0]; char more_x[42]; };

This causes has_static_range for "x" to return false (lower bound is defaulted to zero, upper bound is unknown), which in turn causes is_dynamic_type to return true and we enter resolve_dynamic_type (which is just a wrapper on resolve_dynamic_type_internal).

In the example at hand (I don't have an easy to provide testcase, at least not yet), the type is defined in another CU so what resolve_dynamic_type gets is an opaque type.  We then hit this assert in resolve_dynamic_struct and crash:

  gdb_assert (TYPE_NFIELDS (type) > 0);

resolve_dynamic_type_internal seems to not understand that check_typedef has (at least) two main purposes:

1) check for typedefs (duh ... :-)),
2) AND resolve opaque types.

It's easy to understand this happening given how poorly named check_typedef is.

static struct type *
resolve_dynamic_type_internal (struct type *type, CORE_ADDR addr,
                               int top_level)
{
  struct type *real_type = check_typedef (type);
  struct type *resolved_type = type;
  const struct dynamic_prop *prop;
  CORE_ADDR value;

  if (!is_dynamic_type_internal (real_type, top_level))
    return type;

  switch (TYPE_CODE (type))
    {

    ...

    case TYPE_CODE_STRUCT:
      resolved_type = resolve_dynamic_struct (type, addr);
      break;
    }

  ...
}

Note that we've called check_typedef, BUT we're passing type to resolve_dynamic_struct not real_type.  If type is an opaque type we in fact do want to pass real_type to resolve_dynamic_struct.

Either that or call check_typedef again in resolve_dynamic_struct.  Since this collection of functions is all just implementation detail for resolve_dynamic_type I'm ok with not sprinkling calls to check_typedef everywhere it might be needed (and in fact since the function is so badly named sprinkling it about doesn't improve readability).  It would be reasonable to create a resolve_opaque_type function that handles this part of what check_typedef does, and use that instead, but I'm ok with just passing real_type to resolve_dynamic_struct here.
Comment 1 dje 2014-11-27 00:23:38 UTC
See also PR 17642
Comment 2 Jan Kratochvil 2014-11-27 23:09:13 UTC
I do not see how this is clang specific.

echo 'class c { char x[0]; char more_x[42]; };'|clang++ -c -g -o o.out -x c++ -
gdb ./o.out

does not crash for me.  IMO there have to be at least two CUs to make there the stub type but then it is PR 17642.

*** This bug has been marked as a duplicate of bug 17642 ***
Comment 3 dje 2014-11-27 23:49:52 UTC
Ahem.
Just because I report a bug I'm seeing with clang does not mean I'm claiming the bug is clang specific.
Sheesh.