Skip to content

Commit 56544ac

Browse files
committed
Fixed cppcheck-opensource#8211 (Crash on incomplete code in isVariableChanged ())
1 parent 9f4fc7b commit 56544ac

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎lib/astutils.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ bool isVariableChanged(const Token *start, const Token *end, const unsigned int
457457
if (Token::simpleMatch(shr->astParent(), ">>"))
458458
return true;
459459
const Token *lhs = shr->astOperand1();
460-
if (!lhs->valueType() || !lhs->valueType()->isIntegral())
460+
if (!lhs || !lhs->valueType() || !lhs->valueType()->isIntegral())
461461
return true;
462462
}
463463

‎test/testastutils.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TestAstUtils : public TestFixture {
3434

3535
void run() {
3636
TEST_CASE(isReturnScope);
37+
TEST_CASE(isVariableChanged);
3738
}
3839

3940
bool isReturnScope(const char code[], int offset) {
@@ -54,6 +55,25 @@ class TestAstUtils : public TestFixture {
5455
ASSERT_EQUALS(true, isReturnScope("void f() { if (a) { {throw new string(x);}; } }", -4)); // #7144
5556
ASSERT_EQUALS(true, isReturnScope("void f() { if (a) { {throw new string(x);}; } }", -2)); // #7144
5657
}
58+
59+
bool isVariableChanged(const char code[], const char startPattern[], const char endPattern[]) {
60+
Settings settings;
61+
Tokenizer tokenizer(&settings, this);
62+
std::istringstream istr(code);
63+
tokenizer.tokenize(istr, "test.cpp");
64+
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), startPattern);
65+
const Token * const tok2 = Token::findsimplematch(tokenizer.tokens(), endPattern);
66+
return ::isVariableChanged(tok1,tok2,1,false,&settings);
67+
}
68+
69+
void isVariableChanged() {
70+
// #8211 - no lhs for >> , do not crash
71+
ASSERT_EQUALS(true,
72+
isVariableChanged("void f() {\n"
73+
" int b;\n"
74+
" if (b) { (int)((INTOF(8))result >> b); }\n"
75+
"}", "if", "}"));
76+
}
5777
};
5878

5979
REGISTER_TEST(TestAstUtils)

0 commit comments

Comments
 (0)