2012-05-26 08:53:46 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2012-05-26 08:53:46 +02: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 checkleakautovarH
|
|
|
|
#define checkleakautovarH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "library.h"
|
2023-08-18 12:03:50 +02:00
|
|
|
#include "tokenize.h"
|
2012-05-26 08:53:46 +02:00
|
|
|
|
2014-05-23 14:09:34 +02:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <string>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <utility>
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
class ErrorLogger;
|
|
|
|
class Settings;
|
|
|
|
class Token;
|
2014-05-23 14:09:34 +02:00
|
|
|
|
2012-05-26 08:53:46 +02:00
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB VarInfo {
|
2012-05-26 08:53:46 +02:00
|
|
|
public:
|
2021-01-11 07:55:05 +01:00
|
|
|
enum AllocStatus { REALLOC = -3, OWNED = -2, DEALLOC = -1, NOALLOC = 0, ALLOC = 1 };
|
2015-01-28 13:45:40 +01:00
|
|
|
struct AllocInfo {
|
|
|
|
AllocStatus status;
|
2018-04-15 01:53:00 +02:00
|
|
|
/** 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.
|
|
|
|
*/
|
2015-01-28 13:45:40 +01:00
|
|
|
int type;
|
2021-01-11 07:55:05 +01:00
|
|
|
int reallocedFromType = -1;
|
2019-09-22 21:50:02 +02:00
|
|
|
const Token * allocTok;
|
2022-06-07 21:15:13 +02:00
|
|
|
explicit AllocInfo(int type_ = 0, AllocStatus status_ = NOALLOC, const Token* allocTok_ = nullptr) : status(status_), type(type_), allocTok(allocTok_) {}
|
2018-04-16 11:11:13 +02:00
|
|
|
|
2018-04-16 12:55:37 +02:00
|
|
|
bool managed() const {
|
2018-04-16 11:11:13 +02:00
|
|
|
return status < 0;
|
|
|
|
}
|
2015-01-28 13:45:40 +01:00
|
|
|
};
|
2023-04-06 18:44:03 +02:00
|
|
|
enum Usage { USED, NORET };
|
2019-07-16 08:54:21 +02:00
|
|
|
std::map<int, AllocInfo> alloctype;
|
2023-11-16 14:23:08 +01:00
|
|
|
std::map<int, std::pair<const Token*, Usage>> possibleUsage;
|
2019-07-16 08:54:21 +02:00
|
|
|
std::set<int> conditionalAlloc;
|
|
|
|
std::set<int> referenced;
|
2012-05-26 08:53:46 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void clear() {
|
2012-05-26 08:53:46 +02:00
|
|
|
alloctype.clear();
|
|
|
|
possibleUsage.clear();
|
|
|
|
conditionalAlloc.clear();
|
2012-10-27 16:36:14 +02:00
|
|
|
referenced.clear();
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 08:54:21 +02:00
|
|
|
void erase(nonneg int varid) {
|
2012-05-26 08:53:46 +02:00
|
|
|
alloctype.erase(varid);
|
|
|
|
possibleUsage.erase(varid);
|
|
|
|
conditionalAlloc.erase(varid);
|
2016-02-07 19:54:32 +01:00
|
|
|
referenced.erase(varid);
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void swap(VarInfo &other) {
|
2012-05-26 08:53:46 +02:00
|
|
|
alloctype.swap(other.alloctype);
|
|
|
|
possibleUsage.swap(other.possibleUsage);
|
|
|
|
conditionalAlloc.swap(other.conditionalAlloc);
|
2012-10-27 16:36:14 +02:00
|
|
|
referenced.swap(other.referenced);
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2021-01-11 07:55:05 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-26 08:53:46 +02:00
|
|
|
/** set possible usage for all variables */
|
2023-11-16 14:23:08 +01:00
|
|
|
void possibleUsageAll(const std::pair<const Token*, Usage>& functionUsage);
|
2012-05-26 08:53:46 +02:00
|
|
|
|
|
|
|
void print();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check for leaks
|
|
|
|
*/
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckLeakAutoVar : public Check {
|
2012-05-26 08:53:46 +02:00
|
|
|
public:
|
|
|
|
/** This constructor is used when registering the CheckLeakAutoVar */
|
2021-08-07 20:51:18 +02:00
|
|
|
CheckLeakAutoVar() : Check(myName()) {}
|
2012-05-26 08:53:46 +02:00
|
|
|
|
2023-09-11 11:12:42 +02:00
|
|
|
private:
|
2012-05-26 08:53:46 +02:00
|
|
|
/** 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) {}
|
2012-05-26 08:53:46 +02:00
|
|
|
|
2023-08-18 12:03:50 +02:00
|
|
|
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
|
|
|
|
CheckLeakAutoVar checkLeakAutoVar(&tokenizer, tokenizer.getSettings(), errorLogger);
|
2012-05-26 08:53:46 +02:00
|
|
|
checkLeakAutoVar.check();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** check for leaks in all scopes */
|
|
|
|
void check();
|
|
|
|
|
|
|
|
/** check for leaks in a function scope */
|
2022-11-19 16:49:34 +01:00
|
|
|
bool checkScope(const Token * const startToken,
|
2023-03-02 22:05:41 +01:00
|
|
|
VarInfo &varInfo,
|
2019-07-16 08:54:21 +02:00
|
|
|
std::set<int> notzero,
|
|
|
|
nonneg int recursiveCount);
|
2012-05-26 08:53:46 +02:00
|
|
|
|
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.
|
|
|
|
*/
|
2023-03-02 22:05:41 +01:00
|
|
|
const Token * checkTokenInsideExpression(const Token * const tok, VarInfo &varInfo);
|
2018-05-22 09:08:23 +02:00
|
|
|
|
2012-05-26 08:53:46 +02:00
|
|
|
/** parse function call */
|
2023-03-02 22:05:41 +01:00
|
|
|
void functionCall(const Token *tokName, const Token *tokOpeningPar, VarInfo &varInfo, const VarInfo::AllocInfo& allocation, const Library::AllocFunc* af);
|
2015-01-28 13:45:40 +01:00
|
|
|
|
|
|
|
/** parse changes in allocation status */
|
2023-03-02 22:05:41 +01:00
|
|
|
void changeAllocStatus(VarInfo &varInfo, const VarInfo::AllocInfo& allocation, const Token* tok, const Token* arg);
|
2012-05-26 08:53:46 +02:00
|
|
|
|
2019-07-05 12:44:52 +02:00
|
|
|
/** update allocation status if reallocation function */
|
2023-03-27 17:54:19 +02:00
|
|
|
void changeAllocStatusIfRealloc(std::map<int, VarInfo::AllocInfo> &alloctype, const Token *fTok, const Token *retTok) const;
|
2019-07-05 12:44:52 +02:00
|
|
|
|
2012-05-26 08:53:46 +02:00
|
|
|
/** return. either "return" or end of variable scope is seen */
|
2021-02-26 12:58:52 +01:00
|
|
|
void ret(const Token *tok, VarInfo &varInfo, const bool isEndOfScope = false);
|
2012-05-26 08:53:46 +02:00
|
|
|
|
|
|
|
/** if variable is allocated then there is a leak */
|
|
|
|
void leakIfAllocated(const Token *vartok, const VarInfo &varInfo);
|
|
|
|
|
2023-03-27 17:54:19 +02:00
|
|
|
void leakError(const Token* tok, const std::string &varname, int type) const;
|
|
|
|
void mismatchError(const Token* deallocTok, const Token* allocTok, const std::string &varname) const;
|
|
|
|
void deallocUseError(const Token *tok, const std::string &varname) const;
|
2019-09-22 21:50:02 +02:00
|
|
|
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);
|
2012-05-26 08:53:46 +02:00
|
|
|
|
|
|
|
/** message: user configuration is needed to complete analysis */
|
2023-11-16 14:23:08 +01:00
|
|
|
void configurationInfo(const Token* tok, const std::pair<const Token*, VarInfo::Usage>& functionUsage);
|
2012-05-26 08:53:46 +02:00
|
|
|
|
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
|
|
|
CheckLeakAutoVar c(nullptr, settings, errorLogger);
|
2019-09-22 21:50:02 +02:00
|
|
|
c.deallocReturnError(nullptr, nullptr, "p");
|
2023-11-16 14:23:08 +01:00
|
|
|
c.configurationInfo(nullptr, { nullptr, VarInfo::USED }); // user configuration is needed to complete analysis
|
2019-09-22 21:50:02 +02:00
|
|
|
c.doubleFreeError(nullptr, nullptr, "varname", 0);
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2012-06-30 17:44:05 +02:00
|
|
|
return "Leaks (auto variables)";
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string classInfo() const override {
|
2015-01-28 13:45:40 +01:00
|
|
|
return "Detect when a auto variable is allocated but not deallocated or deallocated twice.\n";
|
2012-05-26 08:53:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkleakautovarH
|