Skip to content

Commit d354cdc

Browse files
committed
Fixed cppcheck-opensource#6168 (False positive: sign conversion for inner calculation)
1 parent 865a252 commit d354cdc

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

lib/checktype.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ void CheckType::checkSignConversion()
238238

239239
// Check if there are signed operands that can be negative..
240240
std::stack<const Token *> tokens;
241-
tokens.push(tok);
241+
tokens.push(tok->astOperand1());
242+
tokens.push(tok->astOperand2());
242243
while (!tokens.empty()) {
243244
const Token *tok1 = tokens.top();
244245
tokens.pop();
@@ -268,8 +269,6 @@ void CheckType::checkSignConversion()
268269
break;
269270
}
270271
}
271-
tokens.push(tok1->astOperand1());
272-
tokens.push(tok1->astOperand2());
273272
}
274273
}
275274
}

test/testtype.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ class TestType : public TestFixture {
117117
"void f2() { f1(-4); }");
118118
ASSERT_EQUALS("[test.cpp:1]: (warning) Suspicious code: sign conversion of x in calculation, even though x can have a negative value\n", errout.str());
119119

120+
check("unsigned int f1(int x) {" // #6168: FP for inner calculation
121+
" return 5U * (1234 - x);\n" // <- signed subtraction, x is not sign converted
122+
"}\n"
123+
"void f2() { f1(-4); }");
124+
ASSERT_EQUALS("", errout.str());
125+
120126
// Dont warn for + and -
121127
check("void f1(int x) {"
122128
" a = x + 5U;\n"

0 commit comments

Comments
 (0)