Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Make gdb::span handle nullptr like std::span
@ 2026-11-22  5:41 Tom Tromey
  2026-11-22  8:08 ` Andrew Burgess
  2026-11-22 23:30 ` Tom Tromey
  0 siblings, 2 replies; 6+ messages in thread
From: Tom Tromey @ 2026-11-22  5:41 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

A C++20 std::span can be constructed with nullptr, but only if the
length is 0.  gdb was violating this rule in a couple of places.  This
patch fixes two spots and changes gdb::span to have a similar
debug-mode assert.

I regression tested this (1) with '-std=c++20 -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC', and (2) with an ordinary build but with a
version of this patch where the asserts were unconditional.
---
 gdb/regcache.c    |  2 +-
 gdb/value.c       |  6 +++++-
 gdbsupport/span.h | 12 ++++++++++--
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/gdb/regcache.c b/gdb/regcache.c
index 0e7e6a85284..a23c2dd318f 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -1161,7 +1161,7 @@ reg_buffer::raw_supply (int regnum, const void *src)
 {
   assert_regnum (regnum);
 
-  int size = m_descr->sizeof_register[regnum];
+  int size = src == nullptr ? 0 : m_descr->sizeof_register[regnum];
   raw_supply (regnum, gdb::make_span ((const gdb_byte *) src, size));
 }
 
diff --git a/gdb/value.c b/gdb/value.c
index 195f4bb5185..a16cb2a6752 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -1158,8 +1158,12 @@ value::contents_for_printing ()
   if (m_lazy)
     fetch_lazy ();
 
+  const gdb_byte *contents = m_contents.get ();
+  if (contents == nullptr)
+    return {};
+
   ULONGEST length = enclosing_type ()->length ();
-  return gdb::make_span (m_contents.get (), length);
+  return gdb::make_span (contents, length);
 }
 
 gdb::span<const gdb_byte>
diff --git a/gdbsupport/span.h b/gdbsupport/span.h
index 5288391d2e1..defd38c655e 100644
--- a/gdbsupport/span.h
+++ b/gdbsupport/span.h
@@ -109,7 +109,11 @@ class span
 	   typename = Requires<DecayedConvertible<U>>>
   constexpr span (U *array, size_t size) noexcept
     : m_array (array), m_size (size)
-  {}
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert (m_size != nullptr || m_size == 0);
+#endif
+  }
 
   /* Create a span from a range.  This is templated on both U
      an V to allow passing in a mix of 'const T *' and 'T *'.  */
@@ -118,7 +122,11 @@ class span
 	   typename = Requires<DecayedConvertible<V>>>
   constexpr span (U *begin, V *end) noexcept
     : m_array (begin), m_size (end - begin)
-  {}
+  {
+#if defined(_GLIBCXX_DEBUG)
+    gdb_assert ((begin == nullptr) == (end == nullptr));
+#endif
+  }
 
   /* Create a span from an array.  */
   template<typename U, size_t Size,
-- 
2.49.0


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

end of thread, other threads:[~2026-11-23 16:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22  5:41 [PATCH] Make gdb::span handle nullptr like std::span Tom Tromey
2026-11-22  8:08 ` Andrew Burgess
2026-11-22 17:03   ` Tom Tromey
2026-11-22 23:25     ` Andrew Burgess
2026-11-22 23:30 ` Tom Tromey
2026-11-23 16:57   ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox