2010-10-31 11:51:25 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 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
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
class Token;
|
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief check for null pointer dereferencing */
|
|
|
|
|
|
|
|
class CheckNullPointer : public Check
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** @brief This constructor is used when registering the CheckNullPointer */
|
2011-02-02 10:29:10 +01: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)
|
2011-02-02 10:29:10 +01:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger)
|
2010-10-31 11:51:25 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
/** @brief Run checks against the normal token list */
|
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
{
|
|
|
|
CheckNullPointer checkNullPointer(tokenizer, settings, errorLogger);
|
|
|
|
checkNullPointer.nullPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Run checks against the simplified token list */
|
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
|
|
|
{
|
|
|
|
CheckNullPointer checkNullPointer(tokenizer, settings, errorLogger);
|
|
|
|
checkNullPointer.nullConstantDereference();
|
|
|
|
checkNullPointer.executionPaths();
|
|
|
|
}
|
|
|
|
|
2011-01-17 19:23:00 +01:00
|
|
|
/** Is string uppercase? */
|
|
|
|
static bool isUpper(const std::string &str);
|
|
|
|
|
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.
|
|
|
|
* @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,
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
static bool isPointerDeRef(const Token *tok, bool &unknown);
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
/** @brief new type of check: check execution paths */
|
|
|
|
void executionPaths();
|
|
|
|
|
|
|
|
void nullPointerError(const Token *tok); // variable name unknown / doesn't exist
|
|
|
|
void nullPointerError(const Token *tok, const std::string &varname);
|
2011-10-09 15:09:02 +02:00
|
|
|
void nullPointerError(const Token *tok, const std::string &varname, const unsigned int line, bool inconclusive = false);
|
2010-10-31 11:51:25 +01:00
|
|
|
|
2010-12-31 14:16:22 +01:00
|
|
|
/** Get error messages. Used by --errorlist */
|
2010-12-29 12:43:29 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
|
2010-10-31 11:51:25 +01:00
|
|
|
{
|
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 */
|
2011-02-02 10:29:10 +01:00
|
|
|
std::string myName() const
|
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 */
|
2010-10-31 11:51:25 +01:00
|
|
|
std::string classInfo() const
|
|
|
|
{
|
|
|
|
return "Null pointers\n"
|
|
|
|
"* null pointer dereferencing\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2010-12-31 18:30:04 +01:00
|
|
|
/**
|
|
|
|
* Check if a variable is a pointer
|
|
|
|
* @param varid variable id for variable
|
|
|
|
*/
|
|
|
|
bool isPointer(const unsigned int varid);
|
|
|
|
|
2010-10-31 11:51:25 +01:00
|
|
|
/**
|
|
|
|
* @brief Does one part of the check for nullPointer().
|
|
|
|
* Locate insufficient null-pointer handling after loop
|
|
|
|
*/
|
|
|
|
void nullPointerAfterLoop();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 struct pointer and then checking if it's NULL..
|
|
|
|
*/
|
|
|
|
void nullPointerStructByDeRefAndChec();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif
|
|
|
|
|