From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id xS9cGHGqAGs0HiUAWB0awg (envelope-from ) for ; Fri, 20 Nov 2026 15:15:45 -0500 Received: by simark.ca (Postfix, from userid 112) id 3A84D1E0CE; Fri, 20 Nov 2026 15:15:45 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 30D5A1E091 for ; Fri, 20 Nov 2026 15:15:44 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 456F240A1FA3 for ; Fri, 20 Nov 2026 20:15:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 456F240A1FA3 Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id C9D4A40A24BF for ; Fri, 20 Nov 2026 20:15:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C9D4A40A24BF Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=efficios.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C9D4A40A24BF Authentication-Results: sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795205719; cv=none; b=LOy3MygtnAsgq5rE4nhvEuvOIbBqio0s1VmcjaADoXoFLGCafJ+r26y9UiVyZH1IsDUmgVtuD1s476azQZ82IJorY7y47D9f6JnQSRZQZBum83q9JjcoHom8q2DEc7o30MteCnEAF7f6+Pl3kgpBICyXkso6MX9dGQ8dhZBW6sI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795205719; c=relaxed/simple; bh=r3JEPwUsdUoQobSpXoRh+tna2H1gxvedPHsXYlI4UrQ=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=TuXOjZSk1+e4bGXt13ub9thuonV2u6+jXzj7nYod+SY5dUAccylVW3A1mODN3RH8aamPnuROPHF5YgMNZxzsGQlu4kgNKflqfeMewq3RjMalUw/ADj1SBS1CHoBi97+vrmbz1+bbHbw5dZbOs50tiQ8FJglqKrmZftOFtu5LBKA= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C9D4A40A24BF Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 6AKKFDvV203048 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 20 Nov 2026 15:15:18 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 6AKKFDvV203048 Received: by simark.ca (Postfix) id C9D961E091; Fri, 20 Nov 2026 15:15:12 -0500 (EST) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi 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 Message-ID: <20261120201510.3162938-2-simon.marchi@efficios.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261120201510.3162938-1-simon.marchi@efficios.com> References: <20261120201510.3162938-1-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Fri, 20 Nov 2026 20:15:13 +0000 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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