[clang] 0005438 - [clangd] Fix a crash when indexing invalid ObjC method declaration

Adam Czachorowski via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 25 06:55:51 PST 2021


Author: Adam Czachorowski
Date: 2021-01-25T15:43:11+01:00
New Revision: 00054382b95a9d95e2df6457e7fe1fca2323d287

URL: https://github.com/llvm/llvm-project/commit/00054382b95a9d95e2df6457e7fe1fca2323d287
DIFF: https://github.com/llvm/llvm-project/commit/00054382b95a9d95e2df6457e7fe1fca2323d287.diff

LOG: [clangd] Fix a crash when indexing invalid ObjC method declaration

This fix will make us not crash, but ideally we would handle this case
better.

Differential Revision: https://reviews.llvm.org/D94919

Added: 
    

Modified: 
    clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
    clang/lib/Sema/SemaCodeComplete.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
index 7e36cff7afa6..924cfd03cba7 100644
--- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1838,6 +1838,20 @@ TEST_F(SymbolCollectorTest, UndefOfModuleMacro) {
   EXPECT_THAT(TU.headerSymbols(), Not(Contains(QName("X"))));
 }
 
+TEST_F(SymbolCollectorTest, NoCrashOnObjCMethodCStyleParam) {
+  auto TU = TestTU::withCode(R"objc(
+    @interface Foo
+    - (void)fun:(bool)foo, bool bar;
+    @end
+  )objc");
+  TU.ExtraArgs.push_back("-xobjective-c++");
+
+  TU.build();
+  // We mostly care about not crashing.
+  EXPECT_THAT(TU.headerSymbols(),
+              UnorderedElementsAre(QName("Foo"), QName("Foo::fun:")));
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang

diff  --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index d77c9e43a9bd..c2785fd60fc2 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -3529,9 +3529,11 @@ CodeCompletionString *CodeCompletionResult::createCodeCompletionStringForDecl(
         Result.AddTypedTextChunk("");
     }
     unsigned Idx = 0;
+    // The extra Idx < Sel.getNumArgs() check is needed due to legacy C-style
+    // method parameters.
     for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(),
                                               PEnd = Method->param_end();
-         P != PEnd; (void)++P, ++Idx) {
+         P != PEnd && Idx < Sel.getNumArgs(); (void)++P, ++Idx) {
       if (Idx > 0) {
         std::string Keyword;
         if (Idx > StartParameter)


        


More information about the cfe-commits mailing list