Cppcheck
checksizeof.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 //---------------------------------------------------------------------------
21 #ifndef checksizeofH
22 #define checksizeofH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "tokenize.h"
28 
29 #include <string>
30 
31 class ErrorLogger;
32 class Settings;
33 class Token;
34 
35 /// @addtogroup Checks
36 /// @{
37 
38 
39 /** @brief checks on usage of sizeof() operator */
40 
41 class CPPCHECKLIB CheckSizeof : public Check {
42 public:
43  /** @brief This constructor is used when registering the CheckClass */
44  CheckSizeof() : Check(myName()) {}
45 
46 private:
47  /** @brief This constructor is used when running checks. */
48  CheckSizeof(const Tokenizer* tokenizer, const Settings* settings, ErrorLogger* errorLogger)
49  : Check(myName(), tokenizer, settings, errorLogger) {}
50 
51  /** @brief Run checks against the normal token list */
52  void runChecks(const Tokenizer& tokenizer, ErrorLogger* errorLogger) override {
53  CheckSizeof checkSizeof(&tokenizer, &tokenizer.getSettings(), errorLogger);
54 
55  // Checks
56  checkSizeof.sizeofsizeof();
57  checkSizeof.sizeofCalculation();
58  checkSizeof.sizeofFunction();
59  checkSizeof.suspiciousSizeofCalculation();
60  checkSizeof.checkSizeofForArrayParameter();
61  checkSizeof.checkSizeofForPointerSize();
62  checkSizeof.checkSizeofForNumericParameter();
63  checkSizeof.sizeofVoid();
64  }
65 
66  /** @brief %Check for 'sizeof sizeof ..' */
67  void sizeofsizeof();
68 
69  /** @brief %Check for calculations inside sizeof */
70  void sizeofCalculation();
71 
72  /** @brief %Check for function call inside sizeof */
73  void sizeofFunction();
74 
75  /** @brief %Check for suspicious calculations with sizeof results */
76  void suspiciousSizeofCalculation();
77 
78  /** @brief %Check for using sizeof with array given as function argument */
79  void checkSizeofForArrayParameter();
80 
81  /** @brief %Check for using sizeof of a variable when allocating it */
82  void checkSizeofForPointerSize();
83 
84  /** @brief %Check for using sizeof with numeric given as function argument */
85  void checkSizeofForNumericParameter();
86 
87  /** @brief %Check for using sizeof(void) */
88  void sizeofVoid();
89 
90  // Error messages..
91  void sizeofsizeofError(const Token* tok);
92  void sizeofCalculationError(const Token* tok, bool inconclusive);
93  void sizeofFunctionError(const Token* tok);
94  void multiplySizeofError(const Token* tok);
95  void divideSizeofError(const Token* tok);
96  void sizeofForArrayParameterError(const Token* tok);
97  void sizeofForPointerError(const Token* tok, const std::string &varname);
98  void divideBySizeofError(const Token* tok, const std::string &memfunc);
99  void sizeofForNumericParameterError(const Token* tok);
100  void sizeofVoidError(const Token *tok);
101  void sizeofDereferencedVoidPointerError(const Token *tok, const std::string &varname);
102  void arithOperationsOnVoidPointerError(const Token* tok, const std::string &varname, const std::string &vartype);
103 
104  void getErrorMessages(ErrorLogger* errorLogger, const Settings* settings) const override {
105  CheckSizeof c(nullptr, settings, errorLogger);
106  c.sizeofForArrayParameterError(nullptr);
107  c.sizeofForPointerError(nullptr, "varname");
108  c.divideBySizeofError(nullptr, "memset");
110  c.sizeofsizeofError(nullptr);
111  c.sizeofCalculationError(nullptr, false);
112  c.sizeofFunctionError(nullptr);
113  c.multiplySizeofError(nullptr);
114  c.divideSizeofError(nullptr);
115  c.sizeofVoidError(nullptr);
116  c.sizeofDereferencedVoidPointerError(nullptr, "varname");
117  c.arithOperationsOnVoidPointerError(nullptr, "varname", "vartype");
118  }
119 
120  static std::string myName() {
121  return "Sizeof";
122  }
123 
124  std::string classInfo() const override {
125  return "sizeof() usage checks\n"
126  "- sizeof for array given as function argument\n"
127  "- sizeof for numeric given as function argument\n"
128  "- using sizeof(pointer) instead of the size of pointed data\n"
129  "- look for 'sizeof sizeof ..'\n"
130  "- look for calculations inside sizeof()\n"
131  "- look for function calls inside sizeof()\n"
132  "- look for suspicious calculations with sizeof()\n"
133  "- using 'sizeof(void)' which is undefined\n";
134  }
135 };
136 /// @}
137 //---------------------------------------------------------------------------
138 #endif // checksizeofH
checks on usage of sizeof() operator
Definition: checksizeof.h:41
void sizeofVoid()
Check for using sizeof(void)
void sizeofFunction()
Check for function call inside sizeof
void sizeofDereferencedVoidPointerError(const Token *tok, const std::string &varname)
void sizeofForArrayParameterError(const Token *tok)
CheckSizeof()
This constructor is used when registering the CheckClass.
Definition: checksizeof.h:44
void sizeofVoidError(const Token *tok)
void checkSizeofForNumericParameter()
Check for using sizeof with numeric given as function argument
Definition: checksizeof.cpp:47
void checkSizeofForPointerSize()
Check for using sizeof of a variable when allocating it
void suspiciousSizeofCalculation()
Check for suspicious calculations with sizeof results
void sizeofsizeofError(const Token *tok)
void sizeofForNumericParameterError(const Token *tok)
Definition: checksizeof.cpp:65
void divideBySizeofError(const Token *tok, const std::string &memfunc)
void checkSizeofForArrayParameter()
Check for using sizeof with array given as function argument
Definition: checksizeof.cpp:77
void divideSizeofError(const Token *tok)
std::string classInfo() const override
get information about this class, used to generate documentation
Definition: checksizeof.h:124
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
Run checks against the normal token list.
Definition: checksizeof.h:52
void sizeofsizeof()
Check for 'sizeof sizeof ..'
void multiplySizeofError(const Token *tok)
void sizeofFunctionError(const Token *tok)
void sizeofForPointerError(const Token *tok, const std::string &varname)
void arithOperationsOnVoidPointerError(const Token *tok, const std::string &varname, const std::string &vartype)
void sizeofCalculationError(const Token *tok, bool inconclusive)
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
Definition: checksizeof.h:104
CheckSizeof(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
Definition: checksizeof.h:48
static std::string myName()
Definition: checksizeof.h:120
void sizeofCalculation()
Check for calculations inside sizeof
Interface class that cppcheck uses to communicate with the checks.
Definition: check.h:59
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
The main purpose is to tokenize the source code.
Definition: tokenize.h:46
const Settings & getSettings() const
Definition: tokenize.h:615
#define CPPCHECKLIB
Definition: config.h:35