From a12a15e7c5ff9f3549dd185fa1acd5a9dddd001b Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Wed, 12 May 2021 13:44:06 +0100 Subject: gdb: handle case where type alignment is unknown It was spotted that if type_align returned 0 then it was possible to trigger a divide by zero exception within GDB. It turns out this will only happen in an edge case where GDB is unable to figure out the alignment of a field within a structure. The attached test generates some non-standard, probably broken, DWARF, that triggers this condition, and then fixes this issue by throwing an exception when this case occurs. gdb/ChangeLog: PR gdb/27847 * amd64-tdep.c (amd64_has_unaligned_fields): Move call to type_align, and spot case where the alignment is unknown. gdb/testsuite/ChangeLog: PR gdb/27847 * gdb.dwarf2/dw2-weird-type-len.c: New file. * gdb.dwarf2/dw2-weird-type-len.exp: New file. --- gdb/amd64-tdep.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'gdb/amd64-tdep.c') diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c index 91305dd..3afac3c 100644 --- a/gdb/amd64-tdep.c +++ b/gdb/amd64-tdep.c @@ -554,7 +554,6 @@ amd64_has_unaligned_fields (struct type *type) { struct type *subtype = check_typedef (type->field (i).type ()); int bitpos = TYPE_FIELD_BITPOS (type, i); - int align = type_align(subtype); /* Ignore static fields, empty fields (for example nested empty structures), and bitfields (these are handled by @@ -568,6 +567,10 @@ amd64_has_unaligned_fields (struct type *type) if (bitpos % 8 != 0) return true; + int align = type_align (subtype); + if (align == 0) + error (_("could not determine alignment of type")); + int bytepos = bitpos / 8; if (bytepos % align != 0) return true; -- cgit v1.1