Fixed stack-overflow when parsing insanely nested compound statements.

Thanks to Clang's parser_overflow.cpp which has >16000 nested compound
statements to check exactly the same.

Change-Id: I2b604f8ceb01115d7fe950994e0677a081e99481
Reviewed-by: Christian Kamm <christian.d.kamm@nokia.com>
This commit is contained in:
Erik Verbruggen
2011-10-17 10:24:39 +02:00
parent 7e3d41da58
commit 29dff7221d
2 changed files with 9 additions and 1 deletions

View File

@@ -37,6 +37,7 @@
#define CPLUSPLUS_NO_DEBUG_RULE
#define MAX_EXPRESSION_DEPTH 100
#define MAX_STATEMENT_DEPTH 100
using namespace CPlusPlus;
@@ -181,7 +182,8 @@ Parser::Parser(TranslationUnit *unit)
_inFunctionBody(false),
_inObjCImplementationContext(false),
_inExpressionStatement(false),
_expressionDepth(0)
_expressionDepth(0),
_statementDepth(0)
{ }
Parser::~Parser()
@@ -3209,6 +3211,10 @@ bool Parser::parseCompoundStatement(StatementAST *&node)
{
DEBUG_THIS_RULE();
if (LA() == T_LBRACE) {
if (_statementDepth > MAX_STATEMENT_DEPTH)
return false;
++_statementDepth;
CompoundStatementAST *ast = new (_pool) CompoundStatementAST;
ast->lbrace_token = consumeToken();
@@ -3233,6 +3239,7 @@ bool Parser::parseCompoundStatement(StatementAST *&node)
}
match(T_RBRACE, &ast->rbrace_token);
node = ast;
--_statementDepth;
return true;
}
return false;

View File

@@ -315,6 +315,7 @@ private:
bool _inObjCImplementationContext: 1;
bool _inExpressionStatement: 1;
int _expressionDepth;
int _statementDepth;
MemoryPool _expressionStatementTempPool;
std::map<unsigned, TemplateArgumentListEntry> _templateArgumentList;