r284777 - Fix off-by-one error in PPCaching.cpp token annotation assertion

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 20 13:53:20 PDT 2016


Author: rnk
Date: Thu Oct 20 15:53:20 2016
New Revision: 284777

URL: http://llvm.org/viewvc/llvm-project?rev=284777&view=rev
Log:
Fix off-by-one error in PPCaching.cpp token annotation assertion

This assert is intended to defend against backtracking into the middle
of a sequence of tokens that is being replaced with an annotation, but
it's OK if we backtrack to the exact position of the start of the
annotation sequence. Use a <= comparison instead of <.

Fixes PR25946

Added:
    cfe/trunk/test/Parser/backtrack-off-by-one.cpp
Modified:
    cfe/trunk/lib/Lex/PPCaching.cpp

Modified: cfe/trunk/lib/Lex/PPCaching.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPCaching.cpp?rev=284777&r1=284776&r2=284777&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPCaching.cpp (original)
+++ cfe/trunk/lib/Lex/PPCaching.cpp Thu Oct 20 15:53:20 2016
@@ -105,7 +105,7 @@ void Preprocessor::AnnotatePreviousCache
   for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
     CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
     if (AnnotBegin->getLocation() == Tok.getLocation()) {
-      assert((BacktrackPositions.empty() || BacktrackPositions.back() < i) &&
+      assert((BacktrackPositions.empty() || BacktrackPositions.back() <= i) &&
              "The backtrack pos points inside the annotated tokens!");
       // Replace the cached tokens with the single annotation token.
       if (i < CachedLexPos)

Added: cfe/trunk/test/Parser/backtrack-off-by-one.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/backtrack-off-by-one.cpp?rev=284777&view=auto
==============================================================================
--- cfe/trunk/test/Parser/backtrack-off-by-one.cpp (added)
+++ cfe/trunk/test/Parser/backtrack-off-by-one.cpp Thu Oct 20 15:53:20 2016
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -verify %s
+
+// PR25946
+// We had an off-by-one error in an assertion when annotating A<int> below.  Our
+// error recovery checks if A<int> is a constructor declarator, and opens a
+// TentativeParsingAction. Then we attempt to annotate the token at the exact
+// position that we want to possibly backtrack to, and this used to crash.
+
+template <typename T> class A {};
+
+// expected-error at +1 {{expected '{' after base class list}}
+template <typename T> class B : T // not ',' or '{'
+// expected-error at +3 {{C++ requires a type specifier for all declarations}}
+// expected-error at +2 {{expected ';' after top level declarator}}
+// expected-error at +1 {{expected ';' after class}}
+A<int> {
+};




More information about the cfe-commits mailing list