From: David Majnemer Date: Sat, 26 Jul 2014 05:41:31 +0000 (+0000) Subject: Parse: Don't crash on trailing whitespace before EOF X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=24b283005db37b259c30673d384807fed16fcbe5;p=platform%2Fupstream%2Fllvm.git Parse: Don't crash on trailing whitespace before EOF Parser::ParseDeclarationSpecifiers eagerly updates the source range of the DeclSpec with the current token position. However, it might not consume any more tokens. Fix this by only setting the start of the range, not the end. This way the SourceRange will be invalid if we don't consume any more tokens. This fixes PR20413. Differential Revision: http://reviews.llvm.org/D4646 llvm-svn: 214018 --- diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 1bbc894..727915c 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -2449,8 +2449,11 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, DeclSpecContext DSContext, LateParsedAttrList *LateAttrs) { if (DS.getSourceRange().isInvalid()) { + // Start the range at the current token but make the end of the range + // invalid. This will make the entire range invalid unless we successfully + // consume a token. DS.SetRangeStart(Tok.getLocation()); - DS.SetRangeEnd(Tok.getLocation()); + DS.SetRangeEnd(SourceLocation()); } bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); diff --git a/clang/test/Parser/eof2.cpp b/clang/test/Parser/eof2.cpp new file mode 100644 index 0000000..6a8b862 --- /dev/null +++ b/clang/test/Parser/eof2.cpp @@ -0,0 +1,15 @@ +// RUN: not %clang_cc1 %s -fsyntax-only 2>&1 | FileCheck %s + +// CHECK: error: expected expression +// CHECK: error: expected member name or ';' after declaration specifiers +// CHECK: error: expected '}' +// CHECK: note: to match this '{' +// CHECK: error: expected ';' after class +// CHECK: 4 errors generated. + +// Do not add anything to the end of this file. This requires the whitespace +// plus EOF after the '<' token. + +template +class a { + a<