Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw
Date: Fri, 20 Nov 2026 15:15:02 -0500	[thread overview]
Message-ID: <20261120201510.3162938-2-simon.marchi@efficios.com> (raw)
In-Reply-To: <20261120201510.3162938-1-simon.marchi@efficios.com>

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


  reply	other threads:[~2026-11-20 20:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-20 21:22   ` [PATCH 2/2] gdb: fix out-of-bounds span in append_composite_type_field_raw 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

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=20261120201510.3162938-2-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    /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