2010-10-31 11:51:25 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 Daniel Marjamäki and 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
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2010-10-31 11:51:25 +01:00
|
|
|
#include "check.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
class Token;
|
2012-01-25 15:16:22 +01:00
|
|
|
class SymbolDatabase;
|
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 {
|
2010-10-31 11:51:25 +01:00
|
|
|
public:
|
|
|
|
/** @brief This constructor is used when registering the CheckNullPointer */
|
2013-08-07 16:27:37 +02:00
|
|
|
CheckNullPointer() : Check(myName()) {
|
|
|
|
}
|
2010-10-31 11:51:25 +01:00
|
|
|
|
|
|
|
/** @brief This constructor is used when running checks. */
|
|
|
|
CheckNullPointer(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2013-08-07 16:27:37 +02:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {
|
|
|
|
}
|
2010-10-31 11:51:25 +01:00
|
|
|
|
|
|
|
/** @brief Run checks against the normal token list */
|
2011-10-13 20:53:06 +02:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2010-10-31 11:51:25 +01:00
|
|
|
CheckNullPointer checkNullPointer(tokenizer, settings, errorLogger);
|
|
|
|
checkNullPointer.nullPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Run checks against the simplified token list */
|
2011-10-13 20:53:06 +02:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2010-10-31 11:51:25 +01:00
|
|
|
CheckNullPointer checkNullPointer(tokenizer, settings, errorLogger);
|
|
|
|
checkNullPointer.nullConstantDereference();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
* @param value 0 => invalid with null pointers as parameter.
|
|
|
|
* non-zero => invalid with uninitialized data.
|
|
|
|
*/
|
|
|
|
static void parseFunctionCall(const Token &tok,
|
|
|
|
std::list<const Token *> &var,
|
2013-07-15 21:56:31 +02:00
|
|
|
const Library *library,
|
2010-10-31 11:51:25 +01:00
|
|
|
unsigned char value);
|
|
|
|
|
2010-10-31 15:32:19 +01: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
|
|
|
|
*/
|
2013-02-01 19:10:14 +01:00
|
|
|
static bool isPointerDeRef(const Token *tok, bool &unknown);
|
2010-10-31 15:32:19 +01:00
|
|
|
|
2010-10-31 11:51:25 +01:00
|
|
|
/** @brief possible null pointer dereference */
|
|
|
|
void nullPointer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does one part of the check for nullPointer().
|
|
|
|
* Checking if pointer is NULL and then dereferencing it..
|
|
|
|
*/
|
|
|
|
void nullPointerByCheckAndDeRef();
|
|
|
|
|
|
|
|
/** @brief dereferencing null constant (after Tokenizer::simplifyKnownVariables) */
|
|
|
|
void nullConstantDereference();
|
|
|
|
|
|
|
|
void nullPointerError(const Token *tok); // variable name unknown / doesn't exist
|
2014-01-22 20:16:31 +01:00
|
|
|
void nullPointerError(const Token *tok, const std::string &varname, bool inconclusive=false);
|
2012-08-05 10:38:48 +02:00
|
|
|
void nullPointerError(const Token *tok, const std::string &varname, const Token* nullcheck, bool inconclusive = false);
|
2012-11-21 08:56:17 +01:00
|
|
|
void nullPointerDefaultArgError(const Token *tok, const std::string &varname);
|
2012-03-16 17:24:03 +01:00
|
|
|
private:
|
|
|
|
|
2010-12-31 14:16:22 +01:00
|
|
|
/** Get error messages. Used by --errorlist */
|
2012-02-18 23:43:51 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
2010-12-29 12:43:29 +01:00
|
|
|
CheckNullPointer c(0, settings, errorLogger);
|
|
|
|
c.nullPointerError(0, "pointer");
|
2010-10-31 11:51:25 +01:00
|
|
|
}
|
|
|
|
|
2010-12-31 14:16:22 +01:00
|
|
|
/** Name of check */
|
2012-08-02 18:50:48 +02: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 */
|
2011-10-13 20:53:06 +02:00
|
|
|
std::string classInfo() const {
|
2010-10-31 11:51:25 +01:00
|
|
|
return "Null pointers\n"
|
|
|
|
"* null pointer dereferencing\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does one part of the check for nullPointer().
|
|
|
|
* looping through items in a linked list in a inner loop..
|
|
|
|
*/
|
|
|
|
void nullPointerLinkedList();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does one part of the check for nullPointer().
|
|
|
|
* Dereferencing a pointer and then checking if it's NULL..
|
|
|
|
*/
|
|
|
|
void nullPointerByDeRefAndChec();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does one part of the check for nullPointer().
|
|
|
|
* -# initialize pointer to 0
|
|
|
|
* -# conditionally assign pointer
|
|
|
|
* -# dereference pointer
|
|
|
|
*/
|
|
|
|
void nullPointerConditionalAssignment();
|
2011-12-25 17:01:45 +01:00
|
|
|
|
2012-11-21 08:56:17 +01:00
|
|
|
/**
|
|
|
|
* @brief Does one part of the check for nullPointer().
|
|
|
|
* -# default argument that sets a pointer to 0
|
|
|
|
* -# dereference pointer
|
|
|
|
*/
|
|
|
|
void nullPointerDefaultArgument();
|
|
|
|
|
2013-04-25 09:25:56 +02:00
|
|
|
/**
|
|
|
|
* @brief Removes any variable that may be assigned from pointerArgs.
|
|
|
|
*/
|
2013-07-30 12:52:27 +02:00
|
|
|
static void removeAssignedVarFromSet(const Token* tok, std::set<unsigned int>& pointerArgs);
|
2013-04-25 09:25:56 +02:00
|
|
|
|
2011-12-25 17:01:45 +01:00
|
|
|
/**
|
|
|
|
* @brief Investigate if function call can make pointer null. If
|
|
|
|
* the pointer is passed by value it can't be made a null pointer.
|
|
|
|
*/
|
2013-03-29 17:55:29 +01:00
|
|
|
static bool CanFunctionAssignPointer(const Token *functiontoken, unsigned int varid, bool& unknown);
|
2010-10-31 11:51:25 +01:00
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checknullpointerH
|