* [PATCH] gdb: fix value::contents_for_printing with std::span
@ 2026-11-21 22:55 Andrew Burgess
2026-11-22 5:43 ` Tom Tromey
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Burgess @ 2026-11-21 22:55 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey, Andrew Burgess
After the switch to use std::span (where possible), and when building
GDB in C++20 mode with -D_GLIBCXX_DEBUG, I'm seeing failures when
running:
make check-gdb TESTS="gdb.dwarf2/bitfield-parent-optimized-out.exp"
The failure looks like this:
/usr/include/c++/16/span:186:
In function:
constexpr std::span<_Type, _Extent>::span(_It, size_type) [with _It =
unsigned char*; _Type = unsigned char; long unsigned int _Extent =
18446744073709551615; size_type = long unsigned int]
Error: function requires a valid iterator range [first, first + count).
Objects involved in the operation:
iterator "=================================================================
==457781==ERROR: AddressSanitizer: global-buffer-overflow on address 0x000007c8f688 at pc 0x7f798f2b31b0 >
READ of size 9 at 0x000007c8f688 thread T0
<snip>
#5 0x000000435a09 in std::span<unsigned char, 18446744073709551615ul>::span<unsigned char*>(unsigned >
#6 0x00000042d9df in std::span<unsigned char, 18446744073709551615ul> gdb::make_span<unsigned char>(u>
#7 0x000004094d61 in value::contents_for_printing() ../../src/gdb/value.c:1162
#8 0x0000040ad448 in value::fetch_lazy_bitfield() ../../src/gdb/value.c:3958
#9 0x0000040af403 in value::fetch_lazy() ../../src/gdb/value.c:4126
#10 0x0000040a76a0 in value::primitive_field(long, int, type*) ../../src/gdb/value.c:3073
#11 0x00000403a1b8 in struct_field_searcher::search(value*, long, type*) ../../src/gdb/valops.c:2025
#12 0x00000403b318 in search_struct_field ../../src/gdb/valops.c:2145
#13 0x00000403ea36 in value_struct_elt(value**, std::optional<std::span<value*, 18446744073709551615u>
<snip>
In this test the used does this:
(gdb) p var
$1 = <optimized out>
This ends up calling value::allocate_optimized_out for VAR, this marks
the value as non-lazy which deliberately avoids allocating an m_contents
buffer for the value; the whole value is instead marked as optimized
out.
Next in the test the user does:
(gdb) p var.bitfield
Which is what triggers the assertion seen above. This creates a value
for BITFIELD with VAR as its parent (in value::primitive_field).
As the value of BITFIELD is being printed we eventually call
value::fetch_lazy for BITFIELD, which calls fetch_lazy_bitfield. This
finds VAR, the parent value, and then calls contents_for_printing on
VAR to gets its content. The content is passed to
VAR->unpack_bitfield which copies the contents into BITFIELD and also
copies over any optimized out and unavailable data from VAR to BITFIELD.
It is worth noting that unpack_bitfield specifically handles the case
where the value contents buffer is nullptr, which a comment helpfully
tells us happens when the parent value is entirely optimized out.
The use of value::contents_for_printing in value::fetch_lazy_bitfield
was introduced in commit:
commit 4875ffdbdaf7793524464c99baea1d8eb0cb7e34
Date: Thu Sep 4 12:12:41 2014 +0100
Regression for i686 gdb.dwarf2/pieces-optimized-out.exp
The commit message doesn't say, but I suspect contents_for_printing was
used instead of value::contents_all_raw specifically to avoid allocating
the m_contents buffer in the case that a value is entirely optimized
out.
So where does this leave us?
I think that the expected behaviour of contents_for_printing is that it
should return a gdb::span with nullpptr for its data pointer in the case
where the value contents are entirely optimized out. This matches the
existing code as far as I understand it.
However, value::contents_for_printing as it's currently written, when
m_contents is not allocated, will create a gdb::span with nullptr for
its data and a non-zero length. This is undefined when gdb::span is an
alias for std::span.
Fix this by specifically spotting the case where m_contents is not
allocated and returning an empty gdb::span.
After this the test mentioned at the start of this commit message
passes.
---
gdb/value.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/gdb/value.c b/gdb/value.c
index 195f4bb5185..9960f2902c6 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -1158,6 +1158,11 @@ value::contents_for_printing ()
if (m_lazy)
fetch_lazy ();
+ /* Avoid creating a std::span with nullptr for data and a non-zero length,
+ this is undefined. */
+ if (m_contents == nullptr)
+ return {};
+
ULONGEST length = enclosing_type ()->length ();
return gdb::make_span (m_contents.get (), length);
}
@@ -1167,6 +1172,11 @@ value::contents_for_printing () const
{
gdb_assert (!m_lazy);
+ /* Avoid creating a std::span with nullptr for data and a non-zero length,
+ this is undefined. */
+ if (m_contents == nullptr)
+ return {};
+
ULONGEST length = enclosing_type ()->length ();
return gdb::make_span (m_contents.get (), length);
}
base-commit: 20dd9eefcb195157a8d7860721627150649ed751
--
2.56.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] gdb: fix value::contents_for_printing with std::span
2026-11-21 22:55 [PATCH] gdb: fix value::contents_for_printing with std::span Andrew Burgess
@ 2026-11-22 5:43 ` Tom Tromey
2026-11-22 23:21 ` Andrew Burgess
0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2026-11-22 5:43 UTC (permalink / raw)
To: Andrew Burgess; +Cc: gdb-patches, Tom Tromey
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
Andrew> After the switch to use std::span (where possible), and when building
Andrew> GDB in C++20 mode with -D_GLIBCXX_DEBUG, I'm seeing failures when
Andrew> running:
This one's better than my patch, I didn't see the second spot; but
neither did the test suite when I ran it.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] gdb: fix value::contents_for_printing with std::span
2026-11-22 5:43 ` Tom Tromey
@ 2026-11-22 23:21 ` Andrew Burgess
0 siblings, 0 replies; 3+ messages in thread
From: Andrew Burgess @ 2026-11-22 23:21 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches, Tom Tromey
Tom Tromey <tom@tromey.com> writes:
>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:
>
> Andrew> After the switch to use std::span (where possible), and when building
> Andrew> GDB in C++20 mode with -D_GLIBCXX_DEBUG, I'm seeing failures when
> Andrew> running:
>
> This one's better than my patch, I didn't see the second spot; but
> neither did the test suite when I ran it.
>
> Approved-By: Tom Tromey <tom@tromey.com>
Pushed.
Thanks,
Andrew
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-22 23:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21 22:55 [PATCH] gdb: fix value::contents_for_printing with std::span Andrew Burgess
2026-11-22 5:43 ` Tom Tromey
2026-11-22 23:21 ` Andrew Burgess
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox