summaryrefslogtreecommitdiff
path: root/gdb/block.h
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2022-04-28 11:37:51 +0100
committerAndrew Burgess <aburgess@redhat.com>2022-04-28 15:09:50 +0100
commitc42dd30d73ec441ed9cab207597c7f5ce88ee231 (patch)
tree226451dbeee4300742b09e95452a4158d3f3d84f /gdb/block.h
parentd942d8db12adf4c9e5c7d9ed6496a779ece7149e (diff)
downloadbinutils-gdb-c42dd30d73ec441ed9cab207597c7f5ce88ee231.tar.gz
gdb: fix nullptr dereference in block::ranges()
This commit: commit f5cb8afdd297dd68273d98a10fbfd350dff918d8 Date: Sun Feb 6 22:27:53 2022 -0500 gdb: remove BLOCK_RANGES macro introduces a potential nullptr dereference in block::ranges, this is breaking most tests, e.g. gdb.base/break.exp is failing for me. In the above patch BLOCK_CONTIGUOUS_P is changed from this: #define BLOCK_CONTIGUOUS_P(bl) (BLOCK_RANGES (bl) == nullptr \ || BLOCK_NRANGES (bl) <= 1) to this: #define BLOCK_CONTIGUOUS_P(bl) ((bl)->ranges ().size () == 0 \ || (bl)->ranges ().size () == 1) So, before the commit we checked for the block ranges being nullptr, but afterwards we just call block::ranges() in all cases. The problem is that block::ranges() looks like this: /* Return a view on this block's ranges. */ gdb::array_view<blockrange> ranges () { return gdb::make_array_view (m_ranges->range, m_ranges->nranges); } where m_ranges is: struct blockranges *m_ranges; And so, we see that the nullptr check has been lost, and we might end up dereferencing a nullptr. My proposed fix is to move the nullptr check into block::ranges, and return an explicit empty array_view if m_ranges is nullptr. After this, everything seems fine again.
Diffstat (limited to 'gdb/block.h')
-rw-r--r--gdb/block.h14
1 files changed, 12 insertions, 2 deletions
diff --git a/gdb/block.h b/gdb/block.h
index b9f4e974c04..038ce7bd2f3 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -157,11 +157,21 @@ struct block
/* Return a view on this block's ranges. */
gdb::array_view<blockrange> ranges ()
- { return gdb::make_array_view (m_ranges->range, m_ranges->nranges); }
+ {
+ if (m_ranges == nullptr)
+ return {};
+ else
+ return gdb::make_array_view (m_ranges->range, m_ranges->nranges);
+ }
/* Const version of the above. */
gdb::array_view<const blockrange> ranges () const
- { return gdb::make_array_view (m_ranges->range, m_ranges->nranges); }
+ {
+ if (m_ranges == nullptr)
+ return {};
+ else
+ return gdb::make_array_view (m_ranges->range, m_ranges->nranges);
+ }
/* Set this block's ranges array. */
void set_ranges (blockranges *ranges)