2009-03-19 20:55:50 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2009-03-19 20:55:50 +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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-19 20:55:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#ifndef checkautovariablesH
|
|
|
|
#define checkautovariablesH
|
2009-03-19 20:55:50 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "errortypes.h"
|
2023-08-18 12:03:50 +02:00
|
|
|
#include "tokenize.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
#include <string>
|
2022-09-14 07:28:04 +02:00
|
|
|
#include <set>
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
class Settings;
|
|
|
|
class Token;
|
2020-05-23 07:16:49 +02:00
|
|
|
class ErrorLogger;
|
|
|
|
class Variable;
|
2009-03-19 20:55:50 +01:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
namespace ValueFlow {
|
|
|
|
class Value;
|
|
|
|
}
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @addtogroup Checks
|
2014-01-01 20:46:00 +01:00
|
|
|
/** @brief Various small checks for automatic variables */
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckAutoVariables : public Check {
|
2009-03-19 20:55:50 +01:00
|
|
|
public:
|
2009-03-21 07:53:23 +01:00
|
|
|
/** This constructor is used when registering the CheckClass */
|
2021-08-07 20:51:18 +02:00
|
|
|
CheckAutoVariables() : Check(myName()) {}
|
2009-03-21 07:53:23 +01:00
|
|
|
|
2023-09-11 11:12:42 +02:00
|
|
|
private:
|
2010-03-17 22:16:18 +01:00
|
|
|
/** This constructor is used when running checks. */
|
2009-03-21 07:53:23 +01:00
|
|
|
CheckAutoVariables(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2021-08-07 20:51:18 +02:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {}
|
2009-03-21 07:53:23 +01:00
|
|
|
|
2011-02-12 15:39:26 +01:00
|
|
|
/** @brief Run checks against the normal token list */
|
2023-08-18 12:03:50 +02:00
|
|
|
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
|
|
|
|
CheckAutoVariables checkAutoVariables(&tokenizer, tokenizer.getSettings(), errorLogger);
|
2013-10-06 16:07:27 +02:00
|
|
|
checkAutoVariables.assignFunctionArg();
|
2018-11-10 16:40:40 +01:00
|
|
|
checkAutoVariables.checkVarLifetime();
|
2019-03-09 16:53:43 +01:00
|
|
|
checkAutoVariables.autoVariables();
|
2011-02-12 15:39:26 +01:00
|
|
|
}
|
|
|
|
|
2013-10-06 16:07:27 +02:00
|
|
|
/** assign function argument */
|
|
|
|
void assignFunctionArg();
|
|
|
|
|
2009-05-22 07:51:30 +02:00
|
|
|
/** Check auto variables */
|
2009-03-19 20:55:50 +01:00
|
|
|
void autoVariables();
|
2009-03-21 07:53:23 +01:00
|
|
|
|
2020-09-28 22:48:57 +02:00
|
|
|
/**
|
|
|
|
* Check variable assignment.. value must be changed later or there will be a error reported
|
|
|
|
* @return true if error is reported */
|
|
|
|
bool checkAutoVariableAssignment(const Token *expr, bool inconclusive, const Token *startToken = nullptr);
|
|
|
|
|
2018-11-10 16:40:40 +01:00
|
|
|
void checkVarLifetime();
|
|
|
|
|
|
|
|
void checkVarLifetimeScope(const Token * start, const Token * end);
|
|
|
|
|
2011-08-09 18:24:39 +02:00
|
|
|
void errorAutoVariableAssignment(const Token *tok, bool inconclusive);
|
2018-11-10 16:40:40 +01:00
|
|
|
void errorReturnDanglingLifetime(const Token *tok, const ValueFlow::Value* val);
|
2018-11-11 16:43:54 +01:00
|
|
|
void errorInvalidLifetime(const Token *tok, const ValueFlow::Value* val);
|
2018-11-21 08:43:57 +01:00
|
|
|
void errorDanglngLifetime(const Token *tok, const ValueFlow::Value *val);
|
2021-11-01 19:23:15 +01:00
|
|
|
void errorDanglingTemporaryLifetime(const Token* tok, const ValueFlow::Value* val, const Token* tempTok);
|
2019-09-11 19:25:09 +02:00
|
|
|
void errorReturnReference(const Token* tok, ErrorPath errorPath, bool inconclusive);
|
2019-01-23 07:29:16 +01:00
|
|
|
void errorDanglingReference(const Token *tok, const Variable *var, ErrorPath errorPath);
|
2020-09-09 01:30:45 +02:00
|
|
|
void errorDanglingTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive);
|
2019-10-08 09:28:39 +02:00
|
|
|
void errorReturnTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive);
|
2018-11-03 18:55:12 +01:00
|
|
|
void errorInvalidDeallocation(const Token *tok, const ValueFlow::Value *val);
|
2014-08-04 11:45:24 +02:00
|
|
|
void errorUselessAssignmentArg(const Token *tok);
|
2013-02-01 19:16:17 +01:00
|
|
|
void errorUselessAssignmentPtrArg(const Token *tok);
|
2009-06-09 19:45:58 +02:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
|
2019-01-23 07:29:16 +01:00
|
|
|
ErrorPath errorPath;
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckAutoVariables c(nullptr,settings,errorLogger);
|
|
|
|
c.errorAutoVariableAssignment(nullptr, false);
|
2019-09-11 19:25:09 +02:00
|
|
|
c.errorReturnReference(nullptr, errorPath, false);
|
2019-01-23 07:29:16 +01:00
|
|
|
c.errorDanglingReference(nullptr, nullptr, errorPath);
|
2019-10-08 09:28:39 +02:00
|
|
|
c.errorReturnTempReference(nullptr, errorPath, false);
|
2020-09-09 01:30:45 +02:00
|
|
|
c.errorDanglingTempReference(nullptr, errorPath, false);
|
2018-11-03 18:55:12 +01:00
|
|
|
c.errorInvalidDeallocation(nullptr, nullptr);
|
2016-05-07 16:30:54 +02:00
|
|
|
c.errorUselessAssignmentArg(nullptr);
|
|
|
|
c.errorUselessAssignmentPtrArg(nullptr);
|
2018-11-12 10:08:17 +01:00
|
|
|
c.errorReturnDanglingLifetime(nullptr, nullptr);
|
|
|
|
c.errorInvalidLifetime(nullptr, nullptr);
|
2018-11-21 08:43:57 +01:00
|
|
|
c.errorDanglngLifetime(nullptr, nullptr);
|
2021-11-01 19:23:15 +01:00
|
|
|
c.errorDanglingTemporaryLifetime(nullptr, nullptr, nullptr);
|
2009-03-22 08:20:15 +01:00
|
|
|
}
|
2009-06-12 12:19:37 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2009-06-12 15:20:08 +02:00
|
|
|
return "Auto Variables";
|
|
|
|
}
|
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string classInfo() const override {
|
2009-06-21 21:03:21 +02:00
|
|
|
return "A pointer to a variable is only valid as long as the variable is in scope.\n"
|
2009-06-12 12:19:37 +02:00
|
|
|
"Check:\n"
|
2014-09-30 14:56:12 +02:00
|
|
|
"- returning a pointer to auto or temporary variable\n"
|
|
|
|
"- assigning address of an variable to an effective parameter of a function\n"
|
|
|
|
"- returning reference to local/temporary variable\n"
|
|
|
|
"- returning address of function parameter\n"
|
|
|
|
"- suspicious assignment of pointer argument\n"
|
|
|
|
"- useless assignment of function argument\n";
|
2009-06-12 12:19:37 +02:00
|
|
|
}
|
2022-09-14 07:28:04 +02:00
|
|
|
|
|
|
|
/** returns true if tokvalue has already been diagnosed */
|
|
|
|
bool diag(const Token* tokvalue);
|
|
|
|
|
|
|
|
std::set<const Token*> mDiagDanglingTemp;
|
2009-03-19 20:55:50 +01:00
|
|
|
};
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
2009-03-19 20:55:50 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkautovariablesH
|