public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Check index in type::field
@ 2021-10-18 19:35 Tom Tromey
  2021-10-18 19:46 ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2021-10-18 19:35 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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 | 2 +-
 gdb/gdbtypes.c | 2 +-
 gdb/gdbtypes.h | 1 +
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 98718bcc98b..935358d0245 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -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);
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index de73a2b5608..2691fabc337 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -5841,11 +5841,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.
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index dc575c42996..4324641eb0d 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -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];
   }
 
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Check index in type::field
  2021-10-18 19:35 [PATCH] Check index in type::field Tom Tromey
@ 2021-10-18 19:46 ` Simon Marchi
  2021-10-19 19:43   ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2021-10-18 19:46 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 2021-10-18 3:35 p.m., Tom Tromey via Gdb-patches wrote:
> 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.

I think that's a good idea, this LGTM.

Simon


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Check index in type::field
  2021-10-18 19:46 ` Simon Marchi
@ 2021-10-19 19:43   ` Tom Tromey
  0 siblings, 0 replies; 3+ messages in thread
From: Tom Tromey @ 2021-10-19 19:43 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, gdb-patches

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> On 2021-10-18 3:35 p.m., Tom Tromey via Gdb-patches wrote:
>> 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.

Simon> I think that's a good idea, this LGTM.

Thanks, I'm going to push this.

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-10-19 19:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-18 19:35 [PATCH] Check index in type::field Tom Tromey
2021-10-18 19:46 ` Simon Marchi
2021-10-19 19:43   ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).