Skip to content

Commit e0838ff

Browse files
committed
Fixed crash on garbage code (cppcheck-opensource#7699)
1 parent d982dc5 commit e0838ff

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
362362
setTokenValue(parent,value);
363363
}
364364

365-
else if (parent->str() == "?" && tok->str() == ":" && tok == parent->astOperand2()) {
365+
else if (parent->str() == "?" && tok->str() == ":" && tok == parent->astOperand2() && parent->astOperand1()) {
366366
// is condition always true/false?
367367
if (parent->astOperand1()->values.size() == 1U && parent->astOperand1()->values.front().isKnown()) {
368368
const ValueFlow::Value &condvalue = parent->astOperand1()->values.front();

test/testgarbage.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class TestGarbage : public TestFixture {
232232
TEST_CASE(garbageCode181);
233233
TEST_CASE(garbageCode182); // #4195
234234
TEST_CASE(garbageCode183); // #7505
235+
TEST_CASE(garbageCode184); // #7699
235236
TEST_CASE(garbageValueFlow);
236237
TEST_CASE(garbageSymbolDatabase);
237238
TEST_CASE(garbageAST);
@@ -1521,6 +1522,13 @@ class TestGarbage : public TestFixture {
15211522
ASSERT_THROW(checkCode("= { int } enum return { r = f() f(); }"), InternalError);
15221523
}
15231524

1525+
void garbageCode184() { // #7699
1526+
checkCode("unsigned int AquaSalSystem::GetDisplayScreenCount() {\n"
1527+
" NSArray* pScreens = [NSScreen screens];\n"
1528+
" return pScreens ? [pScreens count] : 1;\n"
1529+
"}");
1530+
}
1531+
15241532
};
15251533

15261534
REGISTER_TEST(TestGarbage)

0 commit comments

Comments
 (0)