Cppcheck
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions
CheckBufferOverrun Class Reference

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

#include <checkbufferoverrun.h>

Inheritance diagram for CheckBufferOverrun:
Check

List of all members.

Classes

class  ArrayInfo
 Information about N-dimensional array. More...

Public Member Functions

 CheckBufferOverrun ()
 This constructor is used when registering the CheckClass.
 CheckBufferOverrun (const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 This constructor is used when running checks.
void runSimplifiedChecks (const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 run checks, the token list is simplified
void bufferOverrun ()
 Check for buffer overruns
void arrayIndexThenCheck ()
 Using array index before bounds check.
void executionPaths ()
 Check for buffer overruns by inspecting execution paths
void writeOutsideBufferSize ()
 Check using POSIX write function and writing outside buffer size
void checkSprintfCall (const Token *tok, const MathLib::bigint size)
 Check code that matches: "sprintf ( %varid% , %str% [,)]" when varid is not 0, and report found errors.
void checkStructVariable ()
 Check for buffer overruns - locate struct variables and check them with the .._CheckScope function.
void checkGlobalAndLocalVariable ()
 Check for buffer overruns - locate global variables and local function variables and check them with the checkScope function.
void checkBufferAllocatedWithStrlen ()
 Check for buffer overruns due to allocating strlen(src) bytes instead of (strlen(src)+1) bytes before copying a string.
void checkInsecureCmdLineArgs ()
 Check for buffer overruns due to copying command-line args to fixed-sized buffers without bounds checking.
void negativeIndex ()
 Check for negative index.
void checkScope (const Token *tok, const ArrayInfo &arrayInfo)
 Check for buffer overruns (based on ArrayInfo)
void checkScope (const Token *tok, const std::vector< std::string > &varname, const ArrayInfo &arrayInfo)
 Check for buffer overruns.
void checkScopeForBody (const Token *tok, const ArrayInfo &arrayInfo, bool &bailout)
 Check scope helper function - parse for body.
void parse_for_body (const Token *tok2, const ArrayInfo &arrayInfo, const std::string &strindex, bool condition_out_of_bounds, unsigned int counter_varid, const std::string &min_counter_value, const std::string &max_counter_value)
 Helper function used when parsing for-loops.
void checkReadlinkBufferUsage (const Token *tok, const Token *scope_begin, const MathLib::bigint total_size, const bool is_readlinkat)
 Check readlink or readlinkat() buffer usage.
void checkFunctionParameter (const Token &tok, const unsigned int par, const ArrayInfo &arrayInfo, std::list< const Token * > callstack)
 Helper function for checkFunctionCall - check a function parameter.
void checkFunctionCall (const Token *tok, const ArrayInfo &arrayInfo, std::list< const Token * > callstack)
 Helper function that checks if the array is used and if so calls the checkFunctionCall.
void arrayIndexOutOfBoundsError (const Token *tok, const ArrayInfo &arrayInfo, const std::vector< MathLib::bigint > &index)
void arrayIndexInForLoop (const Token *tok, const ArrayInfo &arrayInfo)
void getErrorMessages (ErrorLogger *errorLogger, const Settings *settings) const
 get error messages

Static Public Member Functions

static MathLib::bigint countSprintfLength (const std::string &input_string, const std::list< const Token * > &parameters)
 Get minimum length of format string result.

Private Member Functions

bool isArrayOfStruct (const Token *tok, int &position)
void arrayIndexOutOfBoundsError (const std::list< const Token * > &callstack, const ArrayInfo &arrayInfo, const std::vector< MathLib::bigint > &index)
void bufferOverrunError (const Token *tok, const std::string &varnames="")
void bufferOverrunError (const std::list< const Token * > &callstack, const std::string &varnames="")
void strncatUsageError (const Token *tok)
void outOfBoundsError (const Token *tok, const std::string &what, const bool show_size_info, const MathLib::bigint &supplied_size, const MathLib::bigint &actual_size)
void sizeArgumentAsCharError (const Token *tok)
void terminateStrncpyError (const Token *tok, const std::string &varname)
void bufferNotZeroTerminatedError (const Token *tok, const std::string &varname, const std::string &function)
void negativeIndexError (const Token *tok, MathLib::bigint index)
void cmdLineArgsError (const Token *tok)
void pointerOutOfBoundsError (const Token *tok, const std::string &object)
void arrayIndexThenCheckError (const Token *tok, const std::string &indexName)
void possibleBufferOverrunError (const Token *tok, const std::string &src, const std::string &dst, bool cat)
void possibleReadlinkBufferOverrunError (const Token *tok, const std::string &funcname, const std::string &varname)
void argumentSizeError (const Token *tok, const std::string &functionName, const std::string &varname)
void writeOutsideBufferSizeError (const Token *tok, const std::size_t stringLength, const MathLib::bigint writeLength, const std::string &functionName)
std::string classInfo () const
 get information about this class, used to generate documentation

Static Private Member Functions

static std::string myName ()

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 50 of file checkbufferoverrun.h.


Constructor & Destructor Documentation

This constructor is used when registering the CheckClass.

Definition at line 54 of file checkbufferoverrun.h.

CheckBufferOverrun::CheckBufferOverrun ( const Tokenizer tokenizer,
const Settings settings,
ErrorLogger errorLogger 
) [inline]

This constructor is used when running checks.

Definition at line 58 of file checkbufferoverrun.h.


Member Function Documentation

void CheckBufferOverrun::argumentSizeError ( const Token tok,
const std::string &  functionName,
const std::string &  varname 
) [private]

Definition at line 179 of file checkbufferoverrun.cpp.

References Check::reportError(), and Severity::warning.

Referenced by checkFunctionParameter(), and getErrorMessages().

void CheckBufferOverrun::arrayIndexInForLoop ( const Token tok,
const ArrayInfo arrayInfo 
)
void CheckBufferOverrun::arrayIndexOutOfBoundsError ( const Token tok,
const ArrayInfo arrayInfo,
const std::vector< MathLib::bigint > &  index 
)
void CheckBufferOverrun::arrayIndexOutOfBoundsError ( const std::list< const Token * > &  callstack,
const ArrayInfo arrayInfo,
const std::vector< MathLib::bigint > &  index 
) [private]
void CheckBufferOverrun::arrayIndexThenCheckError ( const Token tok,
const std::string &  indexName 
) [private]

Definition at line 2216 of file checkbufferoverrun.cpp.

References Check::reportError(), and Severity::style.

Referenced by arrayIndexThenCheck(), and getErrorMessages().

void CheckBufferOverrun::bufferNotZeroTerminatedError ( const Token tok,
const std::string &  varname,
const std::string &  function 
) [private]
void CheckBufferOverrun::bufferOverrunError ( const Token tok,
const std::string &  varnames = "" 
) [private]
void CheckBufferOverrun::bufferOverrunError ( const std::list< const Token * > &  callstack,
const std::string &  varnames = "" 
) [private]

Check for buffer overruns due to allocating strlen(src) bytes instead of (strlen(src)+1) bytes before copying a string.

Definition at line 1823 of file checkbufferoverrun.cpp.

References Check::_tokenizer, bufferOverrunError(), Scope::classEnd, Scope::classStart, SymbolDatabase::functionScopes, Tokenizer::getSymbolDatabase(), Token::Match(), Token::next(), and Token::scope().

Referenced by bufferOverrun().

void CheckBufferOverrun::checkFunctionCall ( const Token tok,
const ArrayInfo arrayInfo,
std::list< const Token * >  callstack 
)

Helper function that checks if the array is used and if so calls the checkFunctionCall.

Parameters:
toktoken that matches "%var% ("
arrayInfothe array information
callstackcall stack. This is used to prevent recursion and to provide better error messages. Pass a empty list from checkScope etc.

Definition at line 730 of file checkbufferoverrun.cpp.

References checkFunctionParameter(), CheckBufferOverrun::ArrayInfo::limit(), Token::Match(), Token::nextArgument(), Token::str(), Token::strAt(), Token::tokAt(), MathLib::toLongNumber(), and CheckBufferOverrun::ArrayInfo::varid().

Referenced by checkFunctionParameter(), and checkScope().

void CheckBufferOverrun::checkFunctionParameter ( const Token tok,
const unsigned int  par,
const ArrayInfo arrayInfo,
std::list< const Token * >  callstack 
)

Check for buffer overruns due to copying command-line args to fixed-sized buffers without bounds checking.

Definition at line 1883 of file checkbufferoverrun.cpp.

References Check::_tokenizer, cmdLineArgsError(), Scope::function, SymbolDatabase::functionScopes, Tokenizer::getSymbolDatabase(), Token::link(), Token::Match(), Token::next(), Token::simpleMatch(), Token::strAt(), Token::tokAt(), Function::token, and Token::varId().

Referenced by bufferOverrun().

void CheckBufferOverrun::checkReadlinkBufferUsage ( const Token tok,
const Token scope_begin,
const MathLib::bigint  total_size,
const bool  is_readlinkat 
)
void CheckBufferOverrun::checkScope ( const Token tok,
const ArrayInfo arrayInfo 
)
void CheckBufferOverrun::checkScope ( const Token tok,
const std::vector< std::string > &  varname,
const ArrayInfo arrayInfo 
)
void CheckBufferOverrun::checkScopeForBody ( const Token tok,
const ArrayInfo arrayInfo,
bool &  bailout 
)
void CheckBufferOverrun::checkSprintfCall ( const Token tok,
const MathLib::bigint  size 
)

Check code that matches: "sprintf ( %varid% , %str% [,)]" when varid is not 0, and report found errors.

Parameters:
tokThe "sprintf" token.
sizeThe size of the buffer where sprintf is writing.

Definition at line 1784 of file checkbufferoverrun.cpp.

References bufferOverrunError(), countSprintfLength(), Token::eString, Token::isNumber(), Token::Match(), Token::next(), Token::nextArgument(), Token::strValue(), Token::tokAt(), and Token::type().

Referenced by checkScope().

std::string CheckBufferOverrun::classInfo ( ) const [inline, private, virtual]

get information about this class, used to generate documentation

Implements Check.

Definition at line 265 of file checkbufferoverrun.h.

void CheckBufferOverrun::cmdLineArgsError ( const Token tok) [private]

Definition at line 165 of file checkbufferoverrun.cpp.

References Severity::error, and Check::reportError().

Referenced by checkInsecureCmdLineArgs(), and getErrorMessages().

MathLib::bigint CheckBufferOverrun::countSprintfLength ( const std::string &  input_string,
const std::list< const Token * > &  parameters 
) [static]

Get minimum length of format string result.

Parameters:
input_stringformat string
parametersgiven parameters to sprintf
Returns:
minimum length of resulting string

Definition at line 1683 of file checkbufferoverrun.cpp.

References Token::eString, and Token::getStrLength().

Referenced by checkSprintfCall().

void CheckBufferOverrun::getErrorMessages ( ErrorLogger errorLogger,
const Settings settings 
) const [inline, virtual]
bool CheckBufferOverrun::isArrayOfStruct ( const Token tok,
int &  position 
) [private]

Definition at line 1322 of file checkbufferoverrun.cpp.

References Token::Match(), Token::next(), and Token::tokAt().

Referenced by checkStructVariable().

static std::string CheckBufferOverrun::myName ( ) [inline, static, private]

Definition at line 261 of file checkbufferoverrun.h.

void CheckBufferOverrun::negativeIndexError ( const Token tok,
MathLib::bigint  index 
) [private]

Definition at line 1936 of file checkbufferoverrun.cpp.

References Severity::error, and Check::reportError().

Referenced by getErrorMessages(), and negativeIndex().

void CheckBufferOverrun::outOfBoundsError ( const Token tok,
const std::string &  what,
const bool  show_size_info,
const MathLib::bigint supplied_size,
const MathLib::bigint actual_size 
) [private]
void CheckBufferOverrun::parse_for_body ( const Token tok2,
const ArrayInfo arrayInfo,
const std::string &  strindex,
bool  condition_out_of_bounds,
unsigned int  counter_varid,
const std::string &  min_counter_value,
const std::string &  max_counter_value 
)
void CheckBufferOverrun::pointerOutOfBoundsError ( const Token tok,
const std::string &  object 
) [private]

Definition at line 142 of file checkbufferoverrun.cpp.

References Severity::portability, and Check::reportError().

Referenced by checkScope(), and getErrorMessages().

void CheckBufferOverrun::possibleBufferOverrunError ( const Token tok,
const std::string &  src,
const std::string &  dst,
bool  cat 
) [private]

Definition at line 97 of file checkbufferoverrun.cpp.

References Check::reportError(), and Severity::warning.

Referenced by checkScope(), and getErrorMessages().

void CheckBufferOverrun::possibleReadlinkBufferOverrunError ( const Token tok,
const std::string &  funcname,
const std::string &  varname 
) [private]

Definition at line 111 of file checkbufferoverrun.cpp.

References Check::reportError(), and Severity::warning.

Referenced by checkReadlinkBufferUsage(), and getErrorMessages().

void CheckBufferOverrun::runSimplifiedChecks ( const Tokenizer tokenizer,
const Settings settings,
ErrorLogger errorLogger 
) [inline, virtual]

run checks, the token list is simplified

ExecutionPath checking..

Implements Check.

Definition at line 62 of file checkbufferoverrun.h.

References arrayIndexThenCheck(), bufferOverrun(), executionPaths(), negativeIndex(), and writeOutsideBufferSize().

void CheckBufferOverrun::sizeArgumentAsCharError ( const Token tok) [private]
void CheckBufferOverrun::strncatUsageError ( const Token tok) [private]
void CheckBufferOverrun::terminateStrncpyError ( const Token tok,
const std::string &  varname 
) [private]

Definition at line 156 of file checkbufferoverrun.cpp.

References Check::reportError(), and Severity::warning.

Referenced by checkScope(), and getErrorMessages().

void CheckBufferOverrun::writeOutsideBufferSizeError ( const Token tok,
const std::size_t  stringLength,
const MathLib::bigint  writeLength,
const std::string &  functionName 
) [private]

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