r283145 - ObjectiveC: fix a seg fault when deserialing redeclaration of ObjCMethodDecl.

Manman Ren via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 3 14:26:48 PDT 2016


Author: mren
Date: Mon Oct  3 16:26:46 2016
New Revision: 283145

URL: http://llvm.org/viewvc/llvm-project?rev=283145&view=rev
Log:
ObjectiveC: fix a seg fault when deserialing redeclaration of ObjCMethodDecl.

The deserialization of redeclartion can cause seg fault since getCanonicalDecl
of the redeclaration returns the lookup result on the ObjCContainerDecl,
which can be null if FindExternalVisibleDeclsByName is not done updating
the lookup results.

The fix is to return the redeclaration itself as the canonical decl. Note that
the handling for redeclaration of ObjCMethodDecl is not in line with other
redeclarables.

rdar://28488466

Added:
    cfe/trunk/test/Modules/Inputs/objc-method-redecl.h
    cfe/trunk/test/Modules/objc-method-redecl.m
Modified:
    cfe/trunk/lib/AST/DeclObjC.cpp

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=283145&r1=283144&r2=283145&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Mon Oct  3 16:26:46 2016
@@ -897,9 +897,13 @@ ObjCMethodDecl *ObjCMethodDecl::getCanon
         return MD;
   }
 
-  if (isRedeclaration())
-    return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
-                                                    isInstanceMethod());
+  if (isRedeclaration()) {
+    // It is possible that we have not done deserializing the ObjCMethod yet.
+    ObjCMethodDecl *MD =
+        cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
+                                                 isInstanceMethod());
+    return MD ? MD : this;
+  }
 
   return this;
 }

Added: cfe/trunk/test/Modules/Inputs/objc-method-redecl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/objc-method-redecl.h?rev=283145&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/objc-method-redecl.h (added)
+++ cfe/trunk/test/Modules/Inputs/objc-method-redecl.h Mon Oct  3 16:26:46 2016
@@ -0,0 +1,4 @@
+ at interface T
+- (void)test;
+- (void)test;
+ at end

Added: cfe/trunk/test/Modules/objc-method-redecl.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/objc-method-redecl.m?rev=283145&view=auto
==============================================================================
--- cfe/trunk/test/Modules/objc-method-redecl.m (added)
+++ cfe/trunk/test/Modules/objc-method-redecl.m Mon Oct  3 16:26:46 2016
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t -x objective-c-header -emit-pch %S/Inputs/objc-method-redecl.h -o %t.pch -Wno-objc-root-class
+// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t -x objective-c -include-pch %t.pch %s -verify -Wno-objc-root-class
+// expected-no-diagnostics
+
+ at implementation T
+- (void)test {
+}
+ at end




More information about the cfe-commits mailing list