From 7ff7bc1c2ef57e47fb0a79d9c1965a31da1d6987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 19 Jan 2014 20:16:55 +0100 Subject: [PATCH] Fixed #5376 (false positive: zerodivcond (style) Either the condition 'B>0' is useless or there is division by zero) --- lib/valueflow.cpp | 10 +++++++++- test/testvalueflow.cpp | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 36faf837f..2185d7ba0 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index bbc1129d0..2ab8db046 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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"