https://gcc.gnu.org/g:364174970369811c56b7491e37927b1ce3cda119

commit r17-6606-g364174970369811c56b7491e37927b1ce3cda119
Author: Oleg Tolmatcev <[email protected]>
Date:   Mon Nov 23 11:15:21 2026 -0700

    [PATCH] codeview: Skip local variables without names [PR124432]
    
    Artificial local variables can have no DW_AT_name.  Emitting their
    CodeView symbol records dereferences a null pointer.
    
    Skip unnamed locals in the unoptimized symbol writers and in
    write_optimized_local_variable.  Skip optimized locals before emitting
    both S_LOCAL and its location ranges so no orphaned S_DEFRANGE records
    are emitted.
    
            PR debug/124432
    
    gcc/ChangeLog:
    
            * dwarf2codeview.cc (write_local_s_ldata32): Skip unnamed variables.
            (write_s_register): Likewise.
            (write_fbreg_variable): Likewise.
            (write_optimized_local_variable): Likewise, including their location
            ranges.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/debug/codeview/codeview.exp: New test driver.
            * g++.dg/debug/codeview/pr124432.C: New test.
            * g++.dg/debug/codeview/pr124432-2.C: Likewise.
    
    Signed-off-by: Oleg Tolmatcev <[email protected]>

Diff:
---
 gcc/dwarf2codeview.cc                            | 12 ++++++
 gcc/testsuite/g++.dg/debug/codeview/codeview.exp | 48 ++++++++++++++++++++++++
 gcc/testsuite/g++.dg/debug/codeview/pr124432-2.C | 22 +++++++++++
 gcc/testsuite/g++.dg/debug/codeview/pr124432.C   | 17 +++++++++
 4 files changed, 99 insertions(+)

diff --git a/gcc/dwarf2codeview.cc b/gcc/dwarf2codeview.cc
index 014c35053a5e..dae861973501 100644
--- a/gcc/dwarf2codeview.cc
+++ b/gcc/dwarf2codeview.cc
@@ -2331,6 +2331,9 @@ write_local_s_ldata32 (dw_die_ref die, dw_loc_descr_ref 
loc_ref)
   const char *name = get_AT_string (die, DW_AT_name);
   uint32_t type;
 
+  if (!name)
+    return;
+
   /* This is struct datasym in binutils:
 
       struct datasym
@@ -2522,6 +2525,9 @@ write_s_register (dw_die_ref die, dw_loc_descr_ref 
loc_ref)
   uint16_t regno;
   uint32_t type;
 
+  if (!name)
+    return;
+
   /* This is struct regsym in binutils and REGSYM in Microsoft's cvinfo.h:
 
     struct regsym
@@ -2584,6 +2590,9 @@ write_fbreg_variable (dw_die_ref die, dw_loc_descr_ref 
loc_ref,
   uint16_t regno;
   int offset;
 
+  if (!name)
+    return;
+
   /* This is struct regrel in binutils and REGREL32 in Microsoft's cvinfo.h:
 
     struct regrel
@@ -2908,6 +2917,9 @@ write_optimized_local_variable (dw_die_ref die, 
dw_loc_descr_ref fbloc,
   dw_attr_node *loc;
   dw_loc_list_ref loc_list;
 
+  if (!get_AT_string (die, DW_AT_name))
+    return;
+
   loc = get_AT (die, DW_AT_location);
   if (!loc)
     return;
diff --git a/gcc/testsuite/g++.dg/debug/codeview/codeview.exp 
b/gcc/testsuite/g++.dg/debug/codeview/codeview.exp
new file mode 100644
index 000000000000..85dab59a0390
--- /dev/null
+++ b/gcc/testsuite/g++.dg/debug/codeview/codeview.exp
@@ -0,0 +1,48 @@
+# Copyright (C) 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, 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 GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# G++ testsuite that uses the `dg.exp' driver.
+
+# Load support procs.
+load_lib g++-dg.exp
+
+if {![istarget i*86-*-mingw*]
+  && ![istarget x86_64-*-mingw*]} {
+    return 0
+}
+
+# If a testcase doesn't have special options, use these.
+global DEFAULT_CXXFLAGS
+if ![info exists DEFAULT_CXXFLAGS] then {
+    set DEFAULT_CXXFLAGS " -pedantic-errors -Wno-long-long"
+}
+
+# Initialize `dg'.
+dg-init
+
+# Main loop.
+set comp_output [g++_target_compile \
+    "$srcdir/$subdir/../trivial.C" "trivial.S" assembly \
+    "additional_flags=-gcodeview"]
+if { ! [string match "*: target system does not support the * debug format*" \
+    $comp_output] } {
+    remove-build-file "trivial.S"
+    g++-dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[CH\]]] \
+           "" $DEFAULT_CXXFLAGS
+}
+
+# All done.
+dg-finish
diff --git a/gcc/testsuite/g++.dg/debug/codeview/pr124432-2.C 
b/gcc/testsuite/g++.dg/debug/codeview/pr124432-2.C
new file mode 100644
index 000000000000..84e89c4ed4ba
--- /dev/null
+++ b/gcc/testsuite/g++.dg/debug/codeview/pr124432-2.C
@@ -0,0 +1,22 @@
+// PR debug/124432
+// { dg-do compile }
+// { dg-options "-std=c++17 -O2 -gcodeview" }
+
+// write_optimized_local_variable must omit the unnamed structured-binding
+// variable and its location range, retaining only those for a.
+
+struct S
+{
+  int a;
+};
+
+void
+f ()
+{
+  auto [a] = S {};
+  asm volatile ("" : "+m" (a));
+}
+
+// One S_LOCAL and one S_DEFRANGE_REGISTER_REL for a.
+// { dg-final { scan-assembler-times "0x113e" 1 } }
+// { dg-final { scan-assembler-times "0x1145" 1 } }
diff --git a/gcc/testsuite/g++.dg/debug/codeview/pr124432.C 
b/gcc/testsuite/g++.dg/debug/codeview/pr124432.C
new file mode 100644
index 000000000000..847d11ee39d2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/debug/codeview/pr124432.C
@@ -0,0 +1,17 @@
+// PR debug/124432
+// { dg-do compile }
+// { dg-options "-std=c++17 -O0 -gcodeview" }
+
+// write_fbreg_variable must not dereference a missing DW_AT_name for the
+// artificial structured-binding variable.
+
+struct S
+{
+  int a, b;
+};
+
+void
+f ()
+{
+  auto [a, b] = S {};
+}

Reply via email to