public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] gdbsupport: add assertions in array_view
@ 2021-10-20  1:09 Simon Marchi
  2021-10-20  1:09 ` [PATCH 2/2] gdb: change functions returning value contents to use gdb::array_view Simon Marchi
  2021-10-25 18:36 ` [PATCH 1/2] gdbsupport: add assertions in array_view Tom Tromey
  0 siblings, 2 replies; 9+ messages in thread
From: Simon Marchi @ 2021-10-20  1:09 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

Add assertions to ensure we don't access an array_view out of bounds.
Enable these assertions only when _GLIBCXX_DEBUG is set, as we did for
gdb::optional.

Change-Id: Iffaee38252405073735ed123c8e57fde6b2c6be3
---
 gdbsupport/array-view.h | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index c41fd620f833..ab1d032c60e4 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -162,9 +162,19 @@ class array_view
   constexpr const T *end () const noexcept { return m_array + m_size; }
 
   /*constexpr14*/ reference operator[] (size_t index) noexcept
-  { return m_array[index]; }
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (index < m_size);
+#endif
+    return m_array[index];
+  }
   constexpr const_reference operator[] (size_t index) const noexcept
-  { return m_array[index]; }
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (index < m_size);
+#endif
+    return m_array[index];
+  }
 
   constexpr size_type size () const noexcept { return m_size; }
   constexpr bool empty () const noexcept { return m_size == 0; }
@@ -173,12 +183,22 @@ class array_view
 
   /* Return a new array view over SIZE elements starting at START.  */
   constexpr array_view<T> slice (size_type start, size_type size) const noexcept
-  { return {m_array + start, size}; }
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (start + size <= m_size);
+#endif
+    return {m_array + start, size};
+  }
 
   /* Return a new array view over all the elements after START,
      inclusive.  */
   constexpr array_view<T> slice (size_type start) const noexcept
-  { return {m_array + start, size () - start}; }
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (start <= m_size);
+#endif
+    return {m_array + start, size () - start};
+  }
 
 private:
   T *m_array;
-- 
2.33.1


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

end of thread, other threads:[~2021-10-27 14:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-20  1:09 [PATCH 1/2] gdbsupport: add assertions in array_view Simon Marchi
2021-10-20  1:09 ` [PATCH 2/2] gdb: change functions returning value contents to use gdb::array_view Simon Marchi
2021-10-25 18:47   ` Tom Tromey
2021-10-25 18:52     ` Simon Marchi
2021-10-27 12:53   ` Luis Machado
2021-10-27 13:43     ` Simon Marchi
2021-10-27 14:28       ` Luis Machado
2021-10-25 18:36 ` [PATCH 1/2] gdbsupport: add assertions in array_view Tom Tromey
2021-10-25 18:49   ` 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).