diff options
author | Simon Marchi <simon.marchi@polymtl.ca> | 2021-10-04 20:47:06 -0400 |
---|---|---|
committer | Simon Marchi <simon.marchi@polymtl.ca> | 2021-10-25 14:51:44 -0400 |
commit | 50888e42dcd32b30e1144c0aa6d1c1490da45cd9 (patch) | |
tree | 72fad89d67057ecb53f26bac0464542829053e3e /gdb/valprint.c | |
parent | d9f82e931394efed68858eb7c7bb5832ad888482 (diff) | |
download | gdb-50888e42dcd32b30e1144c0aa6d1c1490da45cd9.zip gdb-50888e42dcd32b30e1144c0aa6d1c1490da45cd9.tar.gz gdb-50888e42dcd32b30e1144c0aa6d1c1490da45cd9.tar.bz2 |
gdb: change functions returning value contents to use gdb::array_view
The bug fixed by this [1] patch was caused by an out-of-bounds access to
a value's content. The code gets the value's content (just a pointer)
and then indexes it with a non-sensical index.
This made me think of changing functions that return value contents to
return array_views instead of a plain pointer. This has the advantage
that when GDB is built with _GLIBCXX_DEBUG, accesses to the array_view
are checked, making bugs more apparent / easier to find.
This patch changes the return types of these functions, and updates
callers to call .data() on the result, meaning it's not changing
anything in practice. Additional work will be needed (which can be done
little by little) to make callers propagate the use of array_view and
reap the benefits.
[1] https://sourceware.org/pipermail/gdb-patches/2021-September/182306.html
Change-Id: I5151f888f169e1c36abe2cbc57620110673816f3
Diffstat (limited to 'gdb/valprint.c')
-rw-r--r-- | gdb/valprint.c | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/gdb/valprint.c b/gdb/valprint.c index 485b5b9..6eb3db2 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -484,7 +484,7 @@ generic_value_print_ptr (struct value *val, struct ui_file *stream, { struct type *type = check_typedef (value_type (val)); struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type)); - const gdb_byte *valaddr = value_contents_for_printing (val); + const gdb_byte *valaddr = value_contents_for_printing (val).data (); CORE_ADDR addr = unpack_pointer (type, valaddr); print_unpacked_pointer (type, elttype, addr, stream, options); @@ -520,7 +520,7 @@ get_value_addr_contents (struct value *deref_val) gdb_assert (deref_val != NULL); if (value_lval_const (deref_val) == lval_memory) - return value_contents_for_printing_const (value_addr (deref_val)); + return value_contents_for_printing_const (value_addr (deref_val)).data (); else { /* We have a non-addressable value, such as a DW_AT_const_value. */ @@ -545,7 +545,7 @@ generic_val_print_ref (struct type *type, const int must_coerce_ref = ((options->addressprint && value_is_synthetic) || options->deref_ref); const int type_is_defined = elttype->code () != TYPE_CODE_UNDEF; - const gdb_byte *valaddr = value_contents_for_printing (original_value); + const gdb_byte *valaddr = value_contents_for_printing (original_value).data (); if (must_coerce_ref && type_is_defined) { @@ -693,7 +693,7 @@ generic_val_print_enum (struct type *type, gdb_assert (!options->format); - const gdb_byte *valaddr = value_contents_for_printing (original_value); + const gdb_byte *valaddr = value_contents_for_printing (original_value).data (); val = unpack_long (type, valaddr + embedded_offset * unit_size); @@ -740,7 +740,7 @@ generic_value_print_bool } else { - const gdb_byte *valaddr = value_contents_for_printing (value); + const gdb_byte *valaddr = value_contents_for_printing (value).data (); struct type *type = check_typedef (value_type (value)); LONGEST val = unpack_long (type, valaddr); if (val == 0) @@ -783,7 +783,7 @@ generic_value_print_char (struct value *value, struct ui_file *stream, { struct type *unresolved_type = value_type (value); struct type *type = check_typedef (unresolved_type); - const gdb_byte *valaddr = value_contents_for_printing (value); + const gdb_byte *valaddr = value_contents_for_printing (value).data (); LONGEST val = unpack_long (type, valaddr); if (type->is_unsigned ()) @@ -804,7 +804,7 @@ generic_val_print_float (struct type *type, struct ui_file *stream, { gdb_assert (!options->format); - const gdb_byte *valaddr = value_contents_for_printing (original_value); + const gdb_byte *valaddr = value_contents_for_printing (original_value).data (); print_floating (valaddr, type, stream); } @@ -821,7 +821,7 @@ generic_val_print_fixed_point (struct value *val, struct ui_file *stream, { struct type *type = value_type (val); - const gdb_byte *valaddr = value_contents_for_printing (val); + const gdb_byte *valaddr = value_contents_for_printing (val).data (); gdb_mpf f; f.read_fixed_point (gdb::make_array_view (valaddr, TYPE_LENGTH (type)), @@ -867,7 +867,7 @@ generic_value_print_memberptr /* Member pointers are essentially specific to C++, and so if we encounter one, we should print it according to C++ rules. */ struct type *type = check_typedef (value_type (val)); - const gdb_byte *valaddr = value_contents_for_printing (val); + const gdb_byte *valaddr = value_contents_for_printing (val).data (); cp_print_class_member (valaddr, type, stream, "&"); } else @@ -977,7 +977,7 @@ generic_value_print (struct value *val, struct ui_file *stream, int recurse, break; case TYPE_CODE_METHODPTR: - cplus_print_method_ptr (value_contents_for_printing (val), type, + cplus_print_method_ptr (value_contents_for_printing (val).data (), type, stream); break; @@ -1193,7 +1193,7 @@ static void val_print_type_code_flags (struct type *type, struct value *original_value, int embedded_offset, struct ui_file *stream) { - const gdb_byte *valaddr = (value_contents_for_printing (original_value) + const gdb_byte *valaddr = (value_contents_for_printing (original_value).data () + embedded_offset); ULONGEST val = unpack_long (type, valaddr); int field, nfields = type->num_fields (); @@ -1267,7 +1267,7 @@ value_print_scalar_formatted (struct value *val, /* value_contents_for_printing fetches all VAL's contents. They are needed to check whether VAL is optimized-out or unavailable below. */ - const gdb_byte *valaddr = value_contents_for_printing (val); + const gdb_byte *valaddr = value_contents_for_printing (val).data (); /* A scalar object that does not have all bits available can't be printed, because all bits contribute to its representation. */ @@ -3155,7 +3155,7 @@ test_print_flags (gdbarch *arch) append_flags_type_field (flags_type, 5, 3, field_type, "C"); value *val = allocate_value (flags_type); - gdb_byte *contents = value_contents_writeable (val); + gdb_byte *contents = value_contents_writeable (val).data (); store_unsigned_integer (contents, 4, gdbarch_byte_order (arch), 0xaa); string_file out; |