* [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode
@ 2026-11-20 20:15 Simon Marchi
2026-11-20 20:15 ` [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw Simon Marchi
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Simon Marchi @ 2026-11-20 20:15 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
When building GDB in C++20 mode with -D_GLIBCXX_DEBUG, span-selftests.c
fails to compile with:
/usr/include/c++/16/span:196:11: error: no matching function for call to '__valid_range(const unsigned char*&, unsigned char*&)'
In debug mode, the (first, last) constructor of libstdc++'s std::span
validates the range by calling __gnu_debug::__valid_range, which
requires both iterators to have the same type. Constructing a span from
two pointers of different constness is valid C++20, but doesn't compile
in that configuration. I believe this is an instance of this existing
gcc bug:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100990<F12>
As a workaround, move the mixed constness case at the end of
check_ptr_ptr_ctor to check_ptr_ptr_mixed_cv, and skip the body of
check_ptr_ptr_mixed_cv when building in C++20 mode with _GLIBCXX_DEBUG
defined.
Change-Id: I54ff18ecc2463183ff9a0617a6e06662c1b092c5
---
gdb/unittests/span-selftests.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/gdb/unittests/span-selftests.c b/gdb/unittests/span-selftests.c
index 2317f34d1d63..c29b63a03c27 100644
--- a/gdb/unittests/span-selftests.c
+++ b/gdb/unittests/span-selftests.c
@@ -314,18 +314,21 @@ check_ptr_ptr_ctor ()
SELF_CHECK (view.data () == &data[1]);
SELF_CHECK (view[0] == data[1]);
SELF_CHECK (view[1] == data[2]);
-
- gdb_byte array[] = {0x11, 0x22, 0x33, 0x44};
- const gdb_byte *p1 = array;
- gdb_byte *p2 = array + ARRAY_SIZE (array);
- gdb::span<const gdb_byte> view2 (p1, p2);
}
-/* Check construction with a pair of pointers of mixed constness. */
+/* Check construction with a pair of pointers of mixed constness.
+
+ This is skipped when using libstdc++'s std::span in debug mode,
+ because of this libstdc++ bug requiring the two arguments to have
+ the exact same type:
+
+ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100990#c1
+ */
static void
check_ptr_ptr_mixed_cv ()
{
+#if !defined (_GLIBCXX_DEBUG) || __cplusplus < 202002L
gdb_byte array[] = {0x11, 0x22, 0x33, 0x44};
const gdb_byte *cp = array;
gdb_byte *p = array;
@@ -333,6 +336,12 @@ check_ptr_ptr_mixed_cv ()
gdb::span<const gdb_byte> view2 (p, cp);
SELF_CHECK (view1.empty ());
SELF_CHECK (view2.empty ());
+
+ const gdb_byte *p1 = array;
+ gdb_byte *p2 = array + ARRAY_SIZE (array);
+ gdb::span<const gdb_byte> view3 (p1, p2);
+ SELF_CHECK (view3.size () == ARRAY_SIZE (array));
+#endif
}
/* Check range-for support (i.e., begin()/end()). This is a template
base-commit: f50db130cf4ef292211cdd81a950c8e6469aa476
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw
2026-11-20 20:15 [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode Simon Marchi
@ 2026-11-20 20:15 ` Simon Marchi
2026-11-20 21:22 ` Tom Tromey
2026-11-20 21:22 ` [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode Tom Tromey
2026-11-21 20:32 ` Andrew Burgess
2 siblings, 1 reply; 7+ messages in thread
From: Simon Marchi @ 2026-11-20 20:15 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
When building GDB in C++20 mode with -D_GLIBCXX_DEBUG, running any
program fails with:
/usr/include/c++/16/span:186:
In function:
constexpr std::span<_Type, _Extent>::span(_It, size_type) [with _It =
field*; _Type = field; long unsigned int _Extent = 18446744073709551615;
size_type = long unsigned int]
Error: function requires a valid iterator range [first, first + count).
This happens when appending a first field with append_composite_type_field_raw:
- the type's "fields" field is nullptr initially, as there are no
fields
- number of field is incremented from 0 to 1
- `t->fields ()` is called, trying to return a span with nullptr and
a size of 1, which is invalid
This ordering is very old, but it became a problem with commit
89495c33260b ("Change type::fields to return an array_view",
2025-08-01), which made type::fields return a view of num_fields
elements. It is detected now that gdb::span is std::span in C++20 mode
(commit ced8c7a28847 ("Allow the use of C++20 std::span in
gdbsupport/span.h", 2026-11-03)), since libstdc++'s debug mode validates
the range.
Fix it by reallocating the array before incrementing the number of
fields.
There are more such existing problems uncovered when using std::span,
but this is the most obvious and blocking one.
Change-Id: I4781fc67914962fff593b9cab4d75e734a232b89
---
gdb/gdbtypes.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 648bb2c7987b..ca7cc04ea22d 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -5389,9 +5389,11 @@ append_composite_type_field_raw (struct type *t, const char *name,
{
struct field *f;
- t->set_num_fields (t->num_fields () + 1);
+ /* Reallocate the array before updating the number of fields, so that
+ fields () never covers more elements than are allocated. */
t->set_fields (XRESIZEVEC (struct field, t->fields ().data (),
- t->num_fields ()));
+ t->num_fields () + 1));
+ t->set_num_fields (t->num_fields () + 1);
f = &t->field (t->num_fields () - 1);
memset (f, 0, sizeof f[0]);
f[0].set_type (field);
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw
2026-11-20 20:15 ` [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw Simon Marchi
@ 2026-11-20 21:22 ` Tom Tromey
2026-11-21 5:09 ` Simon Marchi
0 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2026-11-20 21:22 UTC (permalink / raw)
To: Simon Marchi; +Cc: gdb-patches
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
Simon> Fix it by reallocating the array before incrementing the number of
Simon> fields.
Thanks.
Simon> There are more such existing problems uncovered when using std::span,
Simon> but this is the most obvious and blocking one.
Ugh, sorry. I will try to get to fixing these.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw
2026-11-20 21:22 ` Tom Tromey
@ 2026-11-21 5:09 ` Simon Marchi
0 siblings, 0 replies; 7+ messages in thread
From: Simon Marchi @ 2026-11-21 5:09 UTC (permalink / raw)
To: Tom Tromey, Simon Marchi; +Cc: gdb-patches
On 11/20/26 4:22 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
>
> Simon> Fix it by reallocating the array before incrementing the number of
> Simon> fields.
>
> Thanks.
>
> Simon> There are more such existing problems uncovered when using std::span,
> Simon> but this is the most obvious and blocking one.
>
> Ugh, sorry. I will try to get to fixing these.
No worries. Not your fault in either case.
Here are two I know:
$ make check TESTS="gdb.base/gcore.exp"
(gdb) core /home/smarchi/build/binutils-gdb/gdb/testsuite/outputs/gdb.base/gcore/gcore.test
[New LWP 3444629 (id 1)]
/usr/include/c++/16/span:186:
In function:
constexpr std::span<_Type, _Extent>::span(_It, size_type) [with _It =
const unsigned char*; _Type = const unsigned char; long unsigned int
_Extent = 18446744073709551615; size_type = long unsigned int]
Error: function requires a valid iterator range [first, first + count).
and
$ make check TESTS="gdb.dwarf2/bitfield-parent-optimized-out.exp"
p var.bitfield
/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).
Ideally we would also improve gdb::span's asserts to match std::span's
asserts.
> Approved-By: Tom Tromey <tom@tromey.com>
Pushed both, thanks.
Simon
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode
2026-11-20 20:15 [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode Simon Marchi
2026-11-20 20:15 ` [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw Simon Marchi
@ 2026-11-20 21:22 ` Tom Tromey
2026-11-21 20:32 ` Andrew Burgess
2 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2026-11-20 21:22 UTC (permalink / raw)
To: Simon Marchi; +Cc: gdb-patches
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
Simon> As a workaround, move the mixed constness case at the end of
Simon> check_ptr_ptr_ctor to check_ptr_ptr_mixed_cv, and skip the body of
Simon> check_ptr_ptr_mixed_cv when building in C++20 mode with _GLIBCXX_DEBUG
Simon> defined.
Thanks & sorry about that.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode
2026-11-20 20:15 [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode Simon Marchi
2026-11-20 20:15 ` [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw Simon Marchi
2026-11-20 21:22 ` [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode Tom Tromey
@ 2026-11-21 20:32 ` Andrew Burgess
2026-11-23 15:12 ` Simon Marchi
2 siblings, 1 reply; 7+ messages in thread
From: Andrew Burgess @ 2026-11-21 20:32 UTC (permalink / raw)
To: Simon Marchi, gdb-patches; +Cc: Simon Marchi
Simon Marchi <simon.marchi@efficios.com> writes:
> When building GDB in C++20 mode with -D_GLIBCXX_DEBUG, span-selftests.c
> fails to compile with:
>
> /usr/include/c++/16/span:196:11: error: no matching function for call to '__valid_range(const unsigned char*&, unsigned char*&)'
>
> In debug mode, the (first, last) constructor of libstdc++'s std::span
> validates the range by calling __gnu_debug::__valid_range, which
> requires both iterators to have the same type. Constructing a span from
> two pointers of different constness is valid C++20, but doesn't compile
> in that configuration. I believe this is an instance of this existing
> gcc bug:
>
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100990<F12>
>
> As a workaround, move the mixed constness case at the end of
> check_ptr_ptr_ctor to check_ptr_ptr_mixed_cv, and skip the body of
> check_ptr_ptr_mixed_cv when building in C++20 mode with _GLIBCXX_DEBUG
> defined.
I also ran into this, and was thinking about how to fix this. What you
propose is fine because these are unittests, and skipping them doesn't
really break any GDB functionality. But what happens when we want to
use this in actual GDB code? I guess we'll need to add casts at the
point of use?
But then, what's the alternative, avoid std::span for now? Though I'd
be fine with that, I suspect others might disagree.
Guess I'm not here to add anything constructive, just noting that this
patch is just hiding the issue until it bites someone later on.
Thanks,
Andrew
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode
2026-11-21 20:32 ` Andrew Burgess
@ 2026-11-23 15:12 ` Simon Marchi
0 siblings, 0 replies; 7+ messages in thread
From: Simon Marchi @ 2026-11-23 15:12 UTC (permalink / raw)
To: Andrew Burgess, gdb-patches
On 11/21/26 3:32 PM, Andrew Burgess wrote:
> Simon Marchi <simon.marchi@efficios.com> writes:
>
>> When building GDB in C++20 mode with -D_GLIBCXX_DEBUG, span-selftests.c
>> fails to compile with:
>>
>> /usr/include/c++/16/span:196:11: error: no matching function for call to '__valid_range(const unsigned char*&, unsigned char*&)'
>>
>> In debug mode, the (first, last) constructor of libstdc++'s std::span
>> validates the range by calling __gnu_debug::__valid_range, which
>> requires both iterators to have the same type. Constructing a span from
>> two pointers of different constness is valid C++20, but doesn't compile
>> in that configuration. I believe this is an instance of this existing
>> gcc bug:
>>
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100990<F12>
>>
>> As a workaround, move the mixed constness case at the end of
>> check_ptr_ptr_ctor to check_ptr_ptr_mixed_cv, and skip the body of
>> check_ptr_ptr_mixed_cv when building in C++20 mode with _GLIBCXX_DEBUG
>> defined.
>
> I also ran into this, and was thinking about how to fix this. What you
> propose is fine because these are unittests, and skipping them doesn't
> really break any GDB functionality. But what happens when we want to
> use this in actual GDB code? I guess we'll need to add casts at the
> point of use?
>
> But then, what's the alternative, avoid std::span for now? Though I'd
> be fine with that, I suspect others might disagree.
>
> Guess I'm not here to add anything constructive, just noting that this
> patch is just hiding the issue until it bites someone later on.
If we encountered this is real code, I suppose we could add a cast to
make sure both sides have the exact same type (if possible), and add a
comment that it is a workaround for _GLIBCXX_DEBUG.
In the case of the test, it wouldn't have made sense to add a cast,
because we precisely want to test mixed constness. On the other hand, I
am not sure what point there is to test that, if we can't use that
feature in real code due to this _GLIBCXX_DEBUG bug. Anyway, I am in
the mindset that gdb::array_view is going to be deleted soon-ish, so I
would not spend too much time thinking about that.
Simon
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-23 15:12 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 20:15 [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode Simon Marchi
2026-11-20 20:15 ` [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw Simon Marchi
2026-11-20 21:22 ` Tom Tromey
2026-11-21 5:09 ` Simon Marchi
2026-11-20 21:22 ` [PATCH 1/2] gdb/unittests: skip mixed constness span tests with libstdc++ debug mode Tom Tromey
2026-11-21 20:32 ` Andrew Burgess
2026-11-23 15:12 ` Simon Marchi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox