2013-04-10 18:49:38 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2018-03-31 20:59:09 +02:00
|
|
|
* Copyright (C) 2007-2018 Cppcheck team.
|
2013-04-10 18:49:38 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#ifndef checksizeofH
|
|
|
|
#define checksizeofH
|
2013-04-10 18:49:38 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string>
|
2013-04-10 18:49:38 +02:00
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
class ErrorLogger;
|
|
|
|
class Settings;
|
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
2013-04-10 18:49:38 +02:00
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief checks on usage of sizeof() operator */
|
|
|
|
|
|
|
|
class CPPCHECKLIB CheckSizeof : public Check {
|
|
|
|
public:
|
|
|
|
/** @brief This constructor is used when registering the CheckClass */
|
2014-11-20 14:20:09 +01:00
|
|
|
CheckSizeof() : Check(myName()) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2013-04-10 18:49:38 +02:00
|
|
|
|
|
|
|
/** @brief This constructor is used when running checks. */
|
|
|
|
CheckSizeof(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
|
|
|
}
|
2013-04-10 18:49:38 +02:00
|
|
|
|
|
|
|
/** @brief Run checks against the normal token list */
|
2019-01-12 07:37:42 +01:00
|
|
|
void runChecks(const Tokenizer* tokenizer, const Settings* settings, ErrorLogger* errorLogger) OVERRIDE {
|
2013-04-10 18:49:38 +02:00
|
|
|
CheckSizeof checkSizeof(tokenizer, settings, errorLogger);
|
|
|
|
|
|
|
|
// Checks
|
|
|
|
checkSizeof.sizeofsizeof();
|
|
|
|
checkSizeof.sizeofCalculation();
|
2018-03-15 10:24:17 +01:00
|
|
|
checkSizeof.sizeofFunction();
|
2013-04-10 18:49:38 +02:00
|
|
|
checkSizeof.suspiciousSizeofCalculation();
|
|
|
|
checkSizeof.checkSizeofForArrayParameter();
|
|
|
|
checkSizeof.checkSizeofForPointerSize();
|
|
|
|
checkSizeof.checkSizeofForNumericParameter();
|
2013-07-05 21:07:07 +02:00
|
|
|
checkSizeof.sizeofVoid();
|
2013-04-10 18:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Run checks against the simplified token list */
|
2019-01-12 07:37:42 +01:00
|
|
|
void runSimplifiedChecks(const Tokenizer* /*tokenizer*/, const Settings* /*settings*/, ErrorLogger* /*errorLogger*/) OVERRIDE {
|
2013-04-10 18:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief %Check for 'sizeof sizeof ..' */
|
|
|
|
void sizeofsizeof();
|
|
|
|
|
|
|
|
/** @brief %Check for calculations inside sizeof */
|
|
|
|
void sizeofCalculation();
|
|
|
|
|
2018-03-15 10:24:17 +01:00
|
|
|
/** @brief %Check for function call inside sizeof */
|
|
|
|
void sizeofFunction();
|
|
|
|
|
2013-04-10 18:49:38 +02:00
|
|
|
/** @brief %Check for suspicious calculations with sizeof results */
|
|
|
|
void suspiciousSizeofCalculation();
|
|
|
|
|
|
|
|
/** @brief %Check for using sizeof with array given as function argument */
|
|
|
|
void checkSizeofForArrayParameter();
|
|
|
|
|
|
|
|
/** @brief %Check for using sizeof of a variable when allocating it */
|
|
|
|
void checkSizeofForPointerSize();
|
|
|
|
|
|
|
|
/** @brief %Check for using sizeof with numeric given as function argument */
|
|
|
|
void checkSizeofForNumericParameter();
|
|
|
|
|
2013-07-05 21:07:07 +02:00
|
|
|
/** @brief %Check for using sizeof(void) */
|
|
|
|
void sizeofVoid();
|
|
|
|
|
2013-04-10 18:49:38 +02:00
|
|
|
private:
|
|
|
|
// Error messages..
|
|
|
|
void sizeofsizeofError(const Token* tok);
|
|
|
|
void sizeofCalculationError(const Token* tok, bool inconclusive);
|
2018-03-15 10:24:17 +01:00
|
|
|
void sizeofFunctionError(const Token* tok);
|
2013-04-10 18:49:38 +02:00
|
|
|
void multiplySizeofError(const Token* tok);
|
|
|
|
void divideSizeofError(const Token* tok);
|
|
|
|
void sizeofForArrayParameterError(const Token* tok);
|
|
|
|
void sizeofForPointerError(const Token* tok, const std::string &varname);
|
2014-08-08 09:23:07 +02:00
|
|
|
void divideBySizeofError(const Token* tok, const std::string &memfunc);
|
2013-04-10 18:49:38 +02:00
|
|
|
void sizeofForNumericParameterError(const Token* tok);
|
2013-07-05 21:07:07 +02:00
|
|
|
void sizeofVoidError(const Token *tok);
|
|
|
|
void sizeofDereferencedVoidPointerError(const Token *tok, const std::string &varname);
|
2013-07-10 06:40:09 +02:00
|
|
|
void arithOperationsOnVoidPointerError(const Token* tok, const std::string &varname, const std::string &vartype);
|
2013-04-10 18:49:38 +02:00
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
void getErrorMessages(ErrorLogger* errorLogger, const Settings* settings) const OVERRIDE {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckSizeof c(nullptr, settings, errorLogger);
|
|
|
|
|
|
|
|
c.sizeofForArrayParameterError(nullptr);
|
|
|
|
c.sizeofForPointerError(nullptr, "varname");
|
|
|
|
c.divideBySizeofError(nullptr, "memset");
|
|
|
|
c.sizeofForNumericParameterError(nullptr);
|
|
|
|
c.sizeofsizeofError(nullptr);
|
|
|
|
c.sizeofCalculationError(nullptr, false);
|
2018-03-15 10:24:17 +01:00
|
|
|
c.sizeofFunctionError(nullptr);
|
2016-05-07 16:30:54 +02:00
|
|
|
c.multiplySizeofError(nullptr);
|
|
|
|
c.divideSizeofError(nullptr);
|
|
|
|
c.sizeofVoidError(nullptr);
|
|
|
|
c.sizeofDereferencedVoidPointerError(nullptr, "varname");
|
|
|
|
c.arithOperationsOnVoidPointerError(nullptr, "varname", "vartype");
|
2013-04-10 18:49:38 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2013-04-10 18:49:38 +02:00
|
|
|
return "Sizeof";
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
std::string classInfo() const OVERRIDE {
|
2013-04-10 18:49:38 +02:00
|
|
|
return "sizeof() usage checks\n"
|
2014-09-30 14:56:12 +02:00
|
|
|
"- sizeof for array given as function argument\n"
|
|
|
|
"- sizeof for numeric given as function argument\n"
|
|
|
|
"- using sizeof(pointer) instead of the size of pointed data\n"
|
|
|
|
"- look for 'sizeof sizeof ..'\n"
|
|
|
|
"- look for calculations inside sizeof()\n"
|
2018-03-15 10:24:17 +01:00
|
|
|
"- look for function calls inside sizeof()\n"
|
2014-09-30 14:56:12 +02:00
|
|
|
"- look for suspicious calculations with sizeof()\n"
|
|
|
|
"- using 'sizeof(void)' which is undefined\n";
|
2013-04-10 18:49:38 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checksizeofH
|