From 09588e93bbe486ce782de9fba604f5cd184ec446 Mon Sep 17 00:00:00 2001 From: Timm Baeder Date: Sun, 13 Apr 2025 12:25:29 +0200 Subject: [clang][bytecode] Fix an inconsistency with loop condition jumps (#135530) When emitting the jump for e.g. a for loop condition, we used to jump out of the CondScope, leaving the scope initialized, because we skipped the corresponding Destroy opcode. If that loop was in a loop itself, that outer loop could then iterate once more, leading to us initializing a scope that was still initialized. Fix this by also destroying the scope after the EndLabel. --- clang/lib/AST/ByteCode/Compiler.cpp | 48 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'clang/lib/AST/ByteCode/Compiler.cpp') diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 86b4358..2e22c85 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -5431,39 +5431,39 @@ bool Compiler::visitForStmt(const ForStmt *S) { this->fallthrough(CondLabel); this->emitLabel(CondLabel); - { - LocalScope CondScope(this); - if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) - if (!visitDeclStmt(CondDecl)) - return false; - - if (Cond) { - if (!this->visitBool(Cond)) - return false; - if (!this->jumpFalse(EndLabel)) - return false; - } - - if (!this->maybeEmitDeferredVarInit(S->getConditionVariable())) - return false; - - if (Body && !this->visitStmt(Body)) + // Start of loop body. + LocalScope CondScope(this); + if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) + if (!visitDeclStmt(CondDecl)) return false; - this->fallthrough(IncLabel); - this->emitLabel(IncLabel); - if (Inc && !this->discard(Inc)) + if (Cond) { + if (!this->visitBool(Cond)) return false; - - if (!CondScope.destroyLocals()) + if (!this->jumpFalse(EndLabel)) return false; } + if (!this->maybeEmitDeferredVarInit(S->getConditionVariable())) + return false; + + if (Body && !this->visitStmt(Body)) + return false; + + this->fallthrough(IncLabel); + this->emitLabel(IncLabel); + if (Inc && !this->discard(Inc)) + return false; + + if (!CondScope.destroyLocals()) + return false; if (!this->jump(CondLabel)) return false; + // End of loop body. - this->fallthrough(EndLabel); this->emitLabel(EndLabel); - return true; + // If we jumped out of the loop above, we still need to clean up the condition + // scope. + return CondScope.destroyLocals(); } template -- cgit v1.1