Commit 29dff722 authored by Erik Verbruggen's avatar Erik Verbruggen

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: default avatarChristian Kamm <christian.d.kamm@nokia.com>
parent 7e3d41da
......@@ -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;
......
......@@ -315,6 +315,7 @@ private:
bool _inObjCImplementationContext: 1;
bool _inExpressionStatement: 1;
int _expressionDepth;
int _statementDepth;
MemoryPool _expressionStatementTempPool;
std::map<unsigned, TemplateArgumentListEntry> _templateArgumentList;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment