Skip to content

[Clang] Invert emptiness check for correctness#178068

Merged
JustinStitt merged 1 commit intollvm:mainfrom
JustinStitt:cbl-92
Jan 27, 2026
Merged

[Clang] Invert emptiness check for correctness#178068
JustinStitt merged 1 commit intollvm:mainfrom
JustinStitt:cbl-92

Conversation

@JustinStitt
Copy link
Contributor

After #163885 landed, @zwuis pointed out a bug in a deferred call which was supposed to clear the DeclForInitializer field but was doing so incorrectly because the emptiness check is wrong.

Invert the check since the current check is wrong and an access to .back() is undefined behavior when the container is empty.

It is hard to add a test specifically for this change. It seems rather difficult to achieve the right circumstances to trigger this cleanup function with any valid source input. This check is more of a defensive check to make sure we aren't calling back() on an empty thing.

cc @zwuis

zwuis on github pointed out a bug [1] in a deferred call which was supposed
to clear the DeclForInitializer field but was doing so incorrectly
because the emptiness check needs to be inverted.

[1]: https://github.com/llvm/llvm-project/pull/163885/changes/BASE..082c9769209e80504a2205581a4387bcf39ca5df#r2726301659

Signed-off-by: Justin Stitt <justinstitt@google.com>
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 26, 2026
@JustinStitt JustinStitt requested a review from zwuis January 26, 2026 22:17
@llvmbot
Copy link
Member

llvmbot commented Jan 26, 2026

@llvm/pr-subscribers-clang

Author: Justin Stitt (JustinStitt)

Changes

After #163885 landed, @zwuis pointed out a bug in a deferred call which was supposed to clear the DeclForInitializer field but was doing so incorrectly because the emptiness check is wrong.

Invert the check since the current check is wrong and an access to .back() is undefined behavior when the container is empty.

It is hard to add a test specifically for this change. It seems rather difficult to achieve the right circumstances to trigger this cleanup function with any valid source input. This check is more of a defensive check to make sure we aren't calling back() on an empty thing.

cc @zwuis


Full diff: https://github.com/llvm/llvm-project/pull/178068.diff

1 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+1-1)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 0b3416a12f9f4..907b7b367f19b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13825,7 +13825,7 @@ void Sema::DiagnoseUniqueObjectDuplication(const VarDecl *VD) {
 
 void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
   llvm::scope_exit ResetDeclForInitializer([this]() {
-    if (this->ExprEvalContexts.empty())
+    if (!this->ExprEvalContexts.empty())
       this->ExprEvalContexts.back().DeclForInitializer = nullptr;
   });
 

@JustinStitt JustinStitt merged commit 828f144 into llvm:main Jan 27, 2026
14 checks passed
@JustinStitt JustinStitt deleted the cbl-92 branch January 27, 2026 17:17
stomfaig pushed a commit to stomfaig/llvm-project that referenced this pull request Jan 28, 2026
Fix a bug in a deferred call which was supposed to clear the `DeclForInitializer` field but was doing so incorrectly because the emptiness check needs to be inverted.
sshrestha-aa pushed a commit to sshrestha-aa/llvm-project that referenced this pull request Feb 4, 2026
Fix a bug in a deferred call which was supposed to clear the `DeclForInitializer` field but was doing so incorrectly because the emptiness check needs to be inverted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants