Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "valueflow.h"
#include "vfvalue.h"

#include <cmath>
#include <iomanip>
#include <list>
#include <map>
Expand Down Expand Up @@ -218,9 +219,14 @@ void CheckFunctionsImpl::invalidFunctionArgError(const Token *tok, const std::st
<< " or $symbol() argument nr " << argnr << " can have invalid value.";
else
errmsg << "Invalid $symbol() argument nr " << argnr << '.';
if (invalidValue)
errmsg << " The value is " << std::setprecision(10) << (invalidValue->isIntValue() ? invalidValue->intvalue : invalidValue->floatValue) << " but the valid values are '" << validstr << "'.";
else
if (invalidValue) {
errmsg << " The value is ";
if (!invalidValue->isIntValue() && std::isnan(invalidValue->floatValue))
errmsg << "nan";
else
errmsg << std::setprecision(10) << (invalidValue->isIntValue() ? invalidValue->intvalue : invalidValue->floatValue);
errmsg << " but the valid values are '" << validstr << "'.";
} else
errmsg << " The value is 0 or 1 (boolean) but the valid values are '" << validstr << "'.";
if (invalidValue) {
const auto severity = invalidValue->errorSeverity() && invalidValue->isKnown() && !invalidValue->conditional ? Severity::error : Severity::warning;
Expand Down
5 changes: 3 additions & 2 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
Expand Down Expand Up @@ -1151,10 +1152,10 @@ bool Library::isFloatArgValid(const Token *ftok, int argnr, double argvalue, con
return true;
if ((!tok->previous() || tok->strAt(-1) == ",") && Token::Match(tok,": %num%") && argvalue <= MathLib::toDoubleNumber(tok->tokAt(1)))
return true;
if (Token::Match(tok, "%num%") && MathLib::isFloat(tok->str()) && MathLib::isEqual(tok->str(), MathLib::toString(argvalue)))
if (Token::Match(tok, "%num%") && MathLib::isFloat(tok->str()) && std::isfinite(argvalue) && MathLib::isEqual(tok->str(), MathLib::toString(argvalue)))
return true;
if (Token::Match(tok, "! %num%") && MathLib::isFloat(tok->strAt(1)))
return MathLib::isNotEqual(tok->strAt(1), MathLib::toString(argvalue));
return !std::isfinite(argvalue) || MathLib::isNotEqual(tok->strAt(1), MathLib::toString(argvalue));
}
return false;
}
Expand Down
19 changes: 19 additions & 0 deletions test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TestFunctions : public TestFixture {
// Invalid function argument
TEST_CASE(invalidFunctionArg1);
TEST_CASE(invalidFunctionArgErrorPath);
TEST_CASE(invalidFunctionArgNonFinite);

// Math function usage
TEST_CASE(mathfunctionCall_fmod);
Expand Down Expand Up @@ -810,6 +811,24 @@ class TestFunctions : public TestFixture {
"[test.cpp:5:21]: note: Invalid argument\n", errout_str());
}

void invalidFunctionArgNonFinite() {
check("void f() {\n"
" double c = 0.9;\n"
" c = asin(c);\n"
" c = asin(c);\n"
" c = asin(c);\n"
"}\n"
"void g() {\n"
" double c = 1e300;\n"
" c = c * c;\n"
" c = asin(c);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:14]: (error) Invalid asin() argument nr 1. The value is 1.119769515 but the valid values are '-1.0:1.0'. [invalidFunctionArg]\n"
"[test.cpp:5:14]: (error) Invalid asin() argument nr 1. The value is nan but the valid values are '-1.0:1.0'. [invalidFunctionArg]\n"
"[test.cpp:10:14]: (error) Invalid asin() argument nr 1. The value is inf but the valid values are '-1.0:1.0'. [invalidFunctionArg]\n",
errout_str());
}

void mathfunctionCall_sqrt() {
// sqrt, sqrtf, sqrtl
check("void foo()\n"
Expand Down
Loading