[PATCH] D26664: [ObjC] Prevent infinite loops when iterating over redeclaration of a method that was declared in an invalid interface

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 15 04:53:05 PST 2016


arphaman created this revision.
arphaman added reviewers: manmanren, mehdi_amini.
arphaman added a subscriber: cfe-commits.
arphaman set the repository for this revision to rL LLVM.

This patch fixes an infinite loop that occurs when clang tries to iterate over redeclaration of a method that was declared in an invalid `@interface`. The existing validity checks don't catch this as that `@interface` is a duplicate of a previously declared valid `@interface` declaration, so we have to verify that the found redeclaration is in a valid declaration context.


Repository:
  rL LLVM

https://reviews.llvm.org/D26664

Files:
  lib/AST/DeclObjC.cpp
  test/SemaObjC/method-redecls-invalid-interface.m


Index: test/SemaObjC/method-redecls-invalid-interface.m
===================================================================
--- /dev/null
+++ test/SemaObjC/method-redecls-invalid-interface.m
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wdocumentation -Wno-objc-root-class %s
+// rdar://29220965
+
+ at interface InvalidInterface { // expected-note {{previous definition is here}}
+  int *_property;
+}
+
+ at end
+
+/*!
+ */
+
+ at interface InvalidInterface // expected-error {{duplicate interface definition for class 'InvalidInterface'}}
+ at property int *property;
+
+-(void) method;
+ at end
+
+ at implementation InvalidInterface
+-(void) method { }
+ at end
Index: lib/AST/DeclObjC.cpp
===================================================================
--- lib/AST/DeclObjC.cpp
+++ lib/AST/DeclObjC.cpp
@@ -832,6 +832,18 @@
   setParamsAndSelLocs(C, Params, SelLocs);
 }
 
+/// Ensures that the discovered method redeclaration has a valid declaration
+/// context. Used by ObjCMethodDecl::getNextRedeclarationImpl to prevent
+/// infinite loops when iterating redeclarations in a partially invalid AST.
+static ObjCMethodDecl *ensureRedeclHasValidContext(ObjCMethodDecl *Redecl) {
+  if (!Redecl)
+    return nullptr;
+  if (const auto *Ctx = cast<Decl>(Redecl->getDeclContext()))
+    if (Ctx->isInvalidDecl())
+      return nullptr;
+  return Redecl;
+}
+
 /// \brief A definition will return its interface declaration.
 /// An interface declaration will return its definition.
 /// Otherwise it will return itself.
@@ -849,24 +861,28 @@
     if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
       if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
         if (!ImplD->isInvalidDecl())
-          Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
+          Redecl = ensureRedeclHasValidContext(
+              ImplD->getMethod(getSelector(), isInstanceMethod()));
 
     } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
       if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
         if (!ImplD->isInvalidDecl())
-          Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
+          Redecl = ensureRedeclHasValidContext(
+              ImplD->getMethod(getSelector(), isInstanceMethod()));
 
     } else if (ObjCImplementationDecl *ImplD =
                  dyn_cast<ObjCImplementationDecl>(CtxD)) {
       if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
         if (!IFD->isInvalidDecl())
-          Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
+          Redecl = ensureRedeclHasValidContext(
+              IFD->getMethod(getSelector(), isInstanceMethod()));
 
     } else if (ObjCCategoryImplDecl *CImplD =
                  dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
       if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
         if (!CatD->isInvalidDecl())
-          Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
+          Redecl = ensureRedeclHasValidContext(
+              CatD->getMethod(getSelector(), isInstanceMethod()));
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26664.77980.patch
Type: text/x-patch
Size: 3103 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161115/4029f91c/attachment-0001.bin>


More information about the cfe-commits mailing list