From: Andrew Burgess <aburgess@redhat.com>
To: Tom Tromey <tom@tromey.com>, gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: Re: [PATCH] Make gdb::span handle nullptr like std::span
Date: Sun, 22 Nov 2026 08:08:54 +0000 [thread overview]
Message-ID: <xhuyfa4n1l23t.fsf@aburgess-thinkpadp1gen8.rmtgb.csb> (raw)
In-Reply-To: <20261122054124.3738131-1-tom@tromey.com>
Tom Tromey <tom@tromey.com> writes:
> 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
I don't see any benefit of wrapping these in _GLIBCXX_DEBUG. Sure it
means we match the std::span behaviour, but all that really means is
that we fail to spot undefined behaviour, which isn't a good thing.
I'd prefer these were just unwrapped gdb_assert(...) calls.
But if you feel strongly about it then I'm not trying to block this fix.
I think you should copy the value::contents_for_printing fix to its
overload.
The patches I had posted also copied the regcache fix (or something like
it) to more places (in gdbsupport/common-regcache.*) , you might
consider doing the same, but if not I'll post a follow up once this is
merged.
I think you should just go ahead and merge your patch:
Approved-By: Andrew Burgess <aburgess@redhat.com>
Thanks,
Andrew
> + }
>
> /* 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 prev parent reply other threads:[~2026-11-22 8:09 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 5:41 Tom Tromey
2026-11-22 8:08 ` Andrew Burgess [this message]
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=xhuyfa4n1l23t.fsf@aburgess-thinkpadp1gen8.rmtgb.csb \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/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