From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id Y935Adohg2oFGi0AWB0awg (envelope-from ) for ; Mon, 17 Aug 2026 10:59:38 -0400 Received: by simark.ca (Postfix, from userid 112) id B3E311E167; Mon, 17 Aug 2026 10:59:37 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, 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 AB1571E033 for ; Mon, 17 Aug 2026 10:59:36 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B25B04BA79BD for ; Mon, 17 Aug 2026 14:59:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B25B04BA79BD Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 31B3F4BA23E0 for ; Mon, 17 Aug 2026 14:59:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 31B3F4BA23E0 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=efficios.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=efficios.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 31B3F4BA23E0 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786978750; cv=none; b=Q4ofRgYBMeIij1Xh8xhJAltehbEw91eL55lAnFc4/mZ7Kt7KuaAxeHz5CT1lqcEXzyo4UtYJuQXEAylaP3afJEJFwq6NiW/2ChidWem7ywLN4dw57aD1peaSGkeY4RgrBDpUFD+XeydYTOs0QS3QQy2zXBgz5b77w1Lc/gD00a4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786978750; c=relaxed/simple; bh=sjI7MhUtRojZy/cv2MYEvFQLJy3/d1ZndwQ8vqWPANY=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Az1pft59dAf7Ns9aIbeOvt25A4ayS/KLT32KdLmUmjuG25bggrulQC3ijjG8kI8W2h9yVLUV3ar8L3BbYEWakygK+le1cJPK/2u1uTKYovPa0b6/mzjAqAV2XoEcqiUNTLg1kBG/C+CUafPDTgZ2ozSIr1BQQeqSOgijDxd+oyA= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 31B3F4BA23E0 Received: by simark.ca (Postfix) id 874F71E033; Mon, 17 Aug 2026 10:59:08 -0400 (EDT) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 1/2] gdb: fix crash when pretty printing anonymous struct with base class Date: Mon, 17 Aug 2026 10:59:01 -0400 Message-ID: <20260817145907.142300-1-simon.marchi@efficios.com> X-Mailer: git-send-email 2.55.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 As I was reviewing another patch, I was wondering if we allowed type->name() to be nullptr. The answer is yes, but there are some spots that don't check for nullptr, when they should. Claude came up with a reproducer that inspired the included test case. It is arguably a corner case, but it makes GDB crash. The crash happens when trying to pretty-print (with "set print pretty on") a value of an anonymous struct type that has a base. Given this: struct base { int a; }; struct : base { int b; } v4 = { { 1 }, 2 }; We get: $ ./gdb -nx -q --data-directory=data-directory testsuite/outputs/gdb.cp/anon-struct/anon-struct -ex "with print pretty -- p v4" Reading symbols from testsuite/outputs/gdb.cp/anon-struct/anon-struct... $1 = { = { a = 1 }, /home/smarchi/src/binutils-gdb/gdb/ui-file.h:77:30: runtime error: null pointer passed as argument 1, which is declared to never be null The crash happens here: #4 0x00007ffff600dec8 in __ubsan_handle_nonnull_arg_abort () from /usr/lib/libubsan.so.1 #5 0x00005555637daa35 in ui_file::puts (this=0x7c1ff1c022d0, str=0x0) at /home/smarchi/src/binutils-gdb/gdb/ui-file.h:77 #6 0x0000555566f2b0ab in gdb_puts (linebuffer=0x0, stream=0x7c1ff1c022d0) at /home/smarchi/src/binutils-gdb/gdb/utils.c:1912 #7 0x00005555644e5cd4 in cp_print_value_fields (val=0x7d0ff1c2aa00, stream=0x7c1ff1c022d0, recurse=0, options=0x7bfff094acb0, dont_print_vb=0x0, dont_print_statmem=0) at /home/smarchi/src/binutils-gdb/gdb/cp-valprint.c:213 #8 0x00005555640de708 in c_value_print_struct (val=0x7d0ff1c2aa00, stream=0x7c1ff1c022d0, recurse=0, options=0x7bfff094acb0) at /home/smarchi/src/binutils-gdb/gdb/c-valprint.c:385 #9 0x00005555640df215 in c_value_print_inner (val=0x7d0ff1c2aa00, stream=0x7c1ff1c022d0, recurse=0, options=0x7bfff094acb0) at /home/smarchi/src/binutils-gdb/gdb/c-valprint.c:441 #10 0x000055556563828c in language_defn::value_print_inner (this=0x5555734a0700 , val=0x7d0ff1c2aa00, stream=0x7c1ff1c022d0, recurse=0, options=0x7bfff094acb0) at /home/smarchi/src/binutils-gdb/gdb/language.c:658 #11 0x0000555566fb7749 in common_val_print (value=0x7d0ff1c2aa00, stream=0x7c1ff1c022d0, recurse=0, options=0x7bfff094aa70, language=0x5555734a0700 ) at /home/smarchi/src/binutils-gdb/gdb/valprint.c:1120 when we try to print the nullptr type name: if (options->prettyformat) { gdb_printf (stream, "\n"); print_spaces (2 + 2 * recurse, stream); gdb_puts ("members of ", stream); gdb_puts (type->name (), stream); gdb_puts (":", stream); } Fix it by using type->safe_name() instead of type->name(). This results in: $1 = { = { a = 1 }, members of : b = 2 } Claude pointed out that p-valprint.c has more or less the same code, so I changed it there too, but I did not write a test for that one. Change-Id: Ibc4541bf04b7239e31b8401aa86466a6807e89d5 --- gdb/cp-valprint.c | 2 +- gdb/p-valprint.c | 2 +- gdb/testsuite/gdb.cp/anon-struct-with-base.cc | 49 ++++++++++++++ .../gdb.cp/anon-struct-with-base.exp | 65 +++++++++++++++++++ 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 gdb/testsuite/gdb.cp/anon-struct-with-base.cc create mode 100644 gdb/testsuite/gdb.cp/anon-struct-with-base.exp diff --git a/gdb/cp-valprint.c b/gdb/cp-valprint.c index 349a0d6bf9e9..d450e90e1e1d 100644 --- a/gdb/cp-valprint.c +++ b/gdb/cp-valprint.c @@ -210,7 +210,7 @@ cp_print_value_fields (struct value *val, struct ui_file *stream, gdb_printf (stream, "\n"); print_spaces (2 + 2 * recurse, stream); gdb_puts ("members of ", stream); - gdb_puts (type->name (), stream); + gdb_puts (type->safe_name (), stream); gdb_puts (":", stream); } } diff --git a/gdb/p-valprint.c b/gdb/p-valprint.c index f64c1069b531..2f913a3da150 100644 --- a/gdb/p-valprint.c +++ b/gdb/p-valprint.c @@ -564,7 +564,7 @@ pascal_object_print_value_fields (struct value *val, struct ui_file *stream, gdb_printf (stream, "\n"); print_spaces (2 + 2 * recurse, stream); gdb_puts ("members of ", stream); - gdb_puts (type->name (), stream); + gdb_puts (type->safe_name (), stream); gdb_puts (": ", stream); } } diff --git a/gdb/testsuite/gdb.cp/anon-struct-with-base.cc b/gdb/testsuite/gdb.cp/anon-struct-with-base.cc new file mode 100644 index 000000000000..474473abb666 --- /dev/null +++ b/gdb/testsuite/gdb.cp/anon-struct-with-base.cc @@ -0,0 +1,49 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +struct base_data +{ + int a; +}; + +struct base_no_data +{ +}; + +struct : base_data +{ + int b; +} v_data_base_data = { { 1 }, 2 }; + +struct : base_data +{ +} v_no_data_base_data = { { 3 } }; + +struct : base_no_data +{ + int c; +} v_data_base_no_data = { {}, 4 }; + +struct : base_no_data +{ +} v_no_data_base_no_data = {}; + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.cp/anon-struct-with-base.exp b/gdb/testsuite/gdb.cp/anon-struct-with-base.exp new file mode 100644 index 000000000000..2ff7036198a2 --- /dev/null +++ b/gdb/testsuite/gdb.cp/anon-struct-with-base.exp @@ -0,0 +1,65 @@ +# Copyright 2026 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test printing a value whose type is an unnamed struct with a base class. + +standard_testfile .cc + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile \ + {debug c++}]} { + return +} + +gdb_test "with print pretty off -- print v_data_base_data" \ + "$::valnum_re = \{ = \{a = 1\}, b = 2\}" + +gdb_test "with print pretty off -- print v_no_data_base_data" \ + "$::valnum_re = \{ = \{a = 3\}, \}" + +gdb_test "with print pretty off -- print v_data_base_no_data" \ + "$::valnum_re = \{ = \{\}, c = 4\}" + +gdb_test "with print pretty off -- print v_no_data_base_no_data" \ + "$::valnum_re = \{ = \{\}, \}" + +gdb_test "with print pretty on -- print v_data_base_data" \ + [multi_line \ + "$::valnum_re = \{" \ + " = \{" \ + " a = 1" \ + " \}, " \ + " members of :" \ + " b = 2" \ + "\}"] + +gdb_test "with print pretty on -- print v_no_data_base_data" \ + [multi_line \ + "$::valnum_re = \{" \ + " = \{" \ + " a = 3" \ + " \}, \}"] + +gdb_test "with print pretty on -- print v_data_base_no_data" \ + [multi_line \ + "$::valnum_re = \{" \ + " = \{\}, " \ + " members of :" \ + " c = 4" \ + "\}"] + +gdb_test "with print pretty on -- print v_no_data_base_no_data" \ + [multi_line \ + "$::valnum_re = \{" \ + " = \{\}, \}"] base-commit: 0f9faaebc91bc1886a563bde6c178601b4be743b -- 2.55.0