[clang] [clang-repl] fix segfault in CleanUpPTU() (PR #75629)
    via cfe-commits 
    cfe-commits at lists.llvm.org
       
    Fri Dec 15 09:09:45 PST 2023
    
    
  
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Pavel Kalugin (p4vook)
<details>
<summary>Changes</summary>
Check if the last translation unit or its first declaration are actually empty and do not nead cleanup.
Previously this caused segmentation fault on empty PTUs.
Add a regression test.
Fixes: #<!-- -->72980
---
Full diff: https://github.com/llvm/llvm-project/pull/75629.diff
2 Files Affected:
- (modified) clang/lib/Interpreter/IncrementalParser.cpp (+8) 
- (added) clang/test/Interpreter/anonymous-scope-fail.cpp (+10) 
``````````diff
diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 370bcbfee8b014..f894af881134bb 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -373,7 +373,15 @@ std::unique_ptr<llvm::Module> IncrementalParser::GenModule() {
 
 void IncrementalParser::CleanUpPTU(PartialTranslationUnit &PTU) {
   TranslationUnitDecl *MostRecentTU = PTU.TUPart;
+  if (!MostRecentTU) {
+    return;
+  }
+
   TranslationUnitDecl *FirstTU = MostRecentTU->getFirstDecl();
+  if (!FirstTU) {
+    return;
+  }
+
   if (StoredDeclsMap *Map = FirstTU->getPrimaryContext()->getLookupPtr()) {
     for (auto I = Map->begin(); I != Map->end(); ++I) {
       StoredDeclsList &List = I->second;
diff --git a/clang/test/Interpreter/anonymous-scope-fail.cpp b/clang/test/Interpreter/anonymous-scope-fail.cpp
new file mode 100644
index 00000000000000..c32b42d2859d97
--- /dev/null
+++ b/clang/test/Interpreter/anonymous-scope-fail.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-repl "int x = 10;" "{ int t; a::b(t); }" "int y = 10;"
+// REQUIRES: host-supports-jit
+// UNSUPPORTED: system-aix
+// RUN: cat %s | not clang-repl | FileCheck %s
+{ int t; a::b(t); }
+extern "C" int printf(const char *, ...);
+int i = 42;
+auto r1 = printf("i = %d\n", i);
+// CHECK: i = 42
+%quit
``````````
</details>
https://github.com/llvm/llvm-project/pull/75629
    
    
More information about the cfe-commits
mailing list