Cppcheck
checknullpointer.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 checknullpointerH
22 #define checknullpointerH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "tokenize.h"
28 #include "vfvalue.h"
29 
30 #include <list>
31 #include <string>
32 
33 class ErrorLogger;
34 class Library;
35 class Settings;
36 class Token;
37 
38 namespace CTU {
39  class FileInfo;
40 }
41 
42 namespace tinyxml2 {
43  class XMLElement;
44 }
45 
46 /// @addtogroup Checks
47 /// @{
48 
49 
50 /** @brief check for null pointer dereferencing */
51 
53  friend class TestNullPointer;
54 
55 public:
56  /** @brief This constructor is used when registering the CheckNullPointer */
57  CheckNullPointer() : Check(myName()) {}
58 
59  /**
60  * Is there a pointer dereference? Everything that should result in
61  * a nullpointer dereference error message will result in a true
62  * return value. If it's unknown if the pointer is dereferenced false
63  * is returned.
64  * @param tok token for the pointer
65  * @param unknown it is not known if there is a pointer dereference (could be reported as a debug message)
66  * @return true => there is a dereference
67  */
68  bool isPointerDeRef(const Token *tok, bool &unknown) const;
69 
70  static bool isPointerDeRef(const Token *tok, bool &unknown, const Settings &settings);
71 
72 private:
73  /**
74  * @brief parse a function call and extract information about variable usage
75  * @param tok first token
76  * @param var variables that the function read / write.
77  * @param library --library files data
78  */
79  static void parseFunctionCall(const Token &tok,
80  std::list<const Token *> &var,
81  const Library &library);
82 
83  /** @brief This constructor is used when running checks. */
84  CheckNullPointer(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
85  : Check(myName(), tokenizer, settings, errorLogger) {}
86 
87  /** @brief Run checks against the normal token list */
88  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
89  CheckNullPointer checkNullPointer(&tokenizer, &tokenizer.getSettings(), errorLogger);
90  checkNullPointer.nullPointer();
91  checkNullPointer.arithmetic();
92  checkNullPointer.nullConstantDereference();
93  }
94 
95  /** @brief possible null pointer dereference */
96  void nullPointer();
97 
98  /** @brief dereferencing null constant (after Tokenizer::simplifyKnownVariables) */
99  void nullConstantDereference();
100 
101  void nullPointerError(const Token *tok) {
102  ValueFlow::Value v(0);
103  v.setKnown();
104  nullPointerError(tok, emptyString, &v, false);
105  }
106  void nullPointerError(const Token *tok, const std::string &varname, const ValueFlow::Value* value, bool inconclusive);
107 
108  /** @brief Parse current TU and extract file info */
109  Check::FileInfo *getFileInfo(const Tokenizer &tokenizer, const Settings &settings) const override;
110 
111  Check::FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const override;
112 
113  /** @brief Analyse all file infos for all TU */
114  bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
115 
116  /** Get error messages. Used by --errorlist */
117  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
118  CheckNullPointer c(nullptr, settings, errorLogger);
119  c.nullPointerError(nullptr, "pointer", nullptr, false);
120  c.pointerArithmeticError(nullptr, nullptr, false);
121  c.redundantConditionWarning(nullptr, nullptr, nullptr, false);
122  }
123 
124  /** Name of check */
125  static std::string myName() {
126  return "Null pointer";
127  }
128 
129  /** class info in WIKI format. Used by --doc */
130  std::string classInfo() const override {
131  return "Null pointers\n"
132  "- null pointer dereferencing\n"
133  "- undefined null pointer arithmetic\n";
134  }
135 
136  /**
137  * @brief Does one part of the check for nullPointer().
138  * Dereferencing a pointer and then checking if it's NULL..
139  */
140  void nullPointerByDeRefAndChec();
141 
142  /** undefined null pointer arithmetic */
143  void arithmetic();
144  void pointerArithmeticError(const Token* tok, const ValueFlow::Value *value, bool inconclusive);
145  void redundantConditionWarning(const Token* tok, const ValueFlow::Value *value, const Token *condition, bool inconclusive);
146 };
147 /// @}
148 //---------------------------------------------------------------------------
149 #endif // checknullpointerH
check for null pointer dereferencing
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
Get error messages.
static std::string myName()
Name of check.
void nullPointer()
possible null pointer dereference
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
Run checks against the normal token list.
CheckNullPointer()
This constructor is used when registering the CheckNullPointer.
void pointerArithmeticError(const Token *tok, const ValueFlow::Value *value, bool inconclusive)
void redundantConditionWarning(const Token *tok, const ValueFlow::Value *value, const Token *condition, bool inconclusive)
std::string classInfo() const override
class info in WIKI format.
void arithmetic()
undefined null pointer arithmetic
void nullConstantDereference()
dereferencing null constant (after Tokenizer::simplifyKnownVariables)
void nullPointerError(const Token *tok)
CheckNullPointer(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
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
Library definitions handling.
Definition: library.h:52
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
void setKnown()
Definition: vfvalue.h:349
static const std::string emptyString
Definition: config.h:127
#define CPPCHECKLIB
Definition: config.h:35
Whole program analysis (ctu=Cross Translation Unit)
Definition: check.h:35
Definition: check.h:31