From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>, Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH] gdb: fix value::contents_for_printing with std::span
Date: Sat, 21 Nov 2026 22:55:06 +0000 [thread overview]
Message-ID: <e124dae9b62609ac748fcd655f4f8db40251e6ff.1795301681.git.aburgess@redhat.com> (raw)
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
next reply other threads:[~2026-11-21 22:55 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 22:55 Andrew Burgess [this message]
2026-11-22 5:43 ` Tom Tromey
2026-11-22 23:21 ` Andrew Burgess
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=e124dae9b62609ac748fcd655f4f8db40251e6ff.1795301681.git.aburgess@redhat.com \
--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