| 53 | static const CWE CWE688(688U); // Function Call With Incorrect Variable or Reference as Argument |
| 54 | |
| 55 | void CheckFunctionsImpl::checkProhibitedFunctions() |
| 56 | { |
| 57 | const bool checkAlloca = mSettings.severity.isEnabled(Severity::warning) && ((mTokenizer->isC() && mSettings.standards.c >= Standards::C99) || mSettings.standards.cpp >= Standards::CPP11); |
| 58 | |
| 59 | logChecker("CheckFunctions::checkProhibitedFunctions"); |
| 60 | |
| 61 | const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); |
| 62 | for (const Scope *scope : symbolDatabase->functionScopes) { |
| 63 | for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { |
| 64 | if (!Token::Match(tok, "%name% (") && tok->varId() == 0) |
| 65 | continue; |
| 66 | // alloca() is special as it depends on the code being C or C++, so it is not in Library |
| 67 | if (checkAlloca && Token::simpleMatch(tok, "alloca (") && (!tok->function() || tok->function()->nestedIn->type == ScopeType::eGlobal)) { |
| 68 | if (tok->isC()) { |
| 69 | if (mSettings.standards.c > Standards::C89) |
| 70 | reportError(tok, Severity::warning, "allocaCalled", |
| 71 | "$symbol:alloca\n" |
| 72 | "Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n" |
| 73 | "The obsolete function 'alloca' is called. In C99 and later it is recommended to use a variable length array or " |
| 74 | "a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons " |
| 75 | "(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca)."); |
| 76 | } else |
| 77 | reportError(tok, Severity::warning, "allocaCalled", |
| 78 | "$symbol:alloca\n" |
| 79 | "Obsolete function 'alloca' called.\n" |
| 80 | "The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or " |
| 81 | "a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons " |
| 82 | "(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca)."); |
| 83 | } else { |
| 84 | if (tok->function() && tok->function()->hasBody()) |
| 85 | continue; |
| 86 | |
| 87 | const Library::WarnInfo* wi = mSettings.library.getWarnInfo(tok); |
| 88 | if (wi) { |
| 89 | if (mSettings.severity.isEnabled(wi->severity) && ((tok->isC() && mSettings.standards.c >= wi->standards.c) || (tok->isCpp() && mSettings.standards.cpp >= wi->standards.cpp))) { |
| 90 | const std::string daca = mSettings.daca ? "prohibited" : ""; |
| 91 | const std::string prefix = daca + tok->str(); |
| 92 | functionCalledError(tok, wi->severity, prefix, wi->message); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void CheckFunctionsImpl::functionCalledError(const Token* tok, Severity severity, const std::string& prefix, const std::string& msg) |
| 101 | { |
no test coverage detected