Cppcheck
Static Public Member Functions | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes
UninitVar Class Reference

Check that uninitialized variables aren't used (using ExecutionPath) More...

Inheritance diagram for UninitVar:
ExecutionPath

List of all members.

Static Public Member Functions

 UninitVar (Check *c, const SymbolDatabase *db, bool isc)
 Startup constructor.
static void analyseFunctions (const Token *const tokens, std::set< std::string > &func)

Static Public Attributes

static std::set< std::string > uvarFunctions
 Functions that don't handle uninitialized variables well.

Private Member Functions

ExecutionPathcopy ()
 Create a copy of this check.
void operator= (const UninitVar &)
 no implementation => compiler error if used
 UninitVar (Check *c, const Variable *v, const SymbolDatabase *db, bool isc)
 internal constructor for creating extra checks
bool is_equal (const ExecutionPath *e) const
 is other execution path equal?
void parserhs (const Token *tok2, std::list< ExecutionPath * > &checks) const
 Parse right hand side expression in statement.
const Tokenparse (const Token &tok, std::list< ExecutionPath * > &checks) const
 parse tokens.
bool parseCondition (const Token &tok, std::list< ExecutionPath * > &checks)
 Parse condition.
void parseLoopBody (const Token *tok, std::list< ExecutionPath * > &checks) const
 Parse loop body.

Static Private Member Functions

static void alloc_pointer (std::list< ExecutionPath * > &checks, unsigned int varid)
 allocating pointer.
static void init_pointer (std::list< ExecutionPath * > &checks, const Token *tok)
 Initializing a pointer value.
static void dealloc_pointer (std::list< ExecutionPath * > &checks, const Token *tok)
 Deallocate a pointer.
static void pointer_assignment (std::list< ExecutionPath * > &checks, const Token *tok1, const Token *tok2)
 Pointer assignment: p = x; if p is a pointer and x is an array/pointer then bail out.
static void init_strncpy (std::list< ExecutionPath * > &checks, const Token *tok)
 Initialize an array with strncpy.
static void init_memset_nonzero (std::list< ExecutionPath * > &checks, const Token *tok)
 Initialize an array with memset (not zero).
static bool use (std::list< ExecutionPath * > &checks, const Token *tok, const int mode)
 use - called from the use* functions below.
static bool use (std::list< ExecutionPath * > &checks, const Token *tok)
 Reading variable.
static void use_array (std::list< ExecutionPath * > &checks, const Token *tok)
 Reading array elements.
static void use_array_mem (std::list< ExecutionPath * > &checks, const Token *tok)
 Reading array elements with a "mem.." function.
static bool use_pointer (std::list< ExecutionPath * > &checks, const Token *tok)
 Bad pointer usage.
static bool use_dead_pointer (std::list< ExecutionPath * > &checks, const Token *tok)
 Using variable.
static bool use_array_or_pointer_data (std::list< ExecutionPath * > &checks, const Token *tok)
 Using variable.

Private Attributes

const SymbolDatabasesymbolDatabase
 pointer to symbol database
const bool isC
const Variablevar
 variable for this check
bool alloc
 is this variable allocated?
bool strncpy_
 is this variable initialized with strncpy (not always zero-terminated)
bool memset_nonzero
 is this variable initialized but not zero-terminated (memset)

Detailed Description

Check that uninitialized variables aren't used (using ExecutionPath)

Definition at line 54 of file checkuninitvar.cpp.


Constructor & Destructor Documentation

UninitVar::UninitVar ( Check c,
const SymbolDatabase db,
bool  isc 
) [inline, explicit, static]

Startup constructor.

Definition at line 57 of file checkuninitvar.cpp.

Referenced by copy(), and parse().

UninitVar::UninitVar ( Check c,
const Variable v,
const SymbolDatabase db,
bool  isc 
) [inline, private]

internal constructor for creating extra checks

Definition at line 71 of file checkuninitvar.cpp.


Member Function Documentation

static void UninitVar::alloc_pointer ( std::list< ExecutionPath * > &  checks,
unsigned int  varid 
) [inline, static, private]

allocating pointer.

For example : p = malloc(10);

Definition at line 99 of file checkuninitvar.cpp.

References alloc, ExecutionPath::bailOutVar(), Variable::isArray(), Variable::isPointer(), var, and ExecutionPath::varId.

Referenced by parse().

static void UninitVar::analyseFunctions ( const Token *const  tokens,
std::set< std::string > &  func 
) [inline, static]
Todo:
enable this code. if pointer is written in function then dead pointer is invalid but valid pointer is ok.

Definition at line 925 of file checkuninitvar.cpp.

References Token::eIncDecOp, Token::isName(), Token::isStandardType(), Token::link(), Token::Match(), Token::next(), Token::str(), Token::tokAt(), and Token::varId().

Referenced by CheckUninitVar::analyse(), and CheckUninitVar::executionPaths().

ExecutionPath* UninitVar::copy ( ) [inline, private, virtual]

Create a copy of this check.

Implements ExecutionPath.

Definition at line 63 of file checkuninitvar.cpp.

References UninitVar().

static void UninitVar::dealloc_pointer ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Deallocate a pointer.

For example: free(p);

Definition at line 141 of file checkuninitvar.cpp.

References alloc, Variable::isArray(), Variable::isPointer(), Variable::name(), ExecutionPath::owner, CheckUninitVar::uninitvarError(), var, ExecutionPath::varId, and Token::varId().

Referenced by parse().

static void UninitVar::init_memset_nonzero ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Initialize an array with memset (not zero).

Definition at line 221 of file checkuninitvar.cpp.

References memset_nonzero, ExecutionPath::varId, and Token::varId().

Referenced by parse().

static void UninitVar::init_pointer ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Initializing a pointer value.

For example: *p = 0;

Definition at line 116 of file checkuninitvar.cpp.

References alloc, Variable::isArray(), use_pointer(), var, ExecutionPath::varId, and Token::varId().

Referenced by parse().

static void UninitVar::init_strncpy ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Initialize an array with strncpy.

Definition at line 206 of file checkuninitvar.cpp.

References strncpy_, ExecutionPath::varId, and Token::varId().

Referenced by parse().

bool UninitVar::is_equal ( const ExecutionPath e) const [inline, private, virtual]

is other execution path equal?

Implements ExecutionPath.

Definition at line 76 of file checkuninitvar.cpp.

References alloc, memset_nonzero, strncpy_, and var.

void UninitVar::operator= ( const UninitVar ) [private]

no implementation => compiler error if used

const Token* UninitVar::parse ( const Token tok,
std::list< ExecutionPath * > &  checks 
) const [inline, private, virtual]
bool UninitVar::parseCondition ( const Token tok,
std::list< ExecutionPath * > &  checks 
) [inline, private, virtual]

Parse condition.

Parameters:
tokfirst token in condition.
checksThe execution paths. All execution paths in the list are executed in the current scope
Returns:
true => bail out all checking

Reimplemented from ExecutionPath.

Definition at line 875 of file checkuninitvar.cpp.

References Token::Match(), Token::next(), CheckNullPointer::parseFunctionCall(), Token::simpleMatch(), skipBrackets(), Token::str(), use(), use_array(), use_array_mem(), use_array_or_pointer_data(), and Token::varId().

void UninitVar::parseLoopBody ( const Token tok,
std::list< ExecutionPath * > &  checks 
) const [inline, private, virtual]

Parse loop body.

Parameters:
tokthe first token in the loop body (the token after the {)
checksThe execution paths

Reimplemented from ExecutionPath.

Definition at line 903 of file checkuninitvar.cpp.

References ExecutionPath::bailOutVar(), Token::linkAt(), Token::next(), parse(), Token::simpleMatch(), Token::str(), and Token::tokAt().

void UninitVar::parserhs ( const Token tok2,
std::list< ExecutionPath * > &  checks 
) const [inline, private]

Parse right hand side expression in statement.

Parameters:
tok2start token of rhs
checksthe execution paths

Definition at line 363 of file checkuninitvar.cpp.

References ExecutionPath::bailOutVar(), Token::link(), Token::Match(), Token::next(), Token::previous(), Token::simpleMatch(), Token::str(), Token::strAt(), Token::tokAt(), use(), use_array_or_pointer_data(), and Token::varId().

Referenced by parse().

static void UninitVar::pointer_assignment ( std::list< ExecutionPath * > &  checks,
const Token tok1,
const Token tok2 
) [inline, static, private]

Pointer assignment: p = x; if p is a pointer and x is an array/pointer then bail out.

Parameters:
checksall available checks
tok1the "p" token
tok2the "x" token

Definition at line 172 of file checkuninitvar.cpp.

References ExecutionPath::bailOutVar(), Variable::isArray(), Variable::isPointer(), var, ExecutionPath::varId, and Token::varId().

Referenced by parse().

static bool UninitVar::use ( std::list< ExecutionPath * > &  checks,
const Token tok,
const int  mode 
) [inline, static, private]
static bool UninitVar::use ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Reading variable.

Use this function in situations when it is invalid to read the data of the variable but not the address.

Parameters:
checksall available checks
tokvariable token
Returns:
if error is found, true is returned

Definition at line 305 of file checkuninitvar.cpp.

References use().

static void UninitVar::use_array ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Reading array elements.

If the variable is not an array then the usage is ok.

Parameters:
checksall available checks
tokvariable token

Definition at line 314 of file checkuninitvar.cpp.

References use().

Referenced by parse(), and parseCondition().

static void UninitVar::use_array_mem ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Reading array elements with a "mem.." function.

It's ok if the array is not null-terminated.

Parameters:
checksall available checks
tokvariable token

Definition at line 323 of file checkuninitvar.cpp.

References use().

Referenced by parse(), and parseCondition().

static bool UninitVar::use_array_or_pointer_data ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Using variable.

. reading from uninitialized array or pointer data is invalid. Example: = x[0];

Parameters:
checksall available checks
tokvariable token
Returns:
if error is found, true is returned

Definition at line 354 of file checkuninitvar.cpp.

References use().

Referenced by parse(), parseCondition(), and parserhs().

static bool UninitVar::use_dead_pointer ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Using variable.

. if it's a dead pointer the usage is invalid.

Parameters:
checksall available checks
tokvariable token
Returns:
if error is found, true is returned

Definition at line 343 of file checkuninitvar.cpp.

References use().

Referenced by parse().

static bool UninitVar::use_pointer ( std::list< ExecutionPath * > &  checks,
const Token tok 
) [inline, static, private]

Bad pointer usage.

If the variable is not a pointer then the usage is ok.

Parameters:
checksall available checks
tokvariable token
Returns:
if error is found, true is returned

Definition at line 333 of file checkuninitvar.cpp.

References use().

Referenced by init_pointer(), and parse().


Member Data Documentation

bool UninitVar::alloc [private]

is this variable allocated?

Definition at line 90 of file checkuninitvar.cpp.

Referenced by alloc_pointer(), dealloc_pointer(), init_pointer(), is_equal(), parse(), and use().

const bool UninitVar::isC [private]

Definition at line 84 of file checkuninitvar.cpp.

Referenced by parse().

bool UninitVar::memset_nonzero [private]

is this variable initialized but not zero-terminated (memset)

Definition at line 96 of file checkuninitvar.cpp.

Referenced by init_memset_nonzero(), is_equal(), and use().

bool UninitVar::strncpy_ [private]

is this variable initialized with strncpy (not always zero-terminated)

Definition at line 93 of file checkuninitvar.cpp.

Referenced by init_strncpy(), is_equal(), and use().

pointer to symbol database

Definition at line 82 of file checkuninitvar.cpp.

Referenced by parse().

const Variable* UninitVar::var [private]

variable for this check

Definition at line 87 of file checkuninitvar.cpp.

Referenced by alloc_pointer(), dealloc_pointer(), init_pointer(), is_equal(), parse(), pointer_assignment(), and use().


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