From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id pqPxJW4hAmsQMioAWB0awg (envelope-from ) for ; Sat, 21 Nov 2026 17:55:42 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dOfdPF4k; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 639A71E167; Sat, 21 Nov 2026 17:55:42 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::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 311CD1E023 for ; Sat, 21 Nov 2026 17:55:41 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B707246FE802 for ; Sat, 21 Nov 2026 22:55:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B707246FE802 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dOfdPF4k Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id D78A34AA51CD for ; Sat, 21 Nov 2026 22:55:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D78A34AA51CD Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D78A34AA51CD Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795301715; cv=none; b=mRs5ViZAGN8m/GGkEsLX19kDdzJPHgaz6xPMbWlBDf7bXeCiVrSNu1GiUTorV8l5RtYKfS6cmgKJZlYkHvxZoovOd7dmWA6iSVLkTiDG+NUkG+Pu+r2G5bjQnXC2txVBBhV3MyxjVunMno2LcgZhAmMZnWRDeMBg12PzJR7AhN0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795301715; c=relaxed/simple; bh=SwEtVov64k3UKK7cciIKNpfH0mArV5ypLqoxLpZA5kA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CdaBr4Gcce9C0EJjnNmeL2ePg2R6mzG3jIKsMRUwV4dmQOtnvGpn47NQtYFT7EfBPNcKGJx87xtihM0653+R559VYwgKmoFIN0rU+w1Pi27rBufeZ5gZaCI9QmWmav9leA8bEPyljl0EEkaINpBZYHydg9yjEM5gZShlPzLMOdY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dOfdPF4k DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D78A34AA51CD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795301714; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=z7xxZgeh8KvVEQztad7BPM1HJw20TBIWObmjlB7IieA=; b=dOfdPF4kr0eOnX+C11Fxmpwz626fEK864EjJxTLrNj6cO2yTGBoXR+8zjYNSVrgF/l9pcU YdylFsnhH5ZtBdtUqkEHe65pv2qlp/YaPI/HlrA+8BSwTJxyoeHRRN4BtOSX/PJfid8QmV +XXE9fjMxFJAtZuMJFxBdXGtVmFBuyE= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-621-EZMiKqECNWGCA7kuT0evPA-1; Sat, 21 Nov 2026 22:55:11 +0000 X-MC-Unique: EZMiKqECNWGCA7kuT0evPA-1 X-Mimecast-MFC-AGG-ID: EZMiKqECNWGCA7kuT0evPA_1795301711 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4a204ac318aso9441945e9.2 for ; Sat, 21 Nov 2026 14:55:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795301710; x=1795906510; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=z7xxZgeh8KvVEQztad7BPM1HJw20TBIWObmjlB7IieA=; b=qPkGfrQhh2PYayhB7HBzmOe93Q2BshpvvS8O1STuI4pNPjHsULeUvWQ60laPrdbGwr 0lAgbWO0CsmrrGCT2Hv+nG0MytwS3I9kHT5jyeSlz/8Q1JTKtlf0T7Hr/Ivq3T3mFZ0p Sd00h8KjuV4C/OPc85T7AH2XVCWmnBIJcBGhgwGKNmfuS1d/5w5s4U7fbrPM04P2ckeo Q2tWrHMxQ5iW4i8Ofkm21B6OZhLA0993eBz3V4U7D4pqlMOfJYEPDRZ0b5C2t//dtWMQ uJ1zL4sYA8BX+346R89Z+2mYtt1D4L51QOXYd7v+J7lIjcQlsuvUv4qKsIvKlWOaqJdH zogg== X-Gm-Message-State: APuU4Z9uu5vmxyMLuvzy6A8k8Zr5WGGhWA2kytrdG+EprMSKFw7IGxJM RsfsTCNObmSNOpyeeN8rcAbwJCIuUJnLRtdJgFr4q9NtrZlC7pnZ3BWeLZZhE0sSr0e7GNPMjDX 0OdJYBhyGFh1LU7c5wV1Ks66B1XOZDueRHqqzEPSRRGzIiiUUwts8f39HSNnh/X+Brl89vDltCe H0a9NHFbzOH9oHo0z8s3yBFRnoumadUZCm3JoJoUB4MKFu0PM= X-Gm-Gg: AZFdTZeeG92Q2lrxTJs7ubgAzg8uibNRHfrnJniQCNoXgIcB1eX4g2GqO00xNff0b0g n/W4zn6u7PnzUbHaf4ilzDXlU7xrR4NJVloZXHcWLFN8hUc0Sj8DHAg1tAbsyalWQAv4nO2Xia1 PNPbnRTW/f2Up0oqGIEgnvbjlYeJmqU1AnEWh2RLIzhYXLkDs+jvoHgTP2YaO9BHSaf8tO0C101 iCxqRB65bZRphoksnic4mkPT6/IgoHIW80dldEck8N9Hj3U+Gw2ukhkHVKYFO74nOtLSsKWnTJH Xoo+/Mo4Q5tPGKvYCGM9yXfWj2G+VBBERR7VP1/FrdGfDewEfQSaQpC6/aTQv4scj+DWAwLW/A3 cjZ4op5d/NVYrqQQ1k+sE2zTNMC9SPBDWnyJiwD3+cqt8zA== X-Received: by 2002:a05:600c:a08b:b0:4a5:87b3:ddc1 with SMTP id 5b1f17b1804b1-4bb88922c52mr148849555e9.13.1795301710565; Sat, 21 Nov 2026 14:55:10 -0800 (PST) X-Received: by 2002:a05:600c:a08b:b0:4a5:87b3:ddc1 with SMTP id 5b1f17b1804b1-4bb88922c52mr148849385e9.13.1795301710032; Sat, 21 Nov 2026 14:55:10 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb895f7bc1sm74371105e9.3.2026.11.21.14.55.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Nov 2026 14:55:09 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Tom Tromey , Andrew Burgess Subject: [PATCH] gdb: fix value::contents_for_printing with std::span Date: Sat, 21 Nov 2026 22:55:06 +0000 Message-ID: X-Mailer: git-send-email 2.56.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 8CoxTzFyClfQd-5CgtA4u1HZQtvddnrRI6_F4A9AMUo_1795301711 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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 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 #5 0x000000435a09 in std::span::span(unsigned > #6 0x00000042d9df in std::span gdb::make_span(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 In this test the used does this: (gdb) p var $1 = 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