From: Kevin Buettner <kevinb@redhat.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: Re: [PATCH 1/2] gdb: fix crash when pretty printing anonymous struct with base class
Date: Mon, 17 Aug 2026 22:21:04 -0700 [thread overview]
Message-ID: <20260817222104.0181be66@f44-mesa-1> (raw)
In-Reply-To: <20260817145907.142300-1-simon.marchi@efficios.com>
On Mon, 17 Aug 2026 10:59:01 -0400
Simon Marchi <simon.marchi@efficios.com> wrote:
> 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 = {
> <base> = {
> 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 <cplus_language_defn>, 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 <cplus_language_defn>) 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 = {
> <base> = {
> a = 1
> },
> members of <unnamed type>:
> 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.
>
Both parts LGTM.
Approved-By: Kevin Buettner <kevinb@redhat.com>
next prev parent reply other threads:[~2026-08-18 5:21 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-17 14:59 Simon Marchi
2026-08-17 14:59 ` [PATCH 2/2] gdb: don't print trailing space after base classes in pretty format Simon Marchi
2026-08-18 5:21 ` Kevin Buettner [this message]
2026-08-20 14:12 ` [PATCH 1/2] gdb: fix crash when pretty printing anonymous struct with base class Simon Marchi
2026-08-20 16:21 ` Tom Tromey
2026-08-20 16:26 ` 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=20260817222104.0181be66@f44-mesa-1 \
--to=kevinb@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@efficios.com \
/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