gdb: make extract_integer take an array_view

I think it would make sense for extract_integer, extract_signed_integer
and extract_unsigned_integer to take an array_view.  This way, when we
extract an integer, we can validate that we don't overflow the buffer
passed by the caller (e.g. ask to extract a 4-byte integer but pass a
2-byte buffer).

 - Change extract_integer to take an array_view
 - Add overloads of extract_signed_integer and extract_unsigned_integer
   that take array_views.  Keep the existing versions so we don't
   need to change all callers, but make them call the array_view
   versions.

This shortens some places like:

  result = extract_unsigned_integer (value_contents (result_val).data (),
				     TYPE_LENGTH (value_type (result_val)),
				     byte_order);

into

  result = extract_unsigned_integer (value_contents (result_val), byte_order);

value_contents returns an array view that is of length
`TYPE_LENGTH (value_type (result_val))` already, so the length is
implicitly communicated through the array view.

Change-Id: Ic1c1f98c88d5c17a8486393af316f982604d6c95
This commit is contained in:
Simon Marchi
2021-10-25 23:29:34 -04:00
parent 4bce7cdaf4
commit 2a50938ab7
14 changed files with 60 additions and 55 deletions

View File

@ -620,15 +620,12 @@ template<typename T, typename>
enum register_status
readable_regcache::raw_read (int regnum, T *val)
{
gdb_byte *buf;
enum register_status status;
assert_regnum (regnum);
buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
status = raw_read (regnum, buf);
size_t len = m_descr->sizeof_register[regnum];
gdb_byte *buf = (gdb_byte *) alloca (len);
register_status status = raw_read (regnum, buf);
if (status == REG_VALID)
*val = extract_integer<T> (buf,
m_descr->sizeof_register[regnum],
*val = extract_integer<T> ({buf, len},
gdbarch_byte_order (m_descr->gdbarch));
else
*val = 0;
@ -772,14 +769,12 @@ template<typename T, typename>
enum register_status
readable_regcache::cooked_read (int regnum, T *val)
{
enum register_status status;
gdb_byte *buf;
gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
status = cooked_read (regnum, buf);
size_t len = m_descr->sizeof_register[regnum];
gdb_byte *buf = (gdb_byte *) alloca (len);
register_status status = cooked_read (regnum, buf);
if (status == REG_VALID)
*val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
*val = extract_integer<T> ({buf, len},
gdbarch_byte_order (m_descr->gdbarch));
else
*val = 0;