[binutils-gdb] Avoid crash in resolve_dynamic_struct

Tom Tromey tromey@sourceware.org
Tue Feb 9 19:20:04 GMT 2021


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a4f0544b1ba516db0ab9715e4cccc78bc098ebc9

commit a4f0544b1ba516db0ab9715e4cccc78bc098ebc9
Author: Tom Tromey <tromey@adacore.com>
Date:   Tue Feb 9 12:15:39 2021 -0700

    Avoid crash in resolve_dynamic_struct
    
    resolve_dynamic_struct says:
    
      gdb_assert (type->num_fields () > 0);
    
    However, a certain Ada program has a structure with no fields but with
    a dynamic size, causing this assertion to fire.
    
    It is difficult to be certain, but we think this is a compiler bug.
    However, in the meantime this assertion does not seem to be checking
    any kind of internal consistency; so this patch removes it.
    
    gdb/ChangeLog
    2021-02-09  Tom Tromey  <tromey@adacore.com>
    
            * gdbtypes.c (resolve_dynamic_struct): Handle structure with no
            fields.

Diff:
---
 gdb/ChangeLog  | 5 +++++
 gdb/gdbtypes.c | 8 ++++----
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index e67668d315c..6470c996a30 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2021-02-09  Tom Tromey  <tromey@adacore.com>
+
+	* gdbtypes.c (resolve_dynamic_struct): Handle structure with no
+	fields.
+
 2021-02-08  Shahab Vahedi  <shahab@synopsys.com>
 
 	PR tdep/27369
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index c736dff2ca8..1b2d4836959 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -2545,7 +2545,6 @@ resolve_dynamic_struct (struct type *type,
   unsigned resolved_type_bit_length = 0;
 
   gdb_assert (type->code () == TYPE_CODE_STRUCT);
-  gdb_assert (type->num_fields () > 0);
 
   resolved_type = copy_type (type);
 
@@ -2564,9 +2563,10 @@ resolve_dynamic_struct (struct type *type,
 	((struct field *)
 	 TYPE_ALLOC (resolved_type,
 		     resolved_type->num_fields () * sizeof (struct field)));
-      memcpy (resolved_type->fields (),
-	      type->fields (),
-	      resolved_type->num_fields () * sizeof (struct field));
+      if (type->num_fields () > 0)
+	memcpy (resolved_type->fields (),
+		type->fields (),
+		resolved_type->num_fields () * sizeof (struct field));
     }
 
   for (i = 0; i < resolved_type->num_fields (); ++i)


More information about the Gdb-cvs mailing list