[PATCH] D34185: [Parser][ObjC] Avoid crashing when skipping to EOF while parsing an ObjC interface/implementation

Alex Lorenz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 13 16:55:39 PDT 2017


arphaman created this revision.

This patch fixes a Clang crash that happens when an Objective-C source code contains an `@interface`/`@implementation` declaration that follows unterminated `@implementation` declaration that contains a method with a message send that doesn't have the ']'. The crash happens because the `@interface`/`@implementation` parsing methods encounter an unexpected EOF that is caused by parser's attempt to recover from the missing ']' by skipping to the next ']'/';' (it reaches EOF instead since these tokens are not present).


Repository:
  rL LLVM

https://reviews.llvm.org/D34185

Files:
  lib/Parse/ParseObjc.cpp
  test/Parser/objc-at-implementation-eof-crash.m
  test/Parser/objc-at-interface-eof-crash.m


Index: test/Parser/objc-at-interface-eof-crash.m
===================================================================
--- /dev/null
+++ test/Parser/objc-at-interface-eof-crash.m
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -verify -Wno-objc-root-class %s
+
+ at interface ClassA
+
+- (void)fileExistsAtPath:(int)x;
+
+ at end
+
+ at interface ClassB
+
+ at end
+
+ at implementation ClassB // expected-note {{implementation started here}}
+
+- (void) method:(ClassA *)mgr { // expected-note {{to match this '{'}}
+  mgr fileExistsAtPath:0
+} // expected-error {{expected ']'}}
+
+ at interface ClassC // expected-error {{missing '@end'}}
+
+ at end // expected-error {{expected '}'}}
Index: test/Parser/objc-at-implementation-eof-crash.m
===================================================================
--- /dev/null
+++ test/Parser/objc-at-implementation-eof-crash.m
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -verify -Wno-objc-root-class %s
+
+ at interface ClassA
+
+- (void)fileExistsAtPath:(int)x;
+
+ at end
+
+ at interface ClassB
+
+ at end
+
+ at implementation ClassB // expected-note {{implementation started here}}
+
+- (void) method:(ClassA *)mgr { // expected-note {{to match this '{'}}
+  mgr fileExistsAtPath:0
+} // expected-error {{expected ']'}}
+
+ at implementation ClassC // expected-error {{missing '@end'}}
+
+ at end // expected-error {{expected '}'}}
Index: lib/Parse/ParseObjc.cpp
===================================================================
--- lib/Parse/ParseObjc.cpp
+++ lib/Parse/ParseObjc.cpp
@@ -217,6 +217,8 @@
   assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
          "ParseObjCAtInterfaceDeclaration(): Expected @interface");
   CheckNestedObjCContexts(AtLoc);
+  if (isEofOrEom())
+    return nullptr;
   ConsumeToken(); // the "interface" identifier
 
   // Code completion after '@interface'.
@@ -2101,6 +2103,8 @@
   assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
          "ParseObjCAtImplementationDeclaration(): Expected @implementation");
   CheckNestedObjCContexts(AtLoc);
+  if (isEofOrEom())
+    return nullptr;
   ConsumeToken(); // the "implementation" identifier
 
   // Code completion after '@implementation'.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34185.102453.patch
Type: text/x-patch
Size: 2131 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170613/793cd405/attachment.bin>


More information about the cfe-commits mailing list