]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Check index in type::field
authorTom Tromey <tromey@adacore.com>
Mon, 18 Oct 2021 18:32:18 +0000 (12:32 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 19 Oct 2021 19:30:59 +0000 (13:30 -0600)
This changes gdb to check the index that is passed to type::field.
This caught one bug in the Ada code when running the test suite
(actually I found the bug first, then realized that the check would
have helped), so this patch fixes that as well.

Regression tested on x86-64 Fedora 34.

gdb/ada-lang.c
gdb/gdbtypes.c
gdb/gdbtypes.h

index 98718bcc98bf2f434c66240fa6854296b4fd4c84..935358d02459b31d01af8422f2ab0084de56471a 100644 (file)
@@ -7539,7 +7539,7 @@ ada_template_to_fixed_record_type_1 (struct type *type,
             that follow this one.  */
          if (ada_is_aligner_type (field_type))
            {
-             long field_offset = TYPE_FIELD_BITPOS (field_type, f);
+             long field_offset = TYPE_FIELD_BITPOS (type, f);
 
              field_valaddr = cond_offset_host (field_valaddr, field_offset);
              field_address = cond_offset_target (field_address, field_offset);
index 3110395b7cb25bb5e85a936897046387aede20d3..b39d2b72348bec9d50734ee3409b91725d252459 100644 (file)
@@ -5842,11 +5842,11 @@ append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
   gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
   gdb_assert (name != NULL);
 
+  type->set_num_fields (type->num_fields () + 1);
   type->field (field_nr).set_name (xstrdup (name));
   type->field (field_nr).set_type (field_type);
   type->field (field_nr).set_loc_bitpos (start_bitpos);
   TYPE_FIELD_BITSIZE (type, field_nr) = nr_bits;
-  type->set_num_fields (type->num_fields () + 1);
 }
 
 /* Special version of append_flags_type_field to add a flag field.
index dc575c429967839c7510e70019fea70da6c71cfd..4324641eb0d573b1f453e4ef3def1733678bd423 100644 (file)
@@ -1047,6 +1047,7 @@ struct type
   /* Get the field at index IDX.  */
   struct field &field (int idx) const
   {
+    gdb_assert (idx >= 0 && idx < num_fields ());
     return this->fields ()[idx];
   }