From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Make gdb::span handle nullptr like std::span
Date: Sat, 21 Nov 2026 22:41:24 -0700 [thread overview]
Message-ID: <20261122054124.3738131-1-tom@tromey.com> (raw)
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
next reply other threads:[~2026-11-22 5:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 5:41 Tom Tromey [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261122054124.3738131-1-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox