2010-10-31 12:31:11 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2015-01-03 12:14:58 +01:00
|
|
|
* Copyright (C) 2007-2015 Daniel Marjamäki and Cppcheck team.
|
2010-10-31 12:31:11 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef checkuninitvarH
|
|
|
|
#define checkuninitvarH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2010-10-31 12:31:11 +01:00
|
|
|
#include "check.h"
|
|
|
|
|
2012-05-25 13:40:18 +02:00
|
|
|
class Scope;
|
|
|
|
class Variable;
|
2010-10-31 12:31:11 +01:00
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief Checking for uninitialized variables */
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckUninitVar : public Check {
|
2010-10-31 12:31:11 +01:00
|
|
|
public:
|
|
|
|
/** @brief This constructor is used when registering the CheckUninitVar */
|
2014-11-20 14:20:09 +01:00
|
|
|
CheckUninitVar() : Check(myName()), testrunner(false) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2010-10-31 12:31:11 +01:00
|
|
|
|
|
|
|
/** @brief This constructor is used when running checks. */
|
|
|
|
CheckUninitVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2014-11-20 14:20:09 +01:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger), testrunner(false) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2010-10-31 12:31:11 +01:00
|
|
|
|
|
|
|
/** @brief Run checks against the simplified token list */
|
2014-11-20 14:20:09 +01:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2010-10-31 12:31:11 +01:00
|
|
|
CheckUninitVar checkUninitVar(tokenizer, settings, errorLogger);
|
|
|
|
checkUninitVar.executionPaths();
|
2011-12-13 21:57:27 +01:00
|
|
|
checkUninitVar.check();
|
2014-08-05 06:24:23 +02:00
|
|
|
checkUninitVar.deadPointer();
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 21:57:27 +01:00
|
|
|
/** Check for uninitialized variables */
|
|
|
|
void check();
|
2012-05-25 13:40:18 +02:00
|
|
|
void checkScope(const Scope* scope);
|
2013-12-09 15:58:42 +01:00
|
|
|
void checkStruct(const Scope* scope, const Token *tok, const Variable &structvar);
|
2013-12-04 20:32:20 +01:00
|
|
|
bool checkScopeForVariable(const Scope* scope, const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, bool * const alloc, const std::string &membervar);
|
|
|
|
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, bool alloc, const std::string &membervar);
|
|
|
|
bool checkLoopBody(const Token *tok, const Variable& var, const bool alloc, const std::string &membervar, const bool suppressErrors);
|
|
|
|
void checkRhs(const Token *tok, const Variable &var, bool alloc, const std::string &membervar);
|
2015-01-21 13:52:03 +01:00
|
|
|
bool isVariableUsage(const Token *vartok, bool ispointer, bool alloc) const;
|
2013-07-30 12:52:27 +02:00
|
|
|
static bool isMemberVariableAssignment(const Token *tok, const std::string &membervar);
|
2013-12-04 20:32:20 +01:00
|
|
|
bool isMemberVariableUsage(const Token *tok, bool isPointer, bool alloc, const std::string &membervar) const;
|
2011-12-26 18:32:42 +01:00
|
|
|
|
2014-08-05 06:24:23 +02:00
|
|
|
/** ValueFlow-based checking for dead pointer usage */
|
|
|
|
void deadPointer();
|
|
|
|
void deadPointerError(const Token *pointer, const Token *alias);
|
|
|
|
|
2014-11-15 10:43:49 +01:00
|
|
|
/* data for multifile checking */
|
|
|
|
class MyFileInfo : public Check::FileInfo {
|
|
|
|
public:
|
|
|
|
/* functions that must have initialized data */
|
|
|
|
std::set<std::string> uvarFunctions;
|
|
|
|
|
|
|
|
/* functions calls with uninitialized data */
|
|
|
|
std::set<std::string> functionCalls;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Parse current TU and extract file info */
|
2014-12-02 06:41:18 +01:00
|
|
|
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const;
|
2014-11-15 10:43:49 +01:00
|
|
|
|
|
|
|
/** @brief Analyse all file infos for all TU */
|
2014-12-02 06:41:18 +01:00
|
|
|
void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);
|
2014-11-15 10:43:49 +01:00
|
|
|
|
|
|
|
void analyseFunctions(const Tokenizer *tokenizer, std::set<std::string> &f) const;
|
2010-10-31 12:31:11 +01:00
|
|
|
|
|
|
|
/** @brief new type of check: check execution paths */
|
|
|
|
void executionPaths();
|
|
|
|
|
2011-09-05 19:42:48 +02:00
|
|
|
void uninitstringError(const Token *tok, const std::string &varname, bool strncpy_);
|
2010-10-31 12:31:11 +01:00
|
|
|
void uninitdataError(const Token *tok, const std::string &varname);
|
|
|
|
void uninitvarError(const Token *tok, const std::string &varname);
|
2013-01-17 21:04:22 +01:00
|
|
|
void uninitStructMemberError(const Token *tok, const std::string &membername);
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2013-07-28 12:41:38 +02:00
|
|
|
/** testrunner: (don't abort() when assertion fails, just write error message) */
|
|
|
|
bool testrunner;
|
|
|
|
|
2012-03-27 19:40:39 +02:00
|
|
|
private:
|
2014-11-20 14:20:09 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
2010-12-29 12:43:29 +01:00
|
|
|
CheckUninitVar c(0, settings, errorLogger);
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
// error
|
2011-09-05 19:42:48 +02:00
|
|
|
c.uninitstringError(0, "varname", true);
|
2010-12-29 12:43:29 +01:00
|
|
|
c.uninitdataError(0, "varname");
|
|
|
|
c.uninitvarError(0, "varname");
|
2013-01-17 21:04:22 +01:00
|
|
|
c.uninitStructMemberError(0, "a.b");
|
2014-08-05 06:24:23 +02:00
|
|
|
c.deadPointerError(0,0);
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2010-10-31 12:31:11 +01:00
|
|
|
return "Uninitialized variables";
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
std::string classInfo() const {
|
2010-10-31 12:31:11 +01:00
|
|
|
return "Uninitialized variables\n"
|
2014-09-30 14:56:12 +02:00
|
|
|
"- using uninitialized local variables\n"
|
|
|
|
"- using allocated data before it has been initialized\n"
|
|
|
|
"- using dead pointer\n";
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkuninitvarH
|