Skip to content

Commit ad3c631

Browse files
author
Sergio Durigan Junior
committedJul 11, 2018
Fix PR c++/23373: GDB hangs when printing a struct with a static member of itself
This patch fixes a failure that happens when a structure has a static member whose type is the same as itself. From the bug report: Example code: struct A { static A Empty; int a; }; int main(void) { A a; return 0; } Output: (gdb) ptype/o A /* offset | size */ type = struct A { static struct A { static struct A { static struct A { static struct A { static struct A { static struct A { ... # infinite loop The problem here is that GDB is not taking into account the fact that static members inside a class/struct are not stored in the class/struct, and therefore they should not be accounted for during the display of the offsets/sizes. The fix is simple: we just check if the field we're dealing with (on c-typeprint.c:c_type_print_base_struct_union) is static, and if it is then we don't iterate over it. This patch also adds a new test for this case, and doesn't introduce any regressions. I believe it is important enough to be included in the 8.2 branch. OK? gdb/ChangeLog: 2018-07-11 Sergio Durigan Junior <sergiodj@redhat.com> PR c++/23373 * c-typeprint.c (c_type_print_base_struct_union): Don't print offsets/sizes for static members of a class/struct. gdb/testsuite/ChangeLog: 2018-07-11 Sergio Durigan Junior <sergiodj@redhat.com> PR c++/23373 * gdb.base/ptype-offsets.cc (struct static_member): New struct. (main) <stmember>: New variable. * gdb.base/ptype-offsets.exp: Add test for printing a struct with a static member in it.
1 parent 2b34c21 commit ad3c631

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed
 

‎gdb/ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2018-07-11 Sergio Durigan Junior <sergiodj@redhat.com>
2+
3+
PR c++/23373
4+
* c-typeprint.c (c_type_print_base_struct_union): Don't print
5+
offsets/sizes for static members of a class/struct.
6+
17
2018-07-07 Jan Kratochvil <jan.kratochvil@redhat.com>
28

39
* contrib/gdb-add-index.sh ($dwarf5): New, use it.

‎gdb/c-typeprint.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
11681168

11691169
int newshow = show - 1;
11701170

1171-
if (flags->print_offsets
1171+
if (!is_static && flags->print_offsets
11721172
&& (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_STRUCT
11731173
|| TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_UNION))
11741174
{

‎gdb/testsuite/ChangeLog

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2018-07-11 Sergio Durigan Junior <sergiodj@redhat.com>
2+
3+
PR c++/23373
4+
* gdb.base/ptype-offsets.cc (struct static_member): New
5+
struct.
6+
(main) <stmember>: New variable.
7+
* gdb.base/ptype-offsets.exp: Add test for printing a struct
8+
with a static member in it.
9+
110
2018-07-04 Tom de Vries <tdevries@suse.de>
211

312
* gdb.dwarf2/dw2-error.exp: Update expected error message.

‎gdb/testsuite/gdb.base/ptype-offsets.cc

+8
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ struct asd
177177
void *f16;
178178
};
179179

180+
/* See PR c++/23373. */
181+
182+
struct static_member
183+
{
184+
static static_member Empty;
185+
int abc;
186+
};
180187

181188
int
182189
main (int argc, char *argv[])
@@ -188,6 +195,7 @@ main (int argc, char *argv[])
188195
struct tyu e;
189196
struct asd f;
190197
uint8_t i;
198+
static_member stmember;
191199

192200
return 0;
193201
}

‎gdb/testsuite/gdb.base/ptype-offsets.exp

+11
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,14 @@ gdb_test_multiple "$test" "$test" {
328328
pass $test
329329
}
330330
}
331+
332+
# Test that printing a struct with a static member of itself doesn't
333+
# get us into an infinite loop.
334+
gdb_test "ptype/o static_member" \
335+
[multi_line \
336+
{/\* offset | size \*/ type = struct static_member \{} \
337+
{ static static_member Empty;} \
338+
{\/* 0 | 4 \*/ int abc;} \
339+
{} \
340+
{ /\* total size \(bytes\): 4 \*/} \
341+
{ \}}]

0 commit comments

Comments
 (0)