https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124432

            Bug ID: 124432
           Summary: ICE in write_fbreg_variable (dwarf2codeview.cc:2648)
                    with -gcodeview and artificial local variables
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kachalenko.denis at gmail dot com
  Target Milestone: ---

GCC crashes with a segfault in write_fbreg_variable when compiling valid code
with -gcodeview at -O0 (default). The crash happens when DWARF local variables
with DW_AT_artificial (and no DW_AT_name) are translated to CodeView symbols.

Two independent triggers: structured bindings and C++26 contracts. Both hit the
same crash site.

== Reproducer 1: structured bindings ==

  // sb.cpp
  struct S{int a,b;};
  void f(){auto[a,b]=S{};}

  $ g++ -std=c++17 -gcodeview -c sb.cpp

== Reproducer 2: contracts ==

  // ct.cpp
  void f()pre(true){}

  $ g++ -std=c++26 -gcodeview -fcontracts -c ct.cpp

== Backtrace (identical for both) ==

  internal compiler error: Segmentation fault
  0x418978dc write_fbreg_variable
          ../../gcc-source/gcc/dwarf2codeview.cc:2648
  0x418978dc write_unoptimized_local_variable
          ../../gcc-source/gcc/dwarf2codeview.cc:3015
  0x418978dc write_unoptimized_function_vars
          ../../gcc-source/gcc/dwarf2codeview.cc:3238
  0x41899112 write_function
          ../../gcc-source/gcc/dwarf2codeview.cc:3789
  0x41899112 write_codeview_symbols
          ../../gcc-source/gcc/dwarf2codeview.cc:3841
  0x41899112 codeview_debug_finish()
          ../../gcc-source/gcc/dwarf2codeview.cc:5181
  0x40459318 dwarf2out_finish
          ../../gcc-source/gcc/dwarf2out.cc:32828

== Root cause ==

write_fbreg_variable (dwarf2codeview.cc:2582) does:

  const char *name = get_AT_string (die, DW_AT_name);

This returns NULL for artificial DWARF variables (they have DW_AT_artificial
but no DW_AT_name). Then line 2648:

  ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name) + 1);

dereferences NULL → segfault.

write_unoptimized_function_vars (line 3236-3238) iterates all DW_TAG_variable
and DW_TAG_formal_parameter children without checking DW_AT_artificial.

Structured bindings generate an artificial DW_TAG_variable for the hidden
binding object. Contracts generate artificial variables for contract checking.

== Conditions ==

- Only at -O0 (default). -O1, -Og, -O2, -O3, -Os all work fine.
- Only -gcodeview. Plain -g (DWARF) works fine.
- Only function-local. Global structured bindings compile fine.
- Structured bindings: any form (struct, array, reference) triggers it.
- Contracts: pre(), post(), contract_assert() all trigger it.
- Declaration-only contracts (no body) do not trigger it.

== Compiler version ==

  g++ (GCC) 16.0.1 20260302 (experimental)
  Target: x86_64-w64-mingw32
  Thread model: posix

Reply via email to