public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: fix nr_bits gdb_assert in append_flags_type_field
@ 2021-07-23  4:32 Simon Marchi
  2021-07-29 20:04 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2021-07-23  4:32 UTC (permalink / raw)
  To: gdb-patches

The assertion

    gdb_assert (nr_bits >= 1 && nr_bits <= type_bitsize);

is not correct.  Well, it's correct in that we do want the number of
bits to be in the range [1, type_bitsize].  But we don't check anywhere
that the end of the specified flag is within the containing type.

The following code should generate a failed assertion, as the flag goes
past the 32 bits of the underlying type, but it's currently not caught:

    static void
    test_print_flag (gdbarch *arch)
    {
      type *flags_type = arch_flags_type (arch, "test_type", 32);
      type *field_type = builtin_type (arch)->builtin_uint32;
      append_flags_type_field (flags_type, 31, 2, field_type, "invalid");
    }

(You can test this by registering it as a selftest using
selftests::register_test_foreach_arc and running.)

Change the assertion to verify that the end bit is within the range of
the underlying type.  This implicitly verifies that nr_bits is not
too big as well, so we don't need a separate assertion for that.

Change-Id: I9be79e5fd7a5917bf25b03b598727e6274c892e8
Co-Authored-By: Tony Tye <Tony.Tye@amd.com>
---
 gdb/gdbtypes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 1a261719422a..85c9df0e2c6d 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -5825,7 +5825,7 @@ append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
   gdb_assert (type->code () == TYPE_CODE_FLAGS);
   gdb_assert (type->num_fields () + 1 <= type_bitsize);
   gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
-  gdb_assert (nr_bits >= 1 && nr_bits <= type_bitsize);
+  gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
   gdb_assert (name != NULL);
 
   TYPE_FIELD_NAME (type, field_nr) = xstrdup (name);
-- 
2.32.0


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

* Re: [PATCH] gdb: fix nr_bits gdb_assert in append_flags_type_field
  2021-07-23  4:32 [PATCH] gdb: fix nr_bits gdb_assert in append_flags_type_field Simon Marchi
@ 2021-07-29 20:04 ` Tom Tromey
  2021-07-30  1:59   ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2021-07-29 20:04 UTC (permalink / raw)
  To: Simon Marchi via Gdb-patches

>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:

Simon> -  gdb_assert (nr_bits >= 1 && nr_bits <= type_bitsize);
Simon> +  gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);

This looks good to me.

Tom

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

* Re: [PATCH] gdb: fix nr_bits gdb_assert in append_flags_type_field
  2021-07-29 20:04 ` Tom Tromey
@ 2021-07-30  1:59   ` Simon Marchi
  0 siblings, 0 replies; 3+ messages in thread
From: Simon Marchi @ 2021-07-30  1:59 UTC (permalink / raw)
  To: Tom Tromey, Simon Marchi via Gdb-patches



On 2021-07-29 4:04 p.m., Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Simon> -  gdb_assert (nr_bits >= 1 && nr_bits <= type_bitsize);
> Simon> +  gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
> 
> This looks good to me.
> 
> Tom
> 

Thanks, pushed.

Simon

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

end of thread, other threads:[~2021-07-30  1:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-23  4:32 [PATCH] gdb: fix nr_bits gdb_assert in append_flags_type_field Simon Marchi
2021-07-29 20:04 ` Tom Tromey
2021-07-30  1:59   ` Simon Marchi

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).