Cppcheck
checkbufferoverrun.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 checkbufferoverrunH
22 #define checkbufferoverrunH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "ctu.h"
28 #include "errortypes.h"
29 #include "mathlib.h"
30 #include "symboldatabase.h"
31 #include "tokenize.h"
32 #include "vfvalue.h"
33 
34 #include <list>
35 #include <map>
36 #include <string>
37 #include <vector>
38 
39 namespace tinyxml2 {
40  class XMLElement;
41 }
42 
43 class ErrorLogger;
44 class Settings;
45 class Token;
46 
47 /// @addtogroup Checks
48 /// @{
49 
50 /**
51  * @brief buffer overruns and array index out of bounds
52  *
53  * Buffer overrun and array index out of bounds are pretty much the same.
54  * But I generally use 'array index' if the code contains []. And the given
55  * index is out of bounds.
56  * I generally use 'buffer overrun' if you for example call a strcpy or
57  * other function and pass a buffer and reads or writes too much data.
58  */
60 public:
61  /** This constructor is used when registering the CheckClass */
62  CheckBufferOverrun() : Check(myName()) {}
63 
64 private:
65  /** This constructor is used when running checks. */
66  CheckBufferOverrun(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
67  : Check(myName(), tokenizer, settings, errorLogger) {}
68 
69  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
70  CheckBufferOverrun checkBufferOverrun(&tokenizer, &tokenizer.getSettings(), errorLogger);
71  checkBufferOverrun.arrayIndex();
72  checkBufferOverrun.pointerArithmetic();
73  checkBufferOverrun.bufferOverflow();
74  checkBufferOverrun.arrayIndexThenCheck();
75  checkBufferOverrun.stringNotZeroTerminated();
76  checkBufferOverrun.objectIndex();
77  checkBufferOverrun.argumentSize();
78  checkBufferOverrun.negativeArraySize();
79  }
80 
81  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
82  CheckBufferOverrun c(nullptr, settings, errorLogger);
83  c.arrayIndexError(nullptr, std::vector<Dimension>(), std::vector<ValueFlow::Value>());
84  c.pointerArithmeticError(nullptr, nullptr, nullptr);
85  c.negativeIndexError(nullptr, std::vector<Dimension>(), std::vector<ValueFlow::Value>());
86  c.arrayIndexThenCheckError(nullptr, "i");
87  c.bufferOverflowError(nullptr, nullptr, Certainty::normal);
88  c.objectIndexError(nullptr, nullptr, true);
89  c.argumentSizeError(nullptr, "function", 1, "buffer", nullptr, nullptr);
90  c.negativeMemoryAllocationSizeError(nullptr, nullptr);
91  c.negativeArraySizeError(nullptr);
92  }
93 
94  /** @brief Parse current TU and extract file info */
95  Check::FileInfo *getFileInfo(const Tokenizer &tokenizer, const Settings &settings) const override;
96 
97  /** @brief Analyse all file infos for all TU */
98  bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
99 
100  void arrayIndex();
101  void arrayIndexError(const Token* tok,
102  const std::vector<Dimension>& dimensions,
103  const std::vector<ValueFlow::Value>& indexes);
104  void negativeIndexError(const Token* tok,
105  const std::vector<Dimension>& dimensions,
106  const std::vector<ValueFlow::Value>& indexes);
107 
108  void pointerArithmetic();
109  void pointerArithmeticError(const Token *tok, const Token *indexToken, const ValueFlow::Value *indexValue);
110 
111  void bufferOverflow();
112  void bufferOverflowError(const Token *tok, const ValueFlow::Value *value, Certainty certainty);
113 
114  void arrayIndexThenCheck();
115  void arrayIndexThenCheckError(const Token *tok, const std::string &indexName);
116 
117  void stringNotZeroTerminated();
118  void terminateStrncpyError(const Token *tok, const std::string &varname);
119 
120  void argumentSize();
121  void argumentSizeError(const Token *tok, const std::string &functionName, nonneg int paramIndex, const std::string &paramExpression, const Variable *paramVar, const Variable *functionArg);
122 
123  void negativeArraySize();
124  void negativeArraySizeError(const Token* tok);
125  void negativeMemoryAllocationSizeError(const Token* tok, const ValueFlow::Value* value); // provide a negative value to memory allocation function
126 
127  void objectIndex();
128  void objectIndexError(const Token *tok, const ValueFlow::Value *v, bool known);
129 
130  ValueFlow::Value getBufferSize(const Token *bufTok) const;
131 
132  // CTU
133  static bool isCtuUnsafeBufferUsage(const Settings &settings, const Token *argtok, MathLib::bigint *offset, int type);
134  static bool isCtuUnsafeArrayIndex(const Settings &settings, const Token *argtok, MathLib::bigint *offset);
135  static bool isCtuUnsafePointerArith(const Settings &settings, const Token *argtok, MathLib::bigint *offset);
136 
137  Check::FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const override;
138  static bool analyseWholeProgram1(const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, int type, ErrorLogger &errorLogger);
139 
140 
141  static std::string myName() {
142  return "Bounds checking";
143  }
144 
145  std::string classInfo() const override {
146  return "Out of bounds checking:\n"
147  "- Array index out of bounds\n"
148  "- Pointer arithmetic overflow\n"
149  "- Buffer overflow\n"
150  "- Dangerous usage of strncat()\n"
151  "- Using array index before checking it\n"
152  "- Partial string write that leads to buffer that is not zero terminated.\n"
153  "- Check for large enough arrays being passed to functions\n"
154  "- Allocating memory with a negative size\n";
155  }
156 };
157 /// @}
158 //---------------------------------------------------------------------------
159 #endif // checkbufferoverrunH
buffer overruns and array index out of bounds
void arrayIndexThenCheckError(const Token *tok, const std::string &indexName)
std::string classInfo() const override
get information about this class, used to generate documentation
CheckBufferOverrun()
This constructor is used when registering the CheckClass.
CheckBufferOverrun(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
void negativeIndexError(const Token *tok, const std::vector< Dimension > &dimensions, const std::vector< ValueFlow::Value > &indexes)
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
void pointerArithmeticError(const Token *tok, const Token *indexToken, const ValueFlow::Value *indexValue)
void negativeMemoryAllocationSizeError(const Token *tok, const ValueFlow::Value *value)
static std::string myName()
void arrayIndexError(const Token *tok, const std::vector< Dimension > &dimensions, const std::vector< ValueFlow::Value > &indexes)
void negativeArraySizeError(const Token *tok)
void argumentSizeError(const Token *tok, const std::string &functionName, nonneg int paramIndex, const std::string &paramExpression, const Variable *paramVar, const Variable *functionArg)
void bufferOverflowError(const Token *tok, const ValueFlow::Value *value, Certainty certainty)
void objectIndexError(const Token *tok, const ValueFlow::Value *v, bool known)
Base class used for whole-program analysis.
Definition: check.h:103
Interface class that cppcheck uses to communicate with the checks.
Definition: check.h:59
virtual bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list< FileInfo * > &fileInfo, const Settings &, ErrorLogger &)
Definition: check.h:122
virtual FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const
Definition: check.h:116
virtual FileInfo * getFileInfo(const Tokenizer &, const Settings &) const
Definition: check.h:112
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
long long bigint
Definition: mathlib.h:68
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
Information about a member variable.
#define CPPCHECKLIB
Definition: config.h:35
#define nonneg
Definition: config.h:138
Certainty
Definition: errortypes.h:54
Definition: check.h:31