Cppcheck
Public Member Functions | Private Member Functions | Static Private Member Functions | List of all members
CheckBufferOverrun Class Reference

buffer overruns and array index out of bounds More...

#include <checkbufferoverrun.h>

Inheritance diagram for CheckBufferOverrun:
Check

Public Member Functions

 CheckBufferOverrun ()
 This constructor is used when registering the CheckClass. More...
 
- Public Member Functions inherited from Check
 Check (const std::string &aname)
 This constructor is used when registering the CheckClass. More...
 
virtual ~Check ()
 
 Check (const Check &)=delete
 
Checkoperator= (const Check &)=delete
 
const std::string & name () const
 class name, used to generate documentation More...
 

Private Member Functions

 CheckBufferOverrun (const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 This constructor is used when running checks. More...
 
void runChecks (const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
 run checks, the token list is not simplified More...
 
void getErrorMessages (ErrorLogger *errorLogger, const Settings *settings) const override
 get error messages More...
 
Check::FileInfogetFileInfo (const Tokenizer &tokenizer, const Settings &settings) const override
 Parse current TU and extract file info. More...
 
bool analyseWholeProgram (const CTU::FileInfo *ctu, const std::list< Check::FileInfo * > &fileInfo, const Settings &settings, ErrorLogger &errorLogger) override
 Analyse all file infos for all TU. More...
 
void arrayIndex ()
 
void arrayIndexError (const Token *tok, const std::vector< Dimension > &dimensions, const std::vector< ValueFlow::Value > &indexes)
 
void negativeIndexError (const Token *tok, const std::vector< Dimension > &dimensions, const std::vector< ValueFlow::Value > &indexes)
 
void pointerArithmetic ()
 
void pointerArithmeticError (const Token *tok, const Token *indexToken, const ValueFlow::Value *indexValue)
 
void bufferOverflow ()
 
void bufferOverflowError (const Token *tok, const ValueFlow::Value *value, Certainty certainty)
 
void arrayIndexThenCheck ()
 
void arrayIndexThenCheckError (const Token *tok, const std::string &indexName)
 
void stringNotZeroTerminated ()
 
void terminateStrncpyError (const Token *tok, const std::string &varname)
 
void argumentSize ()
 
void argumentSizeError (const Token *tok, const std::string &functionName, nonneg int paramIndex, const std::string &paramExpression, const Variable *paramVar, const Variable *functionArg)
 
void negativeArraySize ()
 
void negativeArraySizeError (const Token *tok)
 
void negativeMemoryAllocationSizeError (const Token *tok, const ValueFlow::Value *value)
 
void objectIndex ()
 
void objectIndexError (const Token *tok, const ValueFlow::Value *v, bool known)
 
ValueFlow::Value getBufferSize (const Token *bufTok) const
 
Check::FileInfoloadFileInfoFromXml (const tinyxml2::XMLElement *xmlElement) const override
 
std::string classInfo () const override
 get information about this class, used to generate documentation More...
 

Static Private Member Functions

static bool isCtuUnsafeBufferUsage (const Settings &settings, const Token *argtok, MathLib::bigint *offset, int type)
 
static bool isCtuUnsafeArrayIndex (const Settings &settings, const Token *argtok, MathLib::bigint *offset)
 
static bool isCtuUnsafePointerArith (const Settings &settings, const Token *argtok, MathLib::bigint *offset)
 
static bool analyseWholeProgram1 (const std::map< std::string, std::list< const CTU::FileInfo::CallBase * >> &callsMap, const CTU::FileInfo::UnsafeUsage &unsafeUsage, int type, ErrorLogger &errorLogger)
 
static std::string myName ()
 

Additional Inherited Members

- Static Public Member Functions inherited from Check
static std::list< Check * > & instances ()
 List of registered check classes. More...
 
static void writeToErrorList (const ErrorMessage &errmsg)
 Write given error to stdout in xml format. More...
 
- Protected Member Functions inherited from Check
 Check (std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 This constructor is used when running checks. More...
 
void reportError (const Token *tok, const Severity severity, const std::string &id, const std::string &msg)
 report an error More...
 
void reportError (const Token *tok, const Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
 report an error More...
 
void reportError (const std::list< const Token * > &callstack, Severity severity, const std::string &id, const std::string &msg)
 report an error More...
 
void reportError (const std::list< const Token * > &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
 report an error More...
 
void reportError (const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
 
void logChecker (const char id[])
 log checker More...
 
ErrorPath getErrorPath (const Token *errtok, const ValueFlow::Value *value, std::string bug) const
 
bool wrongData (const Token *tok, const char *str)
 Use WRONG_DATA in checkers when you check for wrong data. More...
 
- Static Protected Member Functions inherited from Check
static std::string getMessageId (const ValueFlow::Value &value, const char id[])
 
- Protected Attributes inherited from Check
const Tokenizer *const mTokenizer {}
 
const Settings *const mSettings {}
 
ErrorLogger *const mErrorLogger {}
 

Detailed Description

buffer overruns and array index out of bounds

Buffer overrun and array index out of bounds are pretty much the same. But I generally use 'array index' if the code contains []. And the given index is out of bounds. I generally use 'buffer overrun' if you for example call a strcpy or other function and pass a buffer and reads or writes too much data.

Definition at line 59 of file checkbufferoverrun.h.

Constructor & Destructor Documentation

◆ CheckBufferOverrun() [1/2]

CheckBufferOverrun::CheckBufferOverrun ( )
inline

This constructor is used when registering the CheckClass.

Definition at line 62 of file checkbufferoverrun.h.

◆ CheckBufferOverrun() [2/2]

CheckBufferOverrun::CheckBufferOverrun ( const Tokenizer tokenizer,
const Settings settings,
ErrorLogger errorLogger 
)
inlineprivate

This constructor is used when running checks.

Definition at line 66 of file checkbufferoverrun.h.

Member Function Documentation

◆ analyseWholeProgram()

bool CheckBufferOverrun::analyseWholeProgram ( const CTU::FileInfo ctu,
const std::list< Check::FileInfo * > &  fileInfo,
const Settings settings,
ErrorLogger errorLogger 
)
overrideprivatevirtual

Analyse all file infos for all TU.

Reimplemented from Check.

Definition at line 986 of file checkbufferoverrun.cpp.

References analyseWholeProgram1(), CTU::FileInfo::getCallsMap(), and Check::logChecker().

◆ analyseWholeProgram1()

bool CheckBufferOverrun::analyseWholeProgram1 ( const std::map< std::string, std::list< const CTU::FileInfo::CallBase * >> &  callsMap,
const CTU::FileInfo::UnsafeUsage unsafeUsage,
int  type,
ErrorLogger errorLogger 
)
staticprivate

◆ argumentSize()

void CheckBufferOverrun::argumentSize ( )
private

◆ argumentSizeError()

void CheckBufferOverrun::argumentSizeError ( const Token tok,
const std::string &  functionName,
nonneg int  paramIndex,
const std::string &  paramExpression,
const Variable paramVar,
const Variable functionArg 
)
private

◆ arrayIndex()

void CheckBufferOverrun::arrayIndex ( )
private

◆ arrayIndexError()

void CheckBufferOverrun::arrayIndexError ( const Token tok,
const std::vector< Dimension > &  dimensions,
const std::vector< ValueFlow::Value > &  indexes 
)
private

◆ arrayIndexThenCheck()

void CheckBufferOverrun::arrayIndexThenCheck ( )
private

◆ arrayIndexThenCheckError()

void CheckBufferOverrun::arrayIndexThenCheckError ( const Token tok,
const std::string &  indexName 
)
private

◆ bufferOverflow()

void CheckBufferOverrun::bufferOverflow ( )
private

◆ bufferOverflowError()

void CheckBufferOverrun::bufferOverflowError ( const Token tok,
const ValueFlow::Value value,
Certainty  certainty 
)
private

◆ classInfo()

std::string CheckBufferOverrun::classInfo ( ) const
inlineoverrideprivatevirtual

get information about this class, used to generate documentation

Implements Check.

Definition at line 145 of file checkbufferoverrun.h.

◆ getBufferSize()

ValueFlow::Value CheckBufferOverrun::getBufferSize ( const Token bufTok) const
private

◆ getErrorMessages()

void CheckBufferOverrun::getErrorMessages ( ErrorLogger errorLogger,
const Settings settings 
) const
inlineoverrideprivatevirtual

◆ getFileInfo()

Check::FileInfo * CheckBufferOverrun::getFileInfo ( const Tokenizer tokenizer,
const Settings settings 
) const
overrideprivatevirtual

Parse current TU and extract file info.

Reimplemented from Check.

Definition at line 950 of file checkbufferoverrun.cpp.

References CTU::getUnsafeUsage(), isCtuUnsafeArrayIndex(), and isCtuUnsafePointerArith().

◆ isCtuUnsafeArrayIndex()

bool CheckBufferOverrun::isCtuUnsafeArrayIndex ( const Settings settings,
const Token argtok,
MathLib::bigint offset 
)
staticprivate

Definition at line 939 of file checkbufferoverrun.cpp.

References isCtuUnsafeBufferUsage().

Referenced by getFileInfo().

◆ isCtuUnsafeBufferUsage()

bool CheckBufferOverrun::isCtuUnsafeBufferUsage ( const Settings settings,
const Token argtok,
MathLib::bigint offset,
int  type 
)
staticprivate

◆ isCtuUnsafePointerArith()

bool CheckBufferOverrun::isCtuUnsafePointerArith ( const Settings settings,
const Token argtok,
MathLib::bigint offset 
)
staticprivate

Definition at line 944 of file checkbufferoverrun.cpp.

References isCtuUnsafeBufferUsage().

Referenced by getFileInfo().

◆ loadFileInfoFromXml()

Check::FileInfo * CheckBufferOverrun::loadFileInfoFromXml ( const tinyxml2::XMLElement *  xmlElement) const
overrideprivatevirtual

Reimplemented from Check.

Definition at line 963 of file checkbufferoverrun.cpp.

References arrayIndex(), and CTU::loadUnsafeUsageListFromXml().

◆ myName()

static std::string CheckBufferOverrun::myName ( )
inlinestaticprivate

Definition at line 141 of file checkbufferoverrun.h.

◆ negativeArraySize()

void CheckBufferOverrun::negativeArraySize ( )
private

◆ negativeArraySizeError()

void CheckBufferOverrun::negativeArraySizeError ( const Token tok)
private

◆ negativeIndexError()

void CheckBufferOverrun::negativeIndexError ( const Token tok,
const std::vector< Dimension > &  dimensions,
const std::vector< ValueFlow::Value > &  indexes 
)
private

◆ negativeMemoryAllocationSizeError()

void CheckBufferOverrun::negativeMemoryAllocationSizeError ( const Token tok,
const ValueFlow::Value value 
)
private

◆ objectIndex()

void CheckBufferOverrun::objectIndex ( )
private

◆ objectIndexError()

void CheckBufferOverrun::objectIndexError ( const Token tok,
const ValueFlow::Value v,
bool  known 
)
private

◆ pointerArithmetic()

void CheckBufferOverrun::pointerArithmetic ( )
private

◆ pointerArithmeticError()

void CheckBufferOverrun::pointerArithmeticError ( const Token tok,
const Token indexToken,
const ValueFlow::Value indexValue 
)
private

◆ runChecks()

void CheckBufferOverrun::runChecks ( const Tokenizer ,
ErrorLogger  
)
inlineoverrideprivatevirtual

◆ stringNotZeroTerminated()

void CheckBufferOverrun::stringNotZeroTerminated ( )
private

◆ terminateStrncpyError()

void CheckBufferOverrun::terminateStrncpyError ( const Token tok,
const std::string &  varname 
)
private

Definition at line 807 of file checkbufferoverrun.cpp.

References CWE170, inconclusive, Check::reportError(), and warning.

Referenced by stringNotZeroTerminated().


The documentation for this class was generated from the following files: