2010-10-31 12:31:11 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 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
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
2018-12-25 21:11:23 +01:00
|
|
|
#include "ctu.h"
|
2020-04-13 13:44:48 +02:00
|
|
|
#include "mathlib.h"
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "errortypes.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "valueflow.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2012-05-25 13:40:18 +02:00
|
|
|
class Scope;
|
2017-05-27 04:33:47 +02:00
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
2012-05-25 13:40:18 +02:00
|
|
|
class Variable;
|
2020-05-23 07:16:49 +02:00
|
|
|
class ErrorLogger;
|
2021-12-15 19:47:27 +01:00
|
|
|
class Settings;
|
|
|
|
class Library;
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
namespace tinyxml2 {
|
|
|
|
class XMLElement;
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:08:55 +02:00
|
|
|
|
|
|
|
struct VariableValue {
|
|
|
|
explicit VariableValue(MathLib::bigint val = 0) : value(val), notEqual(false) {}
|
|
|
|
MathLib::bigint value;
|
|
|
|
bool notEqual;
|
|
|
|
};
|
|
|
|
|
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 */
|
2021-08-07 20:51:18 +02:00
|
|
|
CheckUninitVar() : Check(myName()) {}
|
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)
|
2021-08-07 20:51:18 +02:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {}
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2019-02-11 07:43:16 +01:00
|
|
|
/** @brief Run checks against the normal token list */
|
2022-02-10 23:02:24 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) override {
|
2010-10-31 12:31:11 +01:00
|
|
|
CheckUninitVar checkUninitVar(tokenizer, settings, errorLogger);
|
2011-12-13 21:57:27 +01:00
|
|
|
checkUninitVar.check();
|
2017-04-23 18:05:14 +02:00
|
|
|
checkUninitVar.valueFlowUninit();
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 21:57:27 +01:00
|
|
|
/** Check for uninitialized variables */
|
|
|
|
void check();
|
2016-12-21 23:11:11 +01:00
|
|
|
void checkScope(const Scope* scope, const std::set<std::string> &arrayTypeDefs);
|
2015-01-22 13:51:43 +01:00
|
|
|
void checkStruct(const Token *tok, const Variable &structvar);
|
2015-07-23 14:51:38 +02:00
|
|
|
enum Alloc { NO_ALLOC, NO_CTOR_CALL, CTOR_CALL, ARRAY };
|
2021-01-27 19:49:13 +01:00
|
|
|
bool checkScopeForVariable(const Token *tok, const Variable& var, bool* const possibleInit, bool* const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue> variableValue);
|
2021-05-21 14:30:47 +02:00
|
|
|
const Token *checkExpr(const Token *tok, const Variable& var, const Alloc alloc, bool known, bool *bailout=nullptr);
|
2015-01-23 19:38:39 +01:00
|
|
|
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, Alloc alloc, const std::string &membervar);
|
|
|
|
bool checkLoopBody(const Token *tok, const Variable& var, const Alloc alloc, const std::string &membervar, const bool suppressErrors);
|
2021-05-11 20:35:15 +02:00
|
|
|
const Token* checkLoopBodyRecursive(const Token *start, const Variable& var, const Alloc alloc, const std::string &membervar, bool &bailout) const;
|
2019-07-14 12:22:33 +02:00
|
|
|
void checkRhs(const Token *tok, const Variable &var, Alloc alloc, nonneg int number_of_if, const std::string &membervar);
|
2021-07-01 22:08:00 +02:00
|
|
|
static const Token *isVariableUsage(bool cpp, const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect = 0);
|
2021-05-16 22:27:04 +02:00
|
|
|
const Token *isVariableUsage(const Token *vartok, bool pointer, Alloc alloc, int indirect = 0) const;
|
2021-07-01 22:08:00 +02:00
|
|
|
static int isFunctionParUsage(const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect = 0);
|
2019-11-13 12:46:54 +01:00
|
|
|
int isFunctionParUsage(const Token *vartok, bool pointer, Alloc alloc, int indirect = 0) const;
|
2015-06-19 18:21:46 +02:00
|
|
|
bool isMemberVariableAssignment(const Token *tok, const std::string &membervar) const;
|
2015-01-23 19:38:39 +01:00
|
|
|
bool isMemberVariableUsage(const Token *tok, bool isPointer, Alloc alloc, const std::string &membervar) const;
|
2011-12-26 18:32:42 +01:00
|
|
|
|
2017-04-23 18:05:14 +02:00
|
|
|
/** ValueFlow-based checking for uninitialized variables */
|
|
|
|
void valueFlowUninit();
|
|
|
|
|
2018-12-18 07:56:33 +01:00
|
|
|
/* data for multifile checking */
|
|
|
|
class MyFileInfo : public Check::FileInfo {
|
|
|
|
public:
|
|
|
|
/** function arguments that data are unconditionally read */
|
2018-12-25 21:11:23 +01:00
|
|
|
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;
|
2018-12-18 07:56:33 +01:00
|
|
|
|
|
|
|
/** Convert MyFileInfo data into xml string */
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string toString() const override;
|
2018-12-18 07:56:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Parse current TU and extract file info */
|
2022-02-10 23:02:24 +01:00
|
|
|
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const override;
|
2018-12-18 07:56:33 +01:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
Check::FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const override;
|
2018-12-18 07:56:33 +01:00
|
|
|
|
|
|
|
/** @brief Analyse all file infos for all TU */
|
2022-02-10 23:02:24 +01:00
|
|
|
bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
|
2018-12-18 07:56:33 +01:00
|
|
|
|
2021-10-30 07:43:37 +02:00
|
|
|
void uninitvarError(const Token* tok, const ValueFlow::Value& v);
|
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);
|
2019-08-15 10:44:55 +02:00
|
|
|
void uninitvarError(const Token *tok, const std::string &varname, ErrorPath errorPath);
|
2019-08-15 10:46:16 +02:00
|
|
|
void uninitvarError(const Token *tok, const std::string &varname) {
|
2019-08-15 10:44:55 +02:00
|
|
|
ErrorPath errorPath;
|
|
|
|
uninitvarError(tok, varname, errorPath);
|
|
|
|
}
|
2015-07-23 08:46:59 +02:00
|
|
|
void uninitvarError(const Token *tok, const std::string &varname, Alloc alloc) {
|
2015-07-23 14:51:38 +02:00
|
|
|
if (alloc == NO_CTOR_CALL || alloc == CTOR_CALL)
|
2015-07-23 08:46:59 +02:00
|
|
|
uninitdataError(tok, varname);
|
2015-07-23 14:51:38 +02:00
|
|
|
else
|
|
|
|
uninitvarError(tok, varname);
|
2015-07-23 08:46:59 +02:00
|
|
|
}
|
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
|
|
|
|
2012-03-27 19:40:39 +02:00
|
|
|
private:
|
2018-12-18 07:56:33 +01:00
|
|
|
Check::FileInfo *getFileInfo() const;
|
|
|
|
bool isUnsafeFunction(const Scope *scope, int argnr, const Token **tok) const;
|
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckUninitVar c(nullptr, settings, errorLogger);
|
2010-12-29 12:43:29 +01:00
|
|
|
|
2021-10-30 07:43:37 +02:00
|
|
|
ValueFlow::Value v{};
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
// error
|
2021-10-30 07:43:37 +02:00
|
|
|
c.uninitvarError(nullptr, v);
|
2016-05-07 16:30:54 +02:00
|
|
|
c.uninitstringError(nullptr, "varname", true);
|
|
|
|
c.uninitdataError(nullptr, "varname");
|
|
|
|
c.uninitStructMemberError(nullptr, "a.b");
|
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";
|
|
|
|
}
|
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string classInfo() const override {
|
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"
|
2019-09-03 17:15:58 +02:00
|
|
|
"- using allocated data before it has been initialized\n";
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkuninitvarH
|