Re: [PATCH 1/2] gdb: fix crash when pretty printing anonymous struct with base class

Kevin Buettner <[email protected]>
Newsgroups gmane.comp.gdb.patches
Organization Red Hat
Message-ID <20260817222104.0181be66@f44-mesa-1>
On Mon, 17 Aug 2026 10:59:01 -0400
Simon Marchi <[email protected]> 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 <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.