Author: Ivy Xu
Date: 2026-10-09T11:09:09+03:00
New Revision: 6e92994f7a95d11cf450f6bec62104a0028b83cb

URL: 
https://github.com/llvm/llvm-project/commit/6e92994f7a95d11cf450f6bec62104a0028b83cb
DIFF: 
https://github.com/llvm/llvm-project/commit/6e92994f7a95d11cf450f6bec62104a0028b83cb.diff

LOG: [clang-repl] Fix crashing on unusable top-level declarations (#230054)

This is a regression from PR #150215.

After #150215, if R was an unusable statement (e.g., parsed from a
top-level return statement), `R =
Actions.ActOnNullStmt(Tok.getLocation());` updates the local variable
R's value, but the value was never used.
This resulted in a null pointer dereference because `TLSD->setStmt()`
was never called, leaving `TLSD->getStmt() == nullptr`.

This PR correctly sets the `TLSD.Statement`'s value when parsing
unusable top-level declarations.

Fixes #229339.

Added: 
    

Modified: 
    clang/lib/Parse/ParseDecl.cpp
    clang/test/Interpreter/execute.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 2f326451c3f13d..a3567d618cfa52 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -5873,8 +5873,10 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
   TopLevelStmtDecl *TLSD = Actions.ActOnStartTopLevelStmtDecl(getCurScope());
   StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
   Actions.ActOnFinishTopLevelStmtDecl(TLSD, R.get());
-  if (!R.isUsable())
+  if (!R.isUsable()) {
     R = Actions.ActOnNullStmt(Tok.getLocation());
+    TLSD->setStmt(R.get());
+  }
 
   if (Tok.is(tok::annot_repl_input_end) &&
       Tok.getAnnotationValue() != nullptr) {

diff  --git a/clang/test/Interpreter/execute.cpp 
b/clang/test/Interpreter/execute.cpp
index 82cd70a93980a7..65dbc2d70a90f1 100644
--- a/clang/test/Interpreter/execute.cpp
+++ b/clang/test/Interpreter/execute.cpp
@@ -19,3 +19,7 @@ auto r2 = printf("S[f=%f, m=0x%llx]\n", s.f, 
reinterpret_cast<unsigned long long
 
 inline int foo() { return 42; }
 int r3 = foo();
+
+return;
+printf("not crashed\n");
+// CHECK-NEXT: not crashed


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to