Skip to content

Commit 58c3fdd

Browse files
committed
Fixed crash on garbage code introduced recently, optimized code in valueFlowFunctionReturn.
1 parent a4cc4c3 commit 58c3fdd

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

lib/valueflow.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,10 @@ static void valueFlowForLoop(TokenList *tokenlist, SymbolDatabase* symboldatabas
15311531
Token* tok = const_cast<Token*>(scope->classDef);
15321532
Token* const bodyStart = const_cast<Token*>(scope->classStart);
15331533

1534+
if (!Token::simpleMatch(tok->next()->astOperand2(), ";") ||
1535+
!Token::simpleMatch(tok->next()->astOperand2()->astOperand2(), ";"))
1536+
continue;
1537+
15341538
unsigned int varid(0);
15351539
MathLib::bigint num1(0), num2(0), numAfter(0);
15361540

@@ -1632,6 +1636,15 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg
16321636
if (tok->str() != "(" || !tok->astOperand1() || !tok->astOperand1()->function())
16331637
continue;
16341638

1639+
// Get scope and args of function
1640+
const Function * const function = tok->astOperand1()->function();
1641+
const Scope * const functionScope = function->functionScope;
1642+
if (!functionScope || !Token::simpleMatch(functionScope->classStart, "{ return")) {
1643+
if (functionScope && settings->debugwarnings)
1644+
bailout(tokenlist, errorLogger, tok, "function return; nontrivial function body");
1645+
continue;
1646+
}
1647+
16351648
// Arguments..
16361649
std::vector<MathLib::bigint> parvalues;
16371650
{
@@ -1650,15 +1663,6 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg
16501663
continue;
16511664
}
16521665

1653-
// Get scope and args of function
1654-
const Function * const function = tok->astOperand1()->function();
1655-
const Scope * const functionScope = function ? function->functionScope : nullptr;
1656-
if (!functionScope || !Token::simpleMatch(functionScope->classStart, "{ return")) {
1657-
if (functionScope && settings->debugwarnings)
1658-
bailout(tokenlist, errorLogger, tok, "function return; nontrivial function body");
1659-
continue;
1660-
}
1661-
16621666
std::map<unsigned int, MathLib::bigint> programMemory;
16631667
for (std::size_t i = 0; i < parvalues.size(); ++i) {
16641668
const Variable * const arg = function->getArgumentVar(i);

test/testgarbage.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ class TestGarbage : public TestFixture {
400400
// 6122 survive garbage code
401401
code = "; { int i ; for ( i = 0 ; = 123 ; ) - ; }";
402402
checkCode(code);
403+
404+
code = "void f1() { for (int n = 0 n < 10 n++); }";
405+
checkCode(code);
403406
}
404407

405408
void garbageSymbolDatabase() {

0 commit comments

Comments
 (0)