2010-10-31 11:51:25 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2010-10-31 11:51:25 +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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef checknullpointerH
|
|
|
|
#define checknullpointerH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
2018-12-25 21:11:23 +01:00
|
|
|
#include "ctu.h"
|
2023-08-18 12:03:50 +02:00
|
|
|
#include "tokenize.h"
|
2023-01-26 22:23:22 +01:00
|
|
|
#include "vfvalue.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
class ErrorLogger;
|
|
|
|
class Library;
|
|
|
|
class Settings;
|
|
|
|
class Token;
|
2010-10-31 11:51:25 +01:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
namespace tinyxml2 {
|
|
|
|
class XMLElement;
|
|
|
|
}
|
2010-10-31 11:51:25 +01:00
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief check for null pointer dereferencing */
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckNullPointer : public Check {
|
2023-09-11 11:12:42 +02:00
|
|
|
friend class TestNullPointer;
|
|
|
|
|
2010-10-31 11:51:25 +01:00
|
|
|
public:
|
|
|
|
/** @brief This constructor is used when registering the CheckNullPointer */
|
2021-08-07 20:51:18 +02:00
|
|
|
CheckNullPointer() : Check(myName()) {}
|
2010-10-31 11:51:25 +01:00
|
|
|
|
2023-09-11 11:12:42 +02:00
|
|
|
/**
|
|
|
|
* Is there a pointer dereference? Everything that should result in
|
|
|
|
* a nullpointer dereference error message will result in a true
|
|
|
|
* return value. If it's unknown if the pointer is dereferenced false
|
|
|
|
* is returned.
|
|
|
|
* @param tok token for the pointer
|
|
|
|
* @param unknown it is not known if there is a pointer dereference (could be reported as a debug message)
|
|
|
|
* @return true => there is a dereference
|
|
|
|
*/
|
|
|
|
bool isPointerDeRef(const Token *tok, bool &unknown) const;
|
2010-10-31 11:51:25 +01:00
|
|
|
|
2023-09-11 11:12:42 +02:00
|
|
|
static bool isPointerDeRef(const Token *tok, bool &unknown, const Settings *settings);
|
2010-10-31 11:51:25 +01:00
|
|
|
|
2023-09-11 11:12:42 +02:00
|
|
|
private:
|
2010-10-31 11:51:25 +01:00
|
|
|
/**
|
|
|
|
* @brief parse a function call and extract information about variable usage
|
|
|
|
* @param tok first token
|
|
|
|
* @param var variables that the function read / write.
|
2013-07-15 21:56:31 +02:00
|
|
|
* @param library --library files data
|
2010-10-31 11:51:25 +01:00
|
|
|
*/
|
|
|
|
static void parseFunctionCall(const Token &tok,
|
|
|
|
std::list<const Token *> &var,
|
2017-02-24 19:10:34 +01:00
|
|
|
const Library *library);
|
2010-10-31 11:51:25 +01:00
|
|
|
|
2023-09-11 11:12:42 +02:00
|
|
|
/** @brief This constructor is used when running checks. */
|
|
|
|
CheckNullPointer(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {}
|
2018-12-29 09:26:57 +01:00
|
|
|
|
2023-09-11 11:12:42 +02:00
|
|
|
/** @brief Run checks against the normal token list */
|
|
|
|
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
|
|
|
|
CheckNullPointer checkNullPointer(&tokenizer, tokenizer.getSettings(), errorLogger);
|
|
|
|
checkNullPointer.nullPointer();
|
|
|
|
checkNullPointer.arithmetic();
|
|
|
|
checkNullPointer.nullConstantDereference();
|
|
|
|
}
|
2010-10-31 15:32:19 +01:00
|
|
|
|
2010-10-31 11:51:25 +01:00
|
|
|
/** @brief possible null pointer dereference */
|
|
|
|
void nullPointer();
|
|
|
|
|
|
|
|
/** @brief dereferencing null constant (after Tokenizer::simplifyKnownVariables) */
|
|
|
|
void nullConstantDereference();
|
|
|
|
|
2017-05-15 20:05:11 +02:00
|
|
|
void nullPointerError(const Token *tok) {
|
|
|
|
ValueFlow::Value v(0);
|
|
|
|
v.setKnown();
|
2022-07-10 10:57:29 +02:00
|
|
|
nullPointerError(tok, emptyString, &v, false);
|
2017-05-15 20:05:11 +02:00
|
|
|
}
|
|
|
|
void nullPointerError(const Token *tok, const std::string &varname, const ValueFlow::Value* value, bool inconclusive);
|
2018-12-18 07:56:33 +01:00
|
|
|
|
2018-12-25 21:11:23 +01:00
|
|
|
/* data for multifile checking */
|
|
|
|
class MyFileInfo : public Check::FileInfo {
|
|
|
|
public:
|
|
|
|
/** function arguments that are dereferenced without checking if they are null */
|
|
|
|
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;
|
|
|
|
|
|
|
|
/** Convert MyFileInfo data into xml string */
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string toString() const override;
|
2018-12-25 21:11:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Parse current TU and extract file info */
|
2022-02-10 23:02:24 +01:00
|
|
|
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const override;
|
2018-12-25 21:11:23 +01:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
Check::FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const override;
|
2018-12-25 21:11:23 +01:00
|
|
|
|
|
|
|
/** @brief Analyse all file infos for all TU */
|
2022-02-10 23:02:24 +01:00
|
|
|
bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
|
2018-12-25 21:11:23 +01:00
|
|
|
|
2010-12-31 14:16:22 +01:00
|
|
|
/** Get error messages. Used by --errorlist */
|
2022-02-10 23:02:24 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckNullPointer c(nullptr, settings, errorLogger);
|
|
|
|
c.nullPointerError(nullptr, "pointer", nullptr, false);
|
2018-12-29 21:34:22 +01:00
|
|
|
c.pointerArithmeticError(nullptr, nullptr, false);
|
|
|
|
c.redundantConditionWarning(nullptr, nullptr, nullptr, false);
|
2010-10-31 11:51:25 +01:00
|
|
|
}
|
|
|
|
|
2010-12-31 14:16:22 +01:00
|
|
|
/** Name of check */
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2010-10-31 11:51:25 +01:00
|
|
|
return "Null pointer";
|
|
|
|
}
|
|
|
|
|
2010-12-31 14:16:22 +01:00
|
|
|
/** class info in WIKI format. Used by --doc */
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string classInfo() const override {
|
2010-10-31 11:51:25 +01:00
|
|
|
return "Null pointers\n"
|
2016-10-09 15:15:29 +02:00
|
|
|
"- null pointer dereferencing\n"
|
|
|
|
"- undefined null pointer arithmetic\n";
|
2010-10-31 11:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does one part of the check for nullPointer().
|
|
|
|
* Dereferencing a pointer and then checking if it's NULL..
|
|
|
|
*/
|
|
|
|
void nullPointerByDeRefAndChec();
|
2016-10-09 15:15:29 +02:00
|
|
|
|
|
|
|
/** undefined null pointer arithmetic */
|
|
|
|
void arithmetic();
|
2018-12-29 21:34:22 +01:00
|
|
|
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);
|
2010-10-31 11:51:25 +01:00
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checknullpointerH
|