Fixed #3472 (false positive: (error) Passing value 0 to log() leads to undefined result)

This commit is contained in:
Daniel Marjamäki 2012-01-08 12:07:25 +01:00
parent ee55d3294a
commit 65ce55e675
2 changed files with 57 additions and 46 deletions

View File

@ -2160,53 +2160,61 @@ void CheckOther::zerodivError(const Token *tok)
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckOther::checkMathFunctions() void CheckOther::checkMathFunctions()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase *db = _tokenizer->getSymbolDatabase();
if (tok->varId() == 0 && Token::Match(tok, "log|log10 ( %num% )")) {
bool isNegative = MathLib::isNegative(tok->strAt(2));
bool isInt = MathLib::isInt(tok->strAt(2));
bool isFloat = MathLib::isFloat(tok->strAt(2));
if (isNegative && isInt && MathLib::toLongNumber(tok->strAt(2)) <= 0) {
mathfunctionCallError(tok); // case log(-2)
} else if (isNegative && isFloat && MathLib::toDoubleNumber(tok->strAt(2)) <= 0.) {
mathfunctionCallError(tok); // case log(-2.0)
} else if (!isNegative && isFloat && MathLib::toDoubleNumber(tok->strAt(2)) <= 0.) {
mathfunctionCallError(tok); // case log(0.0)
} else if (!isNegative && isInt && MathLib::toLongNumber(tok->strAt(2)) <= 0) {
mathfunctionCallError(tok); // case log(0)
}
}
// acos( x ), asin( x ) where x is defined for intervall [-1,+1], but not beyound std::list<Scope>::const_iterator scope;
else if (tok->varId() == 0 && for (scope = db->scopeList.begin(); scope != db->scopeList.end(); ++scope) {
Token::Match(tok, "acos|asin ( %num% )") && if (scope->type != Scope::eFunction)
std::fabs(MathLib::toDoubleNumber(tok->strAt(2))) > 1.0) { continue;
mathfunctionCallError(tok);
} for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
// sqrt( x ): if x is negative the result is undefined if (tok->varId() == 0 && Token::Match(tok, "log|log10 ( %num% )")) {
else if (tok->varId() == 0 && bool isNegative = MathLib::isNegative(tok->strAt(2));
Token::Match(tok, "sqrt|sqrtf|sqrtl ( %num% )") && bool isInt = MathLib::isInt(tok->strAt(2));
MathLib::isNegative(tok->strAt(2))) { bool isFloat = MathLib::isFloat(tok->strAt(2));
mathfunctionCallError(tok); if (isNegative && isInt && MathLib::toLongNumber(tok->strAt(2)) <= 0) {
} mathfunctionCallError(tok); // case log(-2)
// atan2 ( x , y): x and y can not be zero, because this is mathematically not defined } else if (isNegative && isFloat && MathLib::toDoubleNumber(tok->strAt(2)) <= 0.) {
else if (tok->varId() == 0 && mathfunctionCallError(tok); // case log(-2.0)
Token::Match(tok, "atan2 ( %num% , %num% )") && } else if (!isNegative && isFloat && MathLib::toDoubleNumber(tok->strAt(2)) <= 0.) {
MathLib::isNullValue(tok->strAt(2)) && mathfunctionCallError(tok); // case log(0.0)
MathLib::isNullValue(tok->strAt(4))) { } else if (!isNegative && isInt && MathLib::toLongNumber(tok->strAt(2)) <= 0) {
mathfunctionCallError(tok, 2); mathfunctionCallError(tok); // case log(0)
} }
// fmod ( x , y) If y is zero, then either a range error will occur or the function will return zero (implementation-defined). }
else if (tok->varId() == 0 &&
Token::Match(tok, "fmod ( %num% , %num% )") && // acos( x ), asin( x ) where x is defined for intervall [-1,+1], but not beyound
MathLib::isNullValue(tok->strAt(4))) { else if (tok->varId() == 0 &&
mathfunctionCallError(tok, 2); Token::Match(tok, "acos|asin ( %num% )") &&
} std::fabs(MathLib::toDoubleNumber(tok->strAt(2))) > 1.0) {
// pow ( x , y) If x is zero, and y is negative --> division by zero mathfunctionCallError(tok);
else if (tok->varId() == 0 && }
Token::Match(tok, "pow ( %num% , %num% )") && // sqrt( x ): if x is negative the result is undefined
MathLib::isNullValue(tok->strAt(2)) && else if (tok->varId() == 0 &&
MathLib::isNegative(tok->strAt(4))) { Token::Match(tok, "sqrt|sqrtf|sqrtl ( %num% )") &&
mathfunctionCallError(tok, 2); MathLib::isNegative(tok->strAt(2))) {
mathfunctionCallError(tok);
}
// atan2 ( x , y): x and y can not be zero, because this is mathematically not defined
else if (tok->varId() == 0 &&
Token::Match(tok, "atan2 ( %num% , %num% )") &&
MathLib::isNullValue(tok->strAt(2)) &&
MathLib::isNullValue(tok->strAt(4))) {
mathfunctionCallError(tok, 2);
}
// fmod ( x , y) If y is zero, then either a range error will occur or the function will return zero (implementation-defined).
else if (tok->varId() == 0 &&
Token::Match(tok, "fmod ( %num% , %num% )") &&
MathLib::isNullValue(tok->strAt(4))) {
mathfunctionCallError(tok, 2);
}
// pow ( x , y) If x is zero, and y is negative --> division by zero
else if (tok->varId() == 0 &&
Token::Match(tok, "pow ( %num% , %num% )") &&
MathLib::isNullValue(tok->strAt(2)) &&
MathLib::isNegative(tok->strAt(4))) {
mathfunctionCallError(tok, 2);
}
} }
} }
} }

View File

@ -929,6 +929,9 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #3473 - no warning if "log" is a variable
check("Fred::Fred() : log(0) { }");
ASSERT_EQUALS("", errout.str());
// acos // acos
check("void foo()\n" check("void foo()\n"