2009-03-19 20:55:50 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2018-03-31 20:59:09 +02:00
|
|
|
* Copyright (C) 2007-2018 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"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
class ErrorLogger;
|
|
|
|
class Settings;
|
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
2009-03-19 20:55:50 +01:00
|
|
|
|
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 */
|
2014-11-20 14:20:09 +01:00
|
|
|
CheckAutoVariables() : Check(myName()) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2009-03-21 07:53:23 +01:00
|
|
|
|
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)
|
2014-11-20 14:20:09 +01:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2009-03-21 07:53:23 +01:00
|
|
|
|
2011-02-12 15:39:26 +01:00
|
|
|
/** @brief Run checks against the normal token list */
|
2018-05-15 16:37:40 +02:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) override {
|
2011-02-12 15:39:26 +01:00
|
|
|
CheckAutoVariables checkAutoVariables(tokenizer, settings, errorLogger);
|
2013-10-06 16:07:27 +02:00
|
|
|
checkAutoVariables.assignFunctionArg();
|
2011-02-12 15:39:26 +01:00
|
|
|
checkAutoVariables.returnReference();
|
2018-11-10 16:40:40 +01:00
|
|
|
checkAutoVariables.checkVarLifetime();
|
2011-02-12 15:39:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 16:37:40 +02:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) override {
|
2009-03-21 07:53:23 +01:00
|
|
|
CheckAutoVariables checkAutoVariables(tokenizer, settings, errorLogger);
|
|
|
|
checkAutoVariables.autoVariables();
|
2009-03-19 20:55:50 +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
|
|
|
|
2010-01-23 20:39:12 +01:00
|
|
|
/** Returning reference to local/temporary variable */
|
|
|
|
void returnReference();
|
|
|
|
|
2018-11-10 16:40:40 +01:00
|
|
|
void checkVarLifetime();
|
|
|
|
|
|
|
|
void checkVarLifetimeScope(const Token * start, const Token * end);
|
|
|
|
|
2009-03-19 20:55:50 +01:00
|
|
|
private:
|
2010-01-27 19:16:32 +01:00
|
|
|
/**
|
|
|
|
* Returning a temporary object?
|
|
|
|
* @param tok pointing at the "return" token
|
|
|
|
* @return true if a temporary object is returned
|
|
|
|
*/
|
2015-11-14 18:43:07 +01:00
|
|
|
static bool returnTemporary(const Token *tok);
|
2009-06-09 19:45:58 +02:00
|
|
|
|
2011-07-21 14:50:38 +02:00
|
|
|
void errorReturnAddressToAutoVariable(const Token *tok);
|
2018-01-27 14:48:45 +01:00
|
|
|
void errorReturnAddressToAutoVariable(const Token *tok, const ValueFlow::Value *value);
|
2018-01-24 21:33:58 +01:00
|
|
|
void errorAssignAddressOfLocalArrayToGlobalPointer(const Token *pointer, const Token *array);
|
2018-01-25 22:50:41 +01:00
|
|
|
void errorAssignAddressOfLocalVariableToGlobalPointer(const Token *pointer, const Token *variable);
|
2009-06-09 19:45:58 +02:00
|
|
|
void errorReturnPointerToLocalArray(const Token *tok);
|
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);
|
2010-01-23 20:39:12 +01:00
|
|
|
void errorReturnReference(const Token *tok);
|
2010-01-27 19:16:32 +01:00
|
|
|
void errorReturnTempReference(const Token *tok);
|
2018-11-03 18:55:12 +01:00
|
|
|
void errorInvalidDeallocation(const Token *tok, const ValueFlow::Value *val);
|
2011-09-01 03:36:31 +02:00
|
|
|
void errorReturnAddressOfFunctionParameter(const Token *tok, const std::string &varname);
|
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
|
|
|
|
2018-05-15 16:37:40 +02:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckAutoVariables c(nullptr,settings,errorLogger);
|
|
|
|
c.errorAutoVariableAssignment(nullptr, false);
|
|
|
|
c.errorReturnAddressToAutoVariable(nullptr);
|
2018-01-24 21:33:58 +01:00
|
|
|
c.errorAssignAddressOfLocalArrayToGlobalPointer(nullptr, nullptr);
|
2016-05-07 16:30:54 +02:00
|
|
|
c.errorReturnPointerToLocalArray(nullptr);
|
|
|
|
c.errorReturnReference(nullptr);
|
|
|
|
c.errorReturnTempReference(nullptr);
|
2018-11-03 18:55:12 +01:00
|
|
|
c.errorInvalidDeallocation(nullptr, nullptr);
|
2016-05-07 16:30:54 +02:00
|
|
|
c.errorReturnAddressOfFunctionParameter(nullptr, "parameter");
|
|
|
|
c.errorUselessAssignmentArg(nullptr);
|
|
|
|
c.errorUselessAssignmentPtrArg(nullptr);
|
2018-11-12 10:08:17 +01:00
|
|
|
c.errorReturnDanglingLifetime(nullptr, nullptr);
|
|
|
|
c.errorInvalidLifetime(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";
|
|
|
|
}
|
|
|
|
|
2018-05-15 16:37:40 +02: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
|
|
|
}
|
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
|