From: Jonas Hansen <[email protected]>
With -ftrivial-auto-var-init, an anonymous temporary can be initialized
by an SSA name defined by .DEFERRED_INIT. Inlining gives the destination
declaration a new DECL_UID while the call retains the original D.N name.
warn_uninit then fails to recognize the artificial initialization store
and diagnoses it as an uninitialized read. This occurs, for example,
with a reference bound to a conversion temporary when ASan, UBSan and
-ftrivial-auto-var-init=zero are enabled.
Keep the existing current-UID comparison and also check DECL_ORIGIN when
recognizing this artificial store. Add regression tests for zero and
pattern initialization.
gcc/ChangeLog:
PR middle-end/124380
* tree-ssa-uninit.cc (warn_uninit): Also match the original
declaration's UID when recognizing the artificial initialization
of an anonymous variable after inlining.
gcc/testsuite/ChangeLog:
PR middle-end/124380
* g++.dg/asan/auto-init-uninit-1.C: New test.
* g++.dg/asan/auto-init-uninit-2.C: New test.
Signed-off-by: Jonas Hansen <[email protected]>
---
Fixes [PR
middle-end/124380](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124380),
a false `-Wuninitialized` warning for an initialized temporary bound to a
const reference in an inlined function.
Inlining changes the temporary's `DECL_UID`, while its `.DEFERRED_INIT`
call retains the original name. The warning pass then mistakes the
compiler-inserted initialization store for an uninitialized read.
Preserve the existing current-UID comparison and also check `DECL_ORIGIN`.
Two regression tests cover zero and pattern initialization with ASan
and UBSan. Both fail before the fix and pass afterward. The smaller
PR124380 reproducer also passes with both initialization modes,
without sanitizers.
Validation on x86_64-pc-linux-gnu:
- C and C++ compilers built; compiler self-tests passed.
- Targeted warning, automatic-initialization and sanitizer tests:
2,565 expected passes, 26 expected failures, 16 unsupported tests,
and no unexpected results.
- Comparisons across 192 configurations preserved genuine uninitialized
warnings and produced identical assembly.
- Additional forced-option warning-suite and LTO comparisons produced
unchanged results.
Testing was targeted; a complete bootstrap and full GCC testsuite run
were not performed. A pre-existing LTO form of the false warning remains
with `-flto`; this patch does not address UID renumbering across LTO
streaming.
This is a forge pull request published on the gcc-patches mailing list mailing
list
as requested by Jonas Hansen via Sourceware Forge
<[email protected]>.
Forge discussion: https://forge.sourceware.org/gcc/gcc/pulls/252
Get it locally using:
```
git fetch forge-upstream "+refs/versioned_pull/252/*:refs/versioned_pull/252/*"
git switch -c "pr-252-v1" "refs/versioned_pull/252/1/head"
```
Or, download the patch at: https://forge.sourceware.org/gcc/gcc/pulls/252.diff
Created on: 2026-10-22 05:19:48+00:00
Latest update: 2026-10-22 08:18:35+00:00
Changes: 3 changed files, 33 additions, 0 deletions
Head revision: hrjonashansen/gcc ref fix-uninit-inlined-temporary commit
060a44135bea892a5b2f869f921d4adb3bc7e6e5
Base revision: gcc/gcc ref trunk commit
2d1f1bc18a59c0f106c551304ac459ad7781a711 r17-5299-g2d1f1bc18a59c0
Merge base: 2d1f1bc18a59c0f106c551304ac459ad7781a711
Requested Reviewers: RichardBiener, pinskia
Changed files:
- A: gcc/testsuite/g++.dg/asan/auto-init-uninit-1.C
- A: gcc/testsuite/g++.dg/asan/auto-init-uninit-2.C
- M: gcc/tree-ssa-uninit.cc
.../g++.dg/asan/auto-init-uninit-1.C | 24 +++++++++++++++++++
.../g++.dg/asan/auto-init-uninit-2.C | 4 ++++
gcc/tree-ssa-uninit.cc | 5 ++++
3 files changed, 33 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/asan/auto-init-uninit-1.C
create mode 100644 gcc/testsuite/g++.dg/asan/auto-init-uninit-2.C
diff --git a/gcc/testsuite/g++.dg/asan/auto-init-uninit-1.C
b/gcc/testsuite/g++.dg/asan/auto-init-uninit-1.C
new file mode 100644
index 0000000000000..5585558f63674
--- /dev/null
+++ b/gcc/testsuite/g++.dg/asan/auto-init-uninit-1.C
@@ -0,0 +1,24 @@
+// { dg-do compile { target c++14 } }
+// { dg-options "-O2 -Wuninitialized -fsanitize=undefined
-ftrivial-auto-var-init=zero" }
+
+template <typename T>
+const T &
+max (const T &a, const T &b)
+{
+ return a < b ? b : a;
+}
+
+template <typename A, typename B>
+auto
+max_mixed (const A &a, const B &b)
+{
+ const unsigned long &x = a;
+ const unsigned long &y = b;
+ return max (x, y);
+}
+
+unsigned long
+f (unsigned long a)
+{
+ return max_mixed (a, 1);
+}
diff --git a/gcc/testsuite/g++.dg/asan/auto-init-uninit-2.C
b/gcc/testsuite/g++.dg/asan/auto-init-uninit-2.C
new file mode 100644
index 0000000000000..b0ce10863824f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/asan/auto-init-uninit-2.C
@@ -0,0 +1,4 @@
+// { dg-do compile { target c++14 } }
+// { dg-options "-O2 -Wuninitialized -fsanitize=undefined
-ftrivial-auto-var-init=pattern" }
+
+#include "auto-init-uninit-1.C"
diff --git a/gcc/tree-ssa-uninit.cc b/gcc/tree-ssa-uninit.cc
index 7d242ab7530a0..9ff5432979008 100644
--- a/gcc/tree-ssa-uninit.cc
+++ b/gcc/tree-ssa-uninit.cc
@@ -266,6 +266,11 @@ warn_uninit (opt_code opt, tree t, tree var, gimple
*context,
{
char lhs_var_name_str_buf[3 + (HOST_BITS_PER_INT + 2) / 3];
sprintf (lhs_var_name_str_buf, "D.%u", DECL_UID (lhs_var));
+ /* Inlining changes DECL_UID, but not the name recorded in
+ the DEFERRED_INIT call. Also check the original decl. */
+ if (strcmp (lhs_var_name_str_buf, var_name_str) != 0)
+ sprintf (lhs_var_name_str_buf, "D.%u",
+ DECL_UID (DECL_ORIGIN (lhs_var)));
if (strcmp (lhs_var_name_str_buf, var_name_str) == 0)
return;
}
--
2.54.0