[clang] [Clang] Fix stack-use-after-return in TryArrayCopy by allocating OpaqueValueExpr on the ASTContext (PR #192080)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 14 09:52:26 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: TPPPP (TPPPP72)
<details>
<summary>Changes</summary>
Change the `OpaqueValueExpr` in `TryArrayCopy` from stack memory to heap memory to avoid stack-use-after-return.
close #<!-- -->192026
---
Full diff: https://github.com/llvm/llvm-project/pull/192080.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaInit.cpp (+4-4)
- (added) clang/test/SemaCXX/gh192026.cpp (+16)
``````````diff
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index e54a25405c816..991e7d42bdb87 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4380,10 +4380,10 @@ static void TryArrayCopy(Sema &S, const InitializationKind &Kind,
InitializedEntity::InitializeElement(S.Context, 0, Entity);
QualType InitEltT =
S.Context.getAsArrayType(Initializer->getType())->getElementType();
- OpaqueValueExpr OVE(Initializer->getExprLoc(), InitEltT,
- Initializer->getValueKind(),
- Initializer->getObjectKind());
- Expr *OVEAsExpr = &OVE;
+ OpaqueValueExpr *OVE = new (S.Context) OpaqueValueExpr(
+ Initializer->getExprLoc(), InitEltT, Initializer->getValueKind(),
+ Initializer->getObjectKind());
+ Expr *OVEAsExpr = OVE;
Sequence.InitializeFrom(S, Element, Kind, OVEAsExpr,
/*TopLevelOfInitList*/ false,
TreatUnavailableAsInvalid);
diff --git a/clang/test/SemaCXX/gh192026.cpp b/clang/test/SemaCXX/gh192026.cpp
new file mode 100644
index 0000000000000..3b179f8420119
--- /dev/null
+++ b/clang/test/SemaCXX/gh192026.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+struct ControlSwitcher { bool b; };
+
+class ComplexChain {
+ volatile union {
+ char flag_byte;
+ int ref_count;
+ } state_flags[5]; // expected-note {{copy constructor of 'ComplexChain' is implicitly deleted because field 'state_flags' has no copy constructor}}
+
+ ControlSwitcher cs{true};
+
+ ComplexChain trigger_bug() {
+ return *this; // expected-error {{call to implicitly-deleted copy constructor of 'ComplexChain'}}
+ }
+};
``````````
</details>
https://github.com/llvm/llvm-project/pull/192080
More information about the cfe-commits
mailing list