Blame lib/checkfunctions.cpp

Packit 2035a7
/*
Packit 2035a7
 * Cppcheck - A tool for static C/C++ code analysis
Packit 2035a7
 * Copyright (C) 2007-2017 Cppcheck team.
Packit 2035a7
 *
Packit 2035a7
 * This program is free software: you can redistribute it and/or modify
Packit 2035a7
 * it under the terms of the GNU General Public License as published by
Packit 2035a7
 * the Free Software Foundation, either version 3 of the License, or
Packit 2035a7
 * (at your option) any later version.
Packit 2035a7
 *
Packit 2035a7
 * This program is distributed in the hope that it will be useful,
Packit 2035a7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2035a7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 2035a7
 * GNU General Public License for more details.
Packit 2035a7
 *
Packit 2035a7
 * You should have received a copy of the GNU General Public License
Packit 2035a7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 2035a7
 */
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
// Check functions
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
Packit 2035a7
#include "checkfunctions.h"
Packit 2035a7
Packit 2035a7
#include "astutils.h"
Packit 2035a7
#include "mathlib.h"
Packit 2035a7
#include "standards.h"
Packit 2035a7
#include "symboldatabase.h"
Packit 2035a7
#include "token.h"
Packit 2035a7
#include "tokenize.h"
Packit 2035a7
#include "valueflow.h"
Packit 2035a7
Packit 2035a7
#include <cmath>
Packit 2035a7
#include <cstddef>
Packit 2035a7
#include <ostream>
Packit 2035a7
#include <vector>
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
Packit 2035a7
Packit 2035a7
// Register this check class (by creating a static instance of it)
Packit 2035a7
namespace {
Packit 2035a7
    CheckFunctions instance;
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
static const CWE CWE252(252U);  // Unchecked Return Value
Packit 2035a7
static const CWE CWE477(477U);  // Use of Obsolete Functions
Packit 2035a7
static const CWE CWE758(758U);  // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
Packit 2035a7
static const CWE CWE628(628U);  // Function Call with Incorrectly Specified Arguments
Packit 2035a7
static const CWE CWE686(686U);  // Function Call With Incorrect Argument Type
Packit 2035a7
static const CWE CWE687(687U);  // Function Call With Incorrectly Specified Argument Value
Packit 2035a7
static const CWE CWE688(688U);  // Function Call With Incorrect Variable or Reference as Argument
Packit 2035a7
Packit 2035a7
void CheckFunctions::checkProhibitedFunctions()
Packit 2035a7
{
Packit 2035a7
    const bool checkAlloca = _settings->isEnabled(Settings::WARNING) && ((_settings->standards.c >= Standards::C99 && _tokenizer->isC()) || _settings->standards.cpp >= Standards::CPP11);
Packit 2035a7
Packit 2035a7
    const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
Packit 2035a7
    for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
Packit 2035a7
        const Scope* scope = symbolDatabase->functionScopes[i];
Packit 2035a7
        for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
Packit 2035a7
            if (!Token::Match(tok, "%name% (") && tok->varId() == 0)
Packit 2035a7
                continue;
Packit 2035a7
            // alloca() is special as it depends on the code being C or C++, so it is not in Library
Packit 2035a7
            if (checkAlloca && Token::simpleMatch(tok, "alloca (") && (!tok->function() || tok->function()->nestedIn->type == Scope::eGlobal)) {
Packit 2035a7
                if (_tokenizer->isC()) {
Packit 2035a7
                    if (_settings->standards.c > Standards::C89)
Packit 2035a7
                        reportError(tok, Severity::warning, "allocaCalled",
Packit 2035a7
                                    "Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n"
Packit 2035a7
                                    "The obsolete function 'alloca' is called. In C99 and later it is recommended to use a variable length array or "
Packit 2035a7
                                    "a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons "
Packit 2035a7
                                    "(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).");
Packit 2035a7
                } else
Packit 2035a7
                    reportError(tok, Severity::warning, "allocaCalled",
Packit 2035a7
                                "Obsolete function 'alloca' called.\n"
Packit 2035a7
                                "The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or "
Packit 2035a7
                                "a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons "
Packit 2035a7
                                "(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).");
Packit 2035a7
            } else {
Packit 2035a7
                if (tok->function() && tok->function()->hasBody())
Packit 2035a7
                    continue;
Packit 2035a7
Packit 2035a7
                const Library::WarnInfo* wi = _settings->library.getWarnInfo(tok);
Packit 2035a7
                if (wi) {
Packit 2035a7
                    if (_settings->isEnabled(wi->severity) && _settings->standards.c >= wi->standards.c && _settings->standards.cpp >= wi->standards.cpp) {
Packit 2035a7
                        reportError(tok, wi->severity, tok->str() + "Called", wi->message, CWE477, false);
Packit 2035a7
                    }
Packit 2035a7
                }
Packit 2035a7
            }
Packit 2035a7
        }
Packit 2035a7
    }
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
// Check <valid> and <not-bool>
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
void CheckFunctions::invalidFunctionUsage()
Packit 2035a7
{
Packit 2035a7
    const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase();
Packit 2035a7
    const std::size_t functions = symbolDatabase->functionScopes.size();
Packit 2035a7
    for (std::size_t i = 0; i < functions; ++i) {
Packit 2035a7
        const Scope * scope = symbolDatabase->functionScopes[i];
Packit 2035a7
        for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
Packit 2035a7
            if (!Token::Match(tok, "%name% ( !!)"))
Packit 2035a7
                continue;
Packit 2035a7
            const Token * const functionToken = tok;
Packit 2035a7
            const std::vector<const Token *> arguments = getArguments(tok);
Packit 2035a7
            for (unsigned int argnr = 1; argnr <= arguments.size(); ++argnr) {
Packit 2035a7
                const Token * const argtok = arguments[argnr-1];
Packit 2035a7
Packit 2035a7
                // check <valid>...</valid>
Packit 2035a7
                const ValueFlow::Value *invalidValue = argtok->getInvalidValue(functionToken,argnr,_settings);
Packit 2035a7
                if (invalidValue) {
Packit 2035a7
                    invalidFunctionArgError(argtok, functionToken->next()->astOperand1()->expressionString(), argnr, invalidValue, _settings->library.validarg(functionToken, argnr));
Packit 2035a7
                }
Packit 2035a7
Packit 2035a7
                if (astIsBool(argtok)) {
Packit 2035a7
                    // check <not-bool>
Packit 2035a7
                    if (_settings->library.isboolargbad(functionToken, argnr))
Packit 2035a7
                        invalidFunctionArgBoolError(argtok, functionToken->str(), argnr);
Packit 2035a7
Packit 2035a7
                    // Are the values 0 and 1 valid?
Packit 2035a7
                    else if (!_settings->library.isargvalid(functionToken, argnr, 0))
Packit 2035a7
                        invalidFunctionArgError(argtok, functionToken->str(), argnr, nullptr, _settings->library.validarg(functionToken, argnr));
Packit 2035a7
                    else if (!_settings->library.isargvalid(functionToken, argnr, 1))
Packit 2035a7
                        invalidFunctionArgError(argtok, functionToken->str(), argnr, nullptr, _settings->library.validarg(functionToken, argnr));
Packit 2035a7
                }
Packit 2035a7
            }
Packit 2035a7
        }
Packit 2035a7
    }
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::invalidFunctionArgError(const Token *tok, const std::string &functionName, int argnr, const ValueFlow::Value *invalidValue, const std::string &validstr)
Packit 2035a7
{
Packit 2035a7
    std::ostringstream errmsg;
Packit 2035a7
    if (invalidValue && invalidValue->condition)
Packit 2035a7
        errmsg << ValueFlow::eitherTheConditionIsRedundant(invalidValue->condition)
Packit 2035a7
               << " or " << functionName << "() argument nr " << argnr
Packit 2035a7
               << " can have invalid value.";
Packit 2035a7
    else
Packit 2035a7
        errmsg << "Invalid " << functionName << "() argument nr " << argnr << '.';
Packit 2035a7
    if (invalidValue)
Packit 2035a7
        errmsg << " The value is " << invalidValue->intvalue << " but the valid values are '" << validstr << "'.";
Packit 2035a7
    else
Packit 2035a7
        errmsg << " The value is 0 or 1 (boolean) but the valid values are '" << validstr << "'.";
Packit 2035a7
    if (invalidValue)
Packit 2035a7
        reportError(getErrorPath(tok, invalidValue, "Invalid argument"),
Packit 2035a7
                    invalidValue->errorSeverity() ? Severity::error : Severity::warning,
Packit 2035a7
                    "invalidFunctionArg",
Packit 2035a7
                    errmsg.str(),
Packit 2035a7
                    CWE628,
Packit 2035a7
                    invalidValue->isInconclusive());
Packit 2035a7
    else
Packit 2035a7
        reportError(tok,
Packit 2035a7
                    Severity::error,
Packit 2035a7
                    "invalidFunctionArg",
Packit 2035a7
                    errmsg.str(),
Packit 2035a7
                    CWE628,
Packit 2035a7
                    false);
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::invalidFunctionArgBoolError(const Token *tok, const std::string &functionName, int argnr)
Packit 2035a7
{
Packit 2035a7
    std::ostringstream errmsg;
Packit 2035a7
    errmsg << "Invalid " << functionName << "() argument nr " << argnr << ". A non-boolean value is required.";
Packit 2035a7
    reportError(tok, Severity::error, "invalidFunctionArgBool", errmsg.str(), CWE628, false);
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
// Check for ignored return values.
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
void CheckFunctions::checkIgnoredReturnValue()
Packit 2035a7
{
Packit 2035a7
    if (!_settings->isEnabled(Settings::WARNING))
Packit 2035a7
        return;
Packit 2035a7
Packit 2035a7
    const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
Packit 2035a7
    const std::size_t functions = symbolDatabase->functionScopes.size();
Packit 2035a7
    for (std::size_t i = 0; i < functions; ++i) {
Packit 2035a7
        const Scope * scope = symbolDatabase->functionScopes[i];
Packit 2035a7
        for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
Packit 2035a7
            // skip c++11 initialization, ({...})
Packit 2035a7
            if (Token::Match(tok, "%var%|(|, {"))
Packit 2035a7
                tok = tok->linkAt(1);
Packit 2035a7
            else if (Token::Match(tok, "[(<]") && tok->link())
Packit 2035a7
                tok = tok->link();
Packit 2035a7
Packit 2035a7
            if (tok->varId() || !Token::Match(tok, "%name% ("))
Packit 2035a7
                continue;
Packit 2035a7
Packit 2035a7
            if (tok->next()->astParent())
Packit 2035a7
                continue;
Packit 2035a7
Packit 2035a7
            if (!tok->scope()->isExecutable()) {
Packit 2035a7
                tok = tok->scope()->classEnd;
Packit 2035a7
                continue;
Packit 2035a7
            }
Packit 2035a7
Packit 2035a7
            if ((!tok->function() || !Token::Match(tok->function()->retDef, "void %name%")) && _settings->library.isUseRetVal(tok) && !WRONG_DATA(!tok->next()->astOperand1(), tok))
Packit 2035a7
                ignoredReturnValueError(tok, tok->next()->astOperand1()->expressionString());
Packit 2035a7
        }
Packit 2035a7
    }
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::ignoredReturnValueError(const Token* tok, const std::string& function)
Packit 2035a7
{
Packit 2035a7
    reportError(tok, Severity::warning, "ignoredReturnValue",
Packit 2035a7
                "Return value of function " + function + "() is not used.", CWE252, false);
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
// Detect passing wrong values to <cmath> functions like atan(0, x);
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
void CheckFunctions::checkMathFunctions()
Packit 2035a7
{
Packit 2035a7
    const bool styleC99 = _settings->isEnabled(Settings::STYLE) && _settings->standards.c != Standards::C89 && _settings->standards.cpp != Standards::CPP03;
Packit 2035a7
    const bool printWarnings = _settings->isEnabled(Settings::WARNING);
Packit 2035a7
Packit 2035a7
    const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
Packit 2035a7
    const std::size_t functions = symbolDatabase->functionScopes.size();
Packit 2035a7
    for (std::size_t i = 0; i < functions; ++i) {
Packit 2035a7
        const Scope * scope = symbolDatabase->functionScopes[i];
Packit 2035a7
        for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
Packit 2035a7
            if (tok->varId())
Packit 2035a7
                continue;
Packit 2035a7
            if (printWarnings && Token::Match(tok, "%name% ( !!)")) {
Packit 2035a7
                if (tok->strAt(-1) != "."
Packit 2035a7
                    && Token::Match(tok, "log|logf|logl|log10|log10f|log10l ( %num% )")) {
Packit 2035a7
                    const std::string& number = tok->strAt(2);
Packit 2035a7
                    bool isNegative = MathLib::isNegative(number);
Packit 2035a7
                    bool isInt = MathLib::isInt(number);
Packit 2035a7
                    bool isFloat = MathLib::isFloat(number);
Packit 2035a7
                    if (isNegative && isInt && MathLib::toLongNumber(number) <= 0) {
Packit 2035a7
                        mathfunctionCallWarning(tok); // case log(-2)
Packit 2035a7
                    } else if (isNegative && isFloat && MathLib::toDoubleNumber(number) <= 0.) {
Packit 2035a7
                        mathfunctionCallWarning(tok); // case log(-2.0)
Packit 2035a7
                    } else if (!isNegative && isFloat && MathLib::toDoubleNumber(number) <= 0.) {
Packit 2035a7
                        mathfunctionCallWarning(tok); // case log(0.0)
Packit 2035a7
                    } else if (!isNegative && isInt && MathLib::toLongNumber(number) <= 0) {
Packit 2035a7
                        mathfunctionCallWarning(tok); // case log(0)
Packit 2035a7
                    }
Packit 2035a7
                }
Packit 2035a7
Packit 2035a7
                // acos( x ), asin( x )  where x is defined for interval [-1,+1], but not beyond
Packit 2035a7
                else if (Token::Match(tok, "acos|acosl|acosf|asin|asinf|asinl ( %num% )")) {
Packit 2035a7
                    if (std::fabs(MathLib::toDoubleNumber(tok->strAt(2))) > 1.0)
Packit 2035a7
                        mathfunctionCallWarning(tok);
Packit 2035a7
                }
Packit 2035a7
                // sqrt( x ): if x is negative the result is undefined
Packit 2035a7
                else if (Token::Match(tok, "sqrt|sqrtf|sqrtl ( %num% )")) {
Packit 2035a7
                    if (MathLib::isNegative(tok->strAt(2)))
Packit 2035a7
                        mathfunctionCallWarning(tok);
Packit 2035a7
                }
Packit 2035a7
                // atan2 ( x , y): x and y can not be zero, because this is mathematically not defined
Packit 2035a7
                else if (Token::Match(tok, "atan2|atan2f|atan2l ( %num% , %num% )")) {
Packit 2035a7
                    if (MathLib::isNullValue(tok->strAt(2)) && MathLib::isNullValue(tok->strAt(4)))
Packit 2035a7
                        mathfunctionCallWarning(tok, 2);
Packit 2035a7
                }
Packit 2035a7
                // fmod ( x , y) If y is zero, then either a range error will occur or the function will return zero (implementation-defined).
Packit 2035a7
                else if (Token::Match(tok, "fmod|fmodf|fmodl (")) {
Packit 2035a7
                    const Token* nextArg = tok->tokAt(2)->nextArgument();
Packit 2035a7
                    if (nextArg && nextArg->isNumber() && MathLib::isNullValue(nextArg->str()))
Packit 2035a7
                        mathfunctionCallWarning(tok, 2);
Packit 2035a7
                }
Packit 2035a7
                // pow ( x , y) If x is zero, and y is negative --> division by zero
Packit 2035a7
                else if (Token::Match(tok, "pow|powf|powl ( %num% , %num% )")) {
Packit 2035a7
                    if (MathLib::isNullValue(tok->strAt(2)) && MathLib::isNegative(tok->strAt(4)))
Packit 2035a7
                        mathfunctionCallWarning(tok, 2);
Packit 2035a7
                }
Packit 2035a7
            }
Packit 2035a7
Packit 2035a7
            if (styleC99) {
Packit 2035a7
                if (Token::Match(tok, "%num% - erf (") && Tokenizer::isOneNumber(tok->str()) && tok->next()->astOperand2() == tok->tokAt(3)) {
Packit 2035a7
                    mathfunctionCallWarning(tok, "1 - erf(x)", "erfc(x)");
Packit 2035a7
                } else if (Token::simpleMatch(tok, "exp (") && Token::Match(tok->linkAt(1), ") - %num%") && Tokenizer::isOneNumber(tok->linkAt(1)->strAt(2)) && tok->linkAt(1)->next()->astOperand1() == tok->next()) {
Packit 2035a7
                    mathfunctionCallWarning(tok, "exp(x) - 1", "expm1(x)");
Packit 2035a7
                } else if (Token::simpleMatch(tok, "log (") && tok->next()->astOperand2()) {
Packit 2035a7
                    const Token* plus = tok->next()->astOperand2();
Packit 2035a7
                    if (plus->str() == "+" && ((plus->astOperand1() && Tokenizer::isOneNumber(plus->astOperand1()->str())) || (plus->astOperand2() && Tokenizer::isOneNumber(plus->astOperand2()->str()))))
Packit 2035a7
                        mathfunctionCallWarning(tok, "log(1 + x)", "log1p(x)");
Packit 2035a7
                }
Packit 2035a7
            }
Packit 2035a7
        }
Packit 2035a7
    }
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::mathfunctionCallWarning(const Token *tok, const unsigned int numParam)
Packit 2035a7
{
Packit 2035a7
    if (tok) {
Packit 2035a7
        if (numParam == 1)
Packit 2035a7
            reportError(tok, Severity::warning, "wrongmathcall", "Passing value " + tok->strAt(2) + " to " + tok->str() + "() leads to implementation-defined result.", CWE758, false);
Packit 2035a7
        else if (numParam == 2)
Packit 2035a7
            reportError(tok, Severity::warning, "wrongmathcall", "Passing values " + tok->strAt(2) + " and " + tok->strAt(4) + " to " + tok->str() + "() leads to implementation-defined result.", CWE758, false);
Packit 2035a7
    } else
Packit 2035a7
        reportError(tok, Severity::warning, "wrongmathcall", "Passing value '#' to #() leads to implementation-defined result.", CWE758, false);
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::mathfunctionCallWarning(const Token *tok, const std::string& oldexp, const std::string& newexp)
Packit 2035a7
{
Packit 2035a7
    reportError(tok, Severity::style, "unpreciseMathCall", "Expression '" + oldexp + "' can be replaced by '" + newexp + "' to avoid loss of precision.", CWE758, false);
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
// memset(p, y, 0 /* bytes to fill */) <- 2nd and 3rd arguments inverted
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
void CheckFunctions::memsetZeroBytes()
Packit 2035a7
{
Packit 2035a7
// FIXME:
Packit 2035a7
//  Replace this with library configuration.
Packit 2035a7
//  For instance:
Packit 2035a7
//     <arg nr="3">
Packit 2035a7
//       <warn knownIntValue="0" severity="warning" msg="..."/>
Packit 2035a7
//     </arg>
Packit 2035a7
Packit 2035a7
    if (!_settings->isEnabled(Settings::WARNING))
Packit 2035a7
        return;
Packit 2035a7
Packit 2035a7
    const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
Packit 2035a7
    const std::size_t functions = symbolDatabase->functionScopes.size();
Packit 2035a7
    for (std::size_t i = 0; i < functions; ++i) {
Packit 2035a7
        const Scope * scope = symbolDatabase->functionScopes[i];
Packit 2035a7
        for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
Packit 2035a7
            if (Token::Match(tok, "memset|wmemset (") && (numberOfArguments(tok)==3)) {
Packit 2035a7
                const std::vector<const Token *> &arguments = getArguments(tok);
Packit 2035a7
                if (WRONG_DATA(arguments.size() != 3U, tok))
Packit 2035a7
                    continue;
Packit 2035a7
                const Token* lastParamTok = arguments[2];
Packit 2035a7
                if (lastParamTok->str() == "0")
Packit 2035a7
                    memsetZeroBytesError(tok);
Packit 2035a7
            }
Packit 2035a7
        }
Packit 2035a7
    }
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::memsetZeroBytesError(const Token *tok)
Packit 2035a7
{
Packit 2035a7
    const std::string summary("memset() called to fill 0 bytes.");
Packit 2035a7
    const std::string verbose(summary + " The second and third arguments might be inverted."
Packit 2035a7
                              " The function memset ( void * ptr, int value, size_t num ) sets the"
Packit 2035a7
                              " first num bytes of the block of memory pointed by ptr to the specified value.");
Packit 2035a7
    reportError(tok, Severity::warning, "memsetZeroBytes", summary + "\n" + verbose, CWE687, false);
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::memsetInvalid2ndParam()
Packit 2035a7
{
Packit 2035a7
// FIXME:
Packit 2035a7
//  Replace this with library configuration.
Packit 2035a7
//  For instance:
Packit 2035a7
//     <arg nr="2">
Packit 2035a7
//       <not-float/>
Packit 2035a7
//       <warn possibleIntValue=":-129,256:" severity="warning" msg="..."/>
Packit 2035a7
//     </arg>
Packit 2035a7
Packit 2035a7
    const bool printPortability = _settings->isEnabled(Settings::PORTABILITY);
Packit 2035a7
    const bool printWarning = _settings->isEnabled(Settings::WARNING);
Packit 2035a7
    if (!printWarning && !printPortability)
Packit 2035a7
        return;
Packit 2035a7
Packit 2035a7
    const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
Packit 2035a7
    const std::size_t functions = symbolDatabase->functionScopes.size();
Packit 2035a7
    for (std::size_t i = 0; i < functions; ++i) {
Packit 2035a7
        const Scope * scope = symbolDatabase->functionScopes[i];
Packit 2035a7
        for (const Token* tok = scope->classStart->next(); tok && (tok != scope->classEnd); tok = tok->next()) {
Packit 2035a7
            if (!Token::simpleMatch(tok, "memset ("))
Packit 2035a7
                continue;
Packit 2035a7
Packit 2035a7
            const std::vector<const Token *> args = getArguments(tok);
Packit 2035a7
            if (args.size() != 3)
Packit 2035a7
                continue;
Packit 2035a7
Packit 2035a7
            // Second parameter is zero literal, i.e. 0.0f
Packit 2035a7
            const Token * const secondParamTok = args[1];
Packit 2035a7
            if (Token::Match(secondParamTok, "%num% ,") && MathLib::isNullValue(secondParamTok->str()))
Packit 2035a7
                continue;
Packit 2035a7
Packit 2035a7
            // Check if second parameter is a float variable or a float literal != 0.0f
Packit 2035a7
            if (printPortability && astIsFloat(secondParamTok,false)) {
Packit 2035a7
                memsetFloatError(secondParamTok, secondParamTok->expressionString());
Packit 2035a7
            }
Packit 2035a7
Packit 2035a7
            if (printWarning && secondParamTok->isNumber()) { // Check if the second parameter is a literal and is out of range
Packit 2035a7
                const long long int value = MathLib::toLongNumber(secondParamTok->str());
Packit 2035a7
                const long long sCharMin = _settings->signedCharMin();
Packit 2035a7
                const long long uCharMax = _settings->unsignedCharMax();
Packit 2035a7
                if (value < sCharMin || value > uCharMax)
Packit 2035a7
                    memsetValueOutOfRangeError(secondParamTok, secondParamTok->str());
Packit 2035a7
            }
Packit 2035a7
        }
Packit 2035a7
    }
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::memsetFloatError(const Token *tok, const std::string &var_value)
Packit 2035a7
{
Packit 2035a7
    const std::string message("The 2nd memset() argument '" + var_value +
Packit 2035a7
                              "' is a float, its representation is implementation defined.");
Packit 2035a7
    const std::string verbose(message + " memset() is used to set each byte of a block of memory to a specific value and"
Packit 2035a7
                              " the actual representation of a floating-point value is implementation defined.");
Packit 2035a7
    reportError(tok, Severity::portability, "memsetFloat", message + "\n" + verbose, CWE688, false);
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
void CheckFunctions::memsetValueOutOfRangeError(const Token *tok, const std::string &value)
Packit 2035a7
{
Packit 2035a7
    const std::string message("The 2nd memset() argument '" + value + "' doesn't fit into an 'unsigned char'.");
Packit 2035a7
    const std::string verbose(message + " The 2nd parameter is passed as an 'int', but the function fills the block of memory using the 'unsigned char' conversion of this value.");
Packit 2035a7
    reportError(tok, Severity::warning, "memsetValueOutOfRange", message + "\n" + verbose, CWE686, false);
Packit 2035a7
}
Packit 2035a7
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
// --check-library => warn for unconfigured functions
Packit 2035a7
//---------------------------------------------------------------------------
Packit 2035a7
Packit 2035a7
void CheckFunctions::checkLibraryMatchFunctions()
Packit 2035a7
{
Packit 2035a7
    if (!_settings->checkLibrary || !_settings->isEnabled(Settings::INFORMATION))
Packit 2035a7
        return;
Packit 2035a7
Packit 2035a7
    bool New = false;
Packit 2035a7
    for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
Packit 2035a7
        if (!tok->scope() || !tok->scope()->isExecutable())
Packit 2035a7
            continue;
Packit 2035a7
Packit 2035a7
        if (tok->str() == "new")
Packit 2035a7
            New = true;
Packit 2035a7
        else if (tok->str() == ";")
Packit 2035a7
            New = false;
Packit 2035a7
        else if (New)
Packit 2035a7
            continue;
Packit 2035a7
Packit 2035a7
        if (!Token::Match(tok, "%name% (") || Token::Match(tok, "asm|sizeof|catch"))
Packit 2035a7
            continue;
Packit 2035a7
Packit 2035a7
        if (tok->varId() != 0 || tok->type() || tok->isStandardType() || tok->isControlFlowKeyword())
Packit 2035a7
            continue;
Packit 2035a7
Packit 2035a7
        if (tok->linkAt(1)->strAt(1) == "(")
Packit 2035a7
            continue;
Packit 2035a7
Packit 2035a7
        if (!_settings->library.isNotLibraryFunction(tok))
Packit 2035a7
            continue;
Packit 2035a7
Packit 2035a7
        const std::string &functionName = _settings->library.getFunctionName(tok);
Packit 2035a7
        if (functionName.empty() || _settings->library.functions.find(functionName) != _settings->library.functions.end())
Packit 2035a7
            continue;
Packit 2035a7
Packit 2035a7
        reportError(tok,
Packit 2035a7
                    Severity::information,
Packit 2035a7
                    "checkLibraryFunction",
Packit 2035a7
                    "--check-library: There is no matching configuration for function " + functionName + "()");
Packit 2035a7
    }
Packit 2035a7
}