cppcheck/lib/checkleakautovar.h

183 lines
6.4 KiB
C
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2022-02-05 11:45:17 +01:00
* Copyright (C) 2007-2022 Cppcheck team.
*
* 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 checkleakautovarH
#define checkleakautovarH
//---------------------------------------------------------------------------
#include "check.h"
2017-05-27 04:33:47 +02:00
#include "config.h"
#include "library.h"
2020-05-23 07:16:49 +02:00
#include "utils.h"
#include <map>
#include <set>
2017-05-27 04:33:47 +02:00
#include <string>
#include <utility>
2017-05-27 04:33:47 +02:00
class ErrorLogger;
class Settings;
class Token;
class Tokenizer;
class CPPCHECKLIB VarInfo {
public:
enum AllocStatus { REALLOC = -3, OWNED = -2, DEALLOC = -1, NOALLOC = 0, ALLOC = 1 };
struct AllocInfo {
AllocStatus status;
/** Allocation type. If it is a positive value then it corresponds to
* a Library allocation id. A negative value is a builtin
* checkleakautovar allocation type.
*/
int type;
int reallocedFromType = -1;
const Token * allocTok;
AllocInfo(int type_ = 0, AllocStatus status_ = NOALLOC, const Token* allocTok_ = nullptr) : status(status_), type(type_), allocTok(allocTok_) {}
2018-04-16 12:55:37 +02:00
bool managed() const {
return status < 0;
}
};
std::map<int, AllocInfo> alloctype;
std::map<int, std::string> possibleUsage;
std::set<int> conditionalAlloc;
std::set<int> referenced;
2014-11-20 14:20:09 +01:00
void clear() {
alloctype.clear();
possibleUsage.clear();
conditionalAlloc.clear();
referenced.clear();
}
void erase(nonneg int varid) {
alloctype.erase(varid);
possibleUsage.erase(varid);
conditionalAlloc.erase(varid);
referenced.erase(varid);
}
2014-11-20 14:20:09 +01:00
void swap(VarInfo &other) {
alloctype.swap(other.alloctype);
possibleUsage.swap(other.possibleUsage);
conditionalAlloc.swap(other.conditionalAlloc);
referenced.swap(other.referenced);
}
void reallocToAlloc(nonneg int varid) {
const AllocInfo& alloc = alloctype[varid];
if (alloc.reallocedFromType >= 0) {
const std::map<int, VarInfo::AllocInfo>::iterator it = alloctype.find(alloc.reallocedFromType);
if (it != alloctype.end() && it->second.status == REALLOC) {
it->second.status = ALLOC;
}
}
}
/** set possible usage for all variables */
void possibleUsageAll(const std::string &functionName);
void print();
};
/// @addtogroup Checks
/// @{
/**
* @brief Check for leaks
*/
class CPPCHECKLIB CheckLeakAutoVar : public Check {
public:
/** This constructor is used when registering the CheckLeakAutoVar */
2021-08-07 20:51:18 +02:00
CheckLeakAutoVar() : Check(myName()) {}
/** This constructor is used when running checks. */
CheckLeakAutoVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
2021-08-07 20:51:18 +02:00
: Check(myName(), tokenizer, settings, errorLogger) {}
2019-03-16 09:17:50 +01:00
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
CheckLeakAutoVar checkLeakAutoVar(tokenizer, settings, errorLogger);
checkLeakAutoVar.check();
}
private:
/** check for leaks in all scopes */
void check();
/** check for leaks in a function scope */
void checkScope(const Token * const startToken,
VarInfo *varInfo,
std::set<int> notzero,
nonneg int recursiveCount);
2018-05-25 08:35:37 +02:00
/** Check token inside expression.
2021-08-07 20:51:18 +02:00
* @param tok token inside expression.
* @param varInfo Variable info
* @return next token to process (if no other checks needed for this token). NULL if other checks could be performed.
*/
const Token * checkTokenInsideExpression(const Token * const tok, VarInfo *varInfo);
/** parse function call */
void functionCall(const Token *tokName, const Token *tokOpeningPar, VarInfo *varInfo, const VarInfo::AllocInfo& allocation, const Library::AllocFunc* af);
/** parse changes in allocation status */
void changeAllocStatus(VarInfo *varInfo, const VarInfo::AllocInfo& allocation, const Token* tok, const Token* arg);
/** update allocation status if reallocation function */
void changeAllocStatusIfRealloc(std::map<int, VarInfo::AllocInfo> &alloctype, const Token *fTok, const Token *retTok);
/** return. either "return" or end of variable scope is seen */
void ret(const Token *tok, VarInfo &varInfo, const bool isEndOfScope = false);
/** if variable is allocated then there is a leak */
void leakIfAllocated(const Token *vartok, const VarInfo &varInfo);
2013-07-02 07:18:19 +02:00
void leakError(const Token* tok, const std::string &varname, int type);
void mismatchError(const Token* deallocTok, const Token* allocTok, const std::string &varname);
void deallocUseError(const Token *tok, const std::string &varname);
void deallocReturnError(const Token *tok, const Token *deallocTok, const std::string &varname);
void doubleFreeError(const Token *tok, const Token *prevFreeTok, const std::string &varname, int type);
/** message: user configuration is needed to complete analysis */
void configurationInfo(const Token* tok, const std::string &functionName);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const OVERRIDE {
CheckLeakAutoVar c(nullptr, settings, errorLogger);
c.deallocReturnError(nullptr, nullptr, "p");
c.configurationInfo(nullptr, "f"); // user configuration is needed to complete analysis
c.doubleFreeError(nullptr, nullptr, "varname", 0);
}
2014-11-20 14:20:09 +01:00
static std::string myName() {
2012-06-30 17:44:05 +02:00
return "Leaks (auto variables)";
}
std::string classInfo() const OVERRIDE {
return "Detect when a auto variable is allocated but not deallocated or deallocated twice.\n";
}
};
/// @}
//---------------------------------------------------------------------------
#endif // checkleakautovarH