From 283019a44aee23d2c99c7012451d35ba56b46f28 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Mon, 31 May 2010 16:00:59 +0200
Subject: [PATCH] Fixed possible crash when parsing template declarations at
 the end of a C++ file.

---
 src/shared/cplusplus/Parser.cpp | 12 ++++++++++--
 src/shared/cplusplus/Parser.h   |  3 +--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/shared/cplusplus/Parser.cpp b/src/shared/cplusplus/Parser.cpp
index 325135838ef..6bde85f2e33 100644
--- a/src/shared/cplusplus/Parser.cpp
+++ b/src/shared/cplusplus/Parser.cpp
@@ -986,7 +986,7 @@ bool Parser::parseTemplateDeclaration(DeclarationAST *&node)
             match(T_GREATER, &ast->greater_token);
     }
 
-    do {
+    while (LA()) {
         unsigned start_declaration = cursor();
 
         ast->declaration = 0;
@@ -996,7 +996,7 @@ bool Parser::parseTemplateDeclaration(DeclarationAST *&node)
         _translationUnit->error(start_declaration, "expected a declaration");
         rewind(start_declaration + 1);
         skipUntilDeclaration();
-    } while (LA());
+    }
 
     node = ast;
     return true;
@@ -5825,3 +5825,11 @@ bool Parser::parseTrailingTypeSpecifierSeq(SpecifierListAST *&node)
     DEBUG_THIS_RULE();
     return parseSimpleTypeSpecifier(node);
 }
+
+void Parser::rewind(unsigned cursor)
+{
+    if (cursor < _translationUnit->tokenCount())
+        _tokenIndex = cursor;
+    else
+        _tokenIndex = _translationUnit->tokenCount() - 1;
+}
diff --git a/src/shared/cplusplus/Parser.h b/src/shared/cplusplus/Parser.h
index 4ff708b1cc6..c383c2bda94 100644
--- a/src/shared/cplusplus/Parser.h
+++ b/src/shared/cplusplus/Parser.h
@@ -310,8 +310,7 @@ public:
     inline unsigned cursor() const
     { return _tokenIndex; }
 
-    inline void rewind(unsigned cursor)
-    { _tokenIndex = cursor; }
+    void rewind(unsigned cursor);
 
     struct TemplateArgumentListEntry {
         unsigned index;
-- 
GitLab