Skip to content

Commit 851c248

Browse files
committed
[clang] Prevent possible use-after-free
This prevents further parsing of tokens (that'll be freed) inside method body by propagating EOF emitted by reaching code completion token up the parsing stack. Differential Revision: https://reviews.llvm.org/D158269
1 parent 59c59a3 commit 851c248

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

clang/lib/Parse/ParseObjc.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3764,6 +3764,8 @@ void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
37643764
while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
37653765
ConsumeAnyToken();
37663766
}
3767-
// Clean up the remaining EOF token.
3768-
ConsumeAnyToken();
3767+
// Clean up the remaining EOF token, only if it's inserted by us. Otherwise
3768+
// this might be code-completion token, which must be propagated to callers.
3769+
if (Tok.is(tok::eof) && Tok.getEofData() == MCDecl)
3770+
ConsumeAnyToken();
37693771
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Make sure we don't trigger use-after-free when we encounter a code completion
2+
// token inside a objc method.
3+
@interface Foo
4+
@end
5+
6+
@implementation Foo
7+
- (void)foo {
8+
9+
// RUN: %clang_cc1 -fsyntax-only -Wno-objc-root-class -code-completion-at=%s:%(line-1):1 %s | FileCheck %s
10+
// CHECK: COMPLETION: self : [#Foo *#]self
11+
[self foo];
12+
}
13+
@end

0 commit comments

Comments
 (0)