Fixed #5376 (false positive: zerodivcond (style) Either the condition 'B>0' is useless or there is division by zero)

This commit is contained in:
Daniel Marjamäki 2014-01-19 20:16:55 +01:00
parent 84c5f47eb1
commit 7ff7bc1c2e
2 changed files with 27 additions and 1 deletions

View File

@ -294,7 +294,15 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
val2.varId = varid;
}
}
if (Token::Match(tok,"<|>")) {
if (num!=0)
continue;
bool isunsigned = false;
for (const Token* type = var->typeStartToken(); type && type->varId() == 0U; type = type->next())
isunsigned |= type->isUnsigned();
if (!isunsigned)
continue;
}
for (Token *tok2 = tok->previous(); ; tok2 = tok2->previous()) {
if (!tok2) {
if (settings->debugwarnings) {

View File

@ -157,6 +157,24 @@ private:
ASSERT_EQUALS(true, testValueOfX(code, 2U, 1));
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
code = "void f(unsigned int x) {\n"
" int a = x;\n"
" if (x > 0) {}\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
code = "void f(unsigned int x) {\n"
" int a = x;\n"
" if (x > 1) {}\n" // not zero => don't consider > condition
"}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 1));
code = "void f(int x) {\n" // not unsigned => don't consider > condition
" int a = x;\n"
" if (x > 0) {}\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 2U, 0));
code = "void f(int *x) {\n"
" *x = 100;\n"
" if (x) {}\n"