* [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* Re: [PATCH] Make gdb::span handle nullptr like std::span
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:30 ` Tom Tromey
1 sibling, 1 reply; 6+ messages in thread
From: Andrew Burgess @ 2026-11-22 8:08 UTC (permalink / raw)
To: Tom Tromey, gdb-patches; +Cc: Tom Tromey
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
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] Make gdb::span handle nullptr like std::span
2026-11-22 8:08 ` Andrew Burgess
@ 2026-11-22 17:03 ` Tom Tromey
2026-11-22 23:25 ` Andrew Burgess
0 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2026-11-22 17:03 UTC (permalink / raw)
To: Andrew Burgess; +Cc: Tom Tromey, gdb-patches
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
Andrew> I don't see any benefit of wrapping these in _GLIBCXX_DEBUG. Sure it
Andrew> means we match the std::span behaviour, but all that really means is
Andrew> that we fail to spot undefined behaviour, which isn't a good thing.
Andrew> I'd prefer these were just unwrapped gdb_assert(...) calls.
Andrew> But if you feel strongly about it then I'm not trying to block this fix.
I only did it because it's what is already done in the file, and because
in the past we've had discussions about how certain asserts slow things
down.
I don't mind removing the #ifs at all.
Andrew> I think you should just go ahead and merge your patch:
Actually I think you should push yours & I will rebase on top of those.
Tom
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Make gdb::span handle nullptr like std::span
2026-11-22 17:03 ` Tom Tromey
@ 2026-11-22 23:25 ` Andrew Burgess
0 siblings, 0 replies; 6+ messages in thread
From: Andrew Burgess @ 2026-11-22 23:25 UTC (permalink / raw)
To: Tom Tromey; +Cc: Tom Tromey, gdb-patches
Tom Tromey <tom@tromey.com> writes:
>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
>
> Andrew> I don't see any benefit of wrapping these in _GLIBCXX_DEBUG. Sure it
> Andrew> means we match the std::span behaviour, but all that really means is
> Andrew> that we fail to spot undefined behaviour, which isn't a good thing.
>
> Andrew> I'd prefer these were just unwrapped gdb_assert(...) calls.
>
> Andrew> But if you feel strongly about it then I'm not trying to block this fix.
>
> I only did it because it's what is already done in the file, and because
> in the past we've had discussions about how certain asserts slow things
> down.
Yeah, I think that's why we cannot just set _GLIBCXX_DEBUG on for all
GDB builds (in the same way that standard gdb_asserts are on for all GDB
builds).
But I think these particular asserts are not going to have a massive
performance hit.
>
> I don't mind removing the #ifs at all.
Meh, as you requested, I just pushed my patches, which includes a
similar assert in gdb::make_span, which is an unguarded gdb_assert, so
we will catch some of these anyway, whether using the old gdb::span or
std::span.
I'd drop the #ifs, but I don't feel that strongly about it :)
>
> Andrew> I think you should just go ahead and merge your patch:
>
> Actually I think you should push yours & I will rebase on top of those.
Done.
Thanks for your feedback.
Andrew
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Make gdb::span handle nullptr like std::span
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 23:30 ` Tom Tromey
2026-11-23 16:57 ` Tom Tromey
1 sibling, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2026-11-22 23:30 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:
Tom> I regression tested this (1) with '-std=c++20 -D_GLIBCXX_DEBUG
Tom> -D_GLIBCXX_DEBUG_PEDANTIC', and (2) with an ordinary build but with a
Tom> version of this patch where the asserts were unconditional.
Tom> +#if defined(_GLIBCXX_DEBUG)
Tom> + gdb_assert (m_size != nullptr || m_size == 0);
This won't compile so I obviously botched the testing.
I'm redoing it now.
Tom
^ 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