2010-10-31 11:51:25 +01:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2016-01-01 14:34:45 +01:00
* Copyright ( C ) 2007 - 2016 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/>.
*/
//---------------------------------------------------------------------------
# include "checknullpointer.h"
# include "mathlib.h"
2011-03-06 21:23:33 +01:00
# include "symboldatabase.h"
2015-11-29 10:49:10 +01:00
# include "utils.h"
2011-10-12 22:11:27 +02:00
# include <cctype>
2010-10-31 11:51:25 +01:00
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
2011-10-13 20:53:06 +02:00
namespace {
CheckNullPointer instance ;
2010-10-31 11:51:25 +01:00
}
2016-08-25 19:17:07 +02:00
static const CWE CWE476 ( 476U ) ; // NULL Pointer Dereference
2016-12-26 14:14:48 +01:00
static const CWE CWE682 ( 682U ) ; // Incorrect Calculation
2016-01-25 20:01:48 +01:00
2010-10-31 11:51:25 +01:00
//---------------------------------------------------------------------------
2015-01-02 13:35:39 +01:00
static bool checkNullpointerFunctionCallPlausibility ( const Function * func , unsigned int arg )
{
return ! func | | ( func - > argCount ( ) > = arg & & func - > getArgumentVar ( arg - 1 ) & & func - > getArgumentVar ( arg - 1 ) - > isPointer ( ) ) ;
}
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 .
2013-07-15 18:55:40 +02:00
* @ param library - - library files data
2010-10-31 11:51:25 +01:00
*/
2017-02-24 19:10:34 +01:00
void CheckNullPointer : : parseFunctionCall ( const Token & tok , std : : list < const Token * > & var , const Library * library )
2010-10-31 11:51:25 +01:00
{
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( & tok , " %name% ( ) " ) | | ! tok . tokAt ( 2 ) )
2012-02-11 12:26:48 +01:00
return ;
const Token * firstParam = tok . tokAt ( 2 ) ;
const Token * secondParam = firstParam - > nextArgument ( ) ;
2010-10-31 11:51:25 +01:00
// 1st parameter..
2017-02-24 19:10:34 +01:00
if ( Token : : Match ( & tok , " snprintf|vsnprintf|fnprintf|vfnprintf " ) & & secondParam & & secondParam - > str ( ) ! = " 0 " ) // Only if length (second parameter) is not zero
var . push_back ( firstParam ) ;
2010-10-31 11:51:25 +01:00
2017-02-24 19:10:34 +01:00
if ( library | | tok . function ( ) ! = nullptr ) {
2015-08-16 14:22:46 +02:00
const Token * param = firstParam ;
2015-03-14 19:23:33 +01:00
int argnr = 1 ;
while ( param ) {
2017-02-24 19:10:34 +01:00
if ( library & & library - > isnullargbad ( & tok , argnr ) & & checkNullpointerFunctionCallPlausibility ( tok . function ( ) , argnr ) )
var . push_back ( param ) ;
else if ( tok . function ( ) ) {
const Variable * argVar = tok . function ( ) - > getArgumentVar ( argnr - 1 ) ;
if ( argVar & & argVar - > isStlStringType ( ) & & ! argVar - > isArrayOrPointer ( ) )
2015-03-14 19:23:33 +01:00
var . push_back ( param ) ;
}
param = param - > nextArgument ( ) ;
argnr + + ;
}
2010-10-31 11:51:25 +01:00
}
2011-09-05 20:18:58 +02:00
2012-11-06 19:54:52 +01:00
if ( Token : : Match ( & tok , " printf|sprintf|snprintf|fprintf|fnprintf|scanf|sscanf|fscanf|wprintf|swprintf|fwprintf|wscanf|swscanf|fwscanf " ) ) {
2011-10-12 22:11:27 +02:00
const Token * argListTok = 0 ; // Points to first va_list argument
std : : string formatString ;
2012-11-06 19:54:52 +01:00
bool scan = Token : : Match ( & tok , " scanf|sscanf|fscanf|wscanf|swscanf|fwscanf " ) ;
2011-10-23 11:23:48 +02:00
2012-11-06 19:54:52 +01:00
if ( Token : : Match ( & tok , " printf|scanf|wprintf|wscanf ( %str% " ) ) {
2012-02-11 12:26:48 +01:00
formatString = firstParam - > strValue ( ) ;
argListTok = secondParam ;
2012-11-06 19:54:52 +01:00
} else if ( Token : : Match ( & tok , " sprintf|fprintf|sscanf|fscanf|fwprintf|fwscanf|swscanf " ) ) {
2012-02-11 12:26:48 +01:00
const Token * formatStringTok = secondParam ; // Find second parameter (format string)
2015-08-14 20:46:13 +02:00
if ( formatStringTok & & formatStringTok - > tokType ( ) = = Token : : eString ) {
2011-10-23 11:23:48 +02:00
argListTok = formatStringTok - > nextArgument ( ) ; // Find third parameter (first argument of va_args)
2012-02-11 12:26:48 +01:00
formatString = formatStringTok - > strValue ( ) ;
2011-10-23 11:23:48 +02:00
}
2012-11-06 19:54:52 +01:00
} else if ( Token : : Match ( & tok , " snprintf|fnprintf|swprintf " ) & & secondParam ) {
2012-05-11 19:38:19 +02:00
const Token * formatStringTok = secondParam - > nextArgument ( ) ; // Find third parameter (format string)
2015-08-14 20:46:13 +02:00
if ( formatStringTok & & formatStringTok - > tokType ( ) = = Token : : eString ) {
2011-10-23 11:23:48 +02:00
argListTok = formatStringTok - > nextArgument ( ) ; // Find fourth parameter (first argument of va_args)
2012-02-11 12:26:48 +01:00
formatString = formatStringTok - > strValue ( ) ;
2011-10-23 11:23:48 +02:00
}
2011-10-12 22:11:27 +02:00
}
2011-10-23 11:23:48 +02:00
2011-10-13 20:53:06 +02:00
if ( argListTok ) {
2011-10-12 22:11:27 +02:00
bool percent = false ;
2011-10-13 20:53:06 +02:00
for ( std : : string : : iterator i = formatString . begin ( ) ; i ! = formatString . end ( ) ; + + i ) {
if ( * i = = ' % ' ) {
2011-10-12 22:11:27 +02:00
percent = ! percent ;
2011-11-28 21:32:07 +01:00
} else if ( percent ) {
2011-12-02 17:09:32 +01:00
percent = false ;
bool _continue = false ;
2014-03-18 21:41:47 +01:00
while ( ! std : : isalpha ( ( unsigned char ) * i ) ) {
2011-12-02 17:09:32 +01:00
if ( * i = = ' * ' ) {
if ( scan )
_continue = true ;
else
argListTok = argListTok - > nextArgument ( ) ;
}
2011-11-28 21:32:07 +01:00
+ + i ;
2011-11-30 20:23:29 +01:00
if ( ! argListTok | | i = = formatString . end ( ) )
return ;
2011-11-28 21:32:07 +01:00
}
2011-12-02 17:09:32 +01:00
if ( _continue )
continue ;
2011-11-28 21:32:07 +01:00
2017-02-24 19:10:34 +01:00
if ( ( * i = = ' n ' | | * i = = ' s ' | | scan ) ) {
var . push_back ( argListTok ) ;
2011-10-12 22:11:27 +02:00
}
2011-11-28 21:32:07 +01:00
if ( * i ! = ' m ' ) // %m is a non-standard glibc extension that requires no parameter
argListTok = argListTok - > nextArgument ( ) ; // Find next argument
2011-10-16 07:50:34 +02:00
if ( ! argListTok )
2011-10-16 07:06:18 +02:00
break ;
2011-10-12 22:11:27 +02:00
}
}
}
2011-09-05 20:18:58 +02:00
}
2010-10-31 11:51:25 +01:00
}
2015-06-13 16:22:43 +02:00
namespace {
2015-11-30 22:13:49 +01:00
const std : : set < std : : string > stl_stream = make_container < std : : set < std : : string > > ( ) < <
2015-06-13 16:22:43 +02:00
" fstream " < < " ifstream " < < " iostream " < < " istream " < <
" istringstream " < < " ofstream " < < " ostream " < < " ostringstream " < <
" stringstream " < < " wistringstream " < < " wostringstream " < < " wstringstream " ;
}
2010-10-31 11:51:25 +01:00
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
bool CheckNullPointer : : isPointerDeRef ( const Token * tok , bool & unknown )
2010-10-31 15:32:19 +01:00
{
unknown = false ;
2014-05-22 19:48:00 +02:00
const Token * parent = tok - > astParent ( ) ;
if ( ! parent )
return false ;
2016-05-26 19:21:45 +02:00
if ( parent - > str ( ) = = " . " & & parent - > astOperand2 ( ) = = tok )
return isPointerDeRef ( parent , unknown ) ;
2016-01-20 08:42:58 +01:00
const bool firstOperand = parent - > astOperand1 ( ) = = tok ;
2014-05-22 19:48:00 +02:00
while ( parent - > str ( ) = = " ( " & & ( parent - > astOperand2 ( ) = = nullptr & & parent - > strAt ( 1 ) ! = " ) " ) ) { // Skip over casts
parent = parent - > astParent ( ) ;
if ( ! parent )
return false ;
2013-04-03 10:27:08 +02:00
}
2010-10-31 15:32:19 +01:00
// Dereferencing pointer..
2014-05-22 19:48:00 +02:00
if ( parent - > str ( ) = = " * " & & ! parent - > astOperand2 ( ) & & ! Token : : Match ( parent - > tokAt ( - 2 ) , " sizeof|decltype|typeof " ) )
return true ;
// array access
2016-01-20 08:42:58 +01:00
if ( firstOperand & & parent - > str ( ) = = " [ " & & ( ! parent - > astParent ( ) | | parent - > astParent ( ) - > str ( ) ! = " & " ) )
2010-10-31 15:32:19 +01:00
return true ;
2015-01-05 14:54:24 +01:00
// address of member variable / array element
const Token * parent2 = parent ;
while ( Token : : Match ( parent2 , " [|. " ) )
parent2 = parent2 - > astParent ( ) ;
if ( parent2 ! = parent & & parent2 & & parent2 - > str ( ) = = " & " & & ! parent2 - > astOperand2 ( ) )
return false ;
2011-10-18 19:34:14 +02:00
// read/write member variable
2014-05-22 19:48:00 +02:00
if ( firstOperand & & parent - > str ( ) = = " . " & & ( ! parent - > astParent ( ) | | parent - > astParent ( ) - > str ( ) ! = " & " ) ) {
if ( ! parent - > astParent ( ) | | parent - > astParent ( ) - > str ( ) ! = " ( " | | parent - > astParent ( ) = = tok - > previous ( ) )
2011-10-18 19:34:14 +02:00
return true ;
unknown = true ;
return false ;
}
2010-10-31 15:32:19 +01:00
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok , " %name% ( " ) )
2010-10-31 15:32:19 +01:00
return true ;
2010-10-31 15:46:08 +01:00
if ( Token : : Match ( tok , " %var% = %var% . " ) & &
tok - > varId ( ) = = tok - > tokAt ( 2 ) - > varId ( ) )
return true ;
2012-01-25 15:16:22 +01:00
// std::string dereferences nullpointers
2014-05-22 19:48:00 +02:00
if ( Token : : Match ( parent - > tokAt ( - 3 ) , " std :: string|wstring ( " ) & & tok - > strAt ( 1 ) = = " ) " )
2012-01-25 15:16:22 +01:00
return true ;
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( parent - > previous ( ) , " %name% ( " ) & & tok - > strAt ( 1 ) = = " ) " ) {
2013-02-01 19:10:14 +01:00
const Variable * var = tok - > tokAt ( - 2 ) - > variable ( ) ;
2014-09-05 12:03:08 +02:00
if ( var & & ! var - > isPointer ( ) & & ! var - > isArray ( ) & & var - > isStlStringType ( ) )
2012-01-26 16:50:59 +01:00
return true ;
}
2012-01-25 15:16:22 +01:00
2012-03-16 17:24:03 +01:00
// streams dereference nullpointers
2014-05-22 19:48:00 +02:00
if ( Token : : Match ( parent , " <<|>> " ) & & ! firstOperand ) {
2013-02-01 19:10:14 +01:00
const Variable * var = tok - > variable ( ) ;
2013-01-16 15:37:07 +01:00
if ( var & & var - > isPointer ( ) & & Token : : Match ( var - > typeStartToken ( ) , " char|wchar_t " ) ) { // Only outputting or reading to char* can cause problems
2014-05-22 19:48:00 +02:00
const Token * tok2 = parent ; // Find start of statement
2012-03-16 17:24:03 +01:00
for ( ; tok2 ; tok2 = tok2 - > previous ( ) ) {
if ( Token : : Match ( tok2 - > previous ( ) , " ;|{|}|: " ) )
break ;
}
if ( Token : : Match ( tok2 , " std :: cout|cin|cerr " ) )
return true ;
if ( tok2 & & tok2 - > varId ( ) ! = 0 ) {
2013-02-01 19:10:14 +01:00
const Variable * var2 = tok2 - > variable ( ) ;
2014-01-30 05:26:48 +01:00
if ( var2 & & var2 - > isStlType ( stl_stream ) )
2012-03-16 17:24:03 +01:00
return true ;
}
}
}
2014-02-15 16:12:37 +01:00
const Variable * ovar = nullptr ;
2014-05-22 19:48:00 +02:00
if ( Token : : Match ( parent , " +|==|!= " ) | | ( parent - > str ( ) = = " = " & & ! firstOperand ) ) {
if ( parent - > astOperand1 ( ) = = tok & & parent - > astOperand2 ( ) )
ovar = parent - > astOperand2 ( ) - > variable ( ) ;
2014-07-06 14:48:24 +02:00
else if ( parent - > astOperand1 ( ) & & parent - > astOperand2 ( ) = = tok )
2014-05-22 19:48:00 +02:00
ovar = parent - > astOperand1 ( ) - > variable ( ) ;
}
2014-09-05 12:03:08 +02:00
if ( ovar & & ! ovar - > isPointer ( ) & & ! ovar - > isArray ( ) & & ovar - > isStlStringType ( ) )
2013-02-01 19:10:14 +01:00
return true ;
2012-01-25 15:16:22 +01:00
2010-10-31 15:32:19 +01:00
// assume that it's not a dereference (no false positives)
return false ;
}
2011-12-25 17:01:45 +01:00
2010-10-31 11:51:25 +01:00
void CheckNullPointer : : nullPointerLinkedList ( )
{
2015-04-06 15:05:34 +02:00
2017-04-11 11:49:09 +02:00
if ( ! _settings - > isEnabled ( Settings : : WARNING ) )
2015-04-06 15:05:34 +02:00
return ;
2011-12-09 22:28:10 +01:00
const SymbolDatabase * const symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
2010-12-31 14:37:34 +01:00
// looping through items in a linked list in a inner loop.
// Here is an example:
// for (const Token *tok = tokens; tok; tok = tok->next) {
// if (tok->str() == "hello")
// tok = tok->next; // <- tok might become a null pointer!
// }
2011-12-09 22:28:10 +01:00
for ( std : : list < Scope > : : const_iterator i = symbolDatabase - > scopeList . begin ( ) ; i ! = symbolDatabase - > scopeList . end ( ) ; + + i ) {
const Token * const tok1 = i - > classDef ;
// search for a "for" scope..
if ( i - > type ! = Scope : : eFor | | ! tok1 )
2010-10-31 11:51:25 +01:00
continue ;
2010-12-31 14:37:34 +01:00
// is there any dereferencing occurring in the for statement
2011-11-27 07:30:58 +01:00
const Token * end2 = tok1 - > linkAt ( 1 ) ;
2011-11-26 21:02:04 +01:00
for ( const Token * tok2 = tok1 - > tokAt ( 2 ) ; tok2 ! = end2 ; tok2 = tok2 - > next ( ) ) {
2010-12-15 18:45:53 +01:00
// Dereferencing a variable inside the "for" parentheses..
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok2 , " %var% . %name% " ) ) {
2012-01-25 15:16:22 +01:00
// Is this variable a pointer?
2013-02-06 06:39:58 +01:00
const Variable * var = tok2 - > variable ( ) ;
2012-01-25 15:16:22 +01:00
if ( ! var | | ! var - > isPointer ( ) )
2010-10-31 11:51:25 +01:00
continue ;
2013-02-06 06:39:58 +01:00
// Variable id for dereferenced variable
const unsigned int varid ( tok2 - > varId ( ) ) ;
2012-01-25 15:16:22 +01:00
if ( Token : : Match ( tok2 - > tokAt ( - 2 ) , " %varid% ? " , varid ) )
continue ;
2010-10-31 11:51:25 +01:00
// Check usage of dereferenced variable in the loop..
2017-05-15 20:05:11 +02:00
// TODO: Move this to ValueFlow
2012-01-25 15:16:22 +01:00
for ( std : : list < Scope * > : : const_iterator j = i - > nestedList . begin ( ) ; j ! = i - > nestedList . end ( ) ; + + j ) {
2016-07-22 14:35:07 +02:00
const Scope * const scope = * j ;
2012-01-25 15:16:22 +01:00
if ( scope - > type ! = Scope : : eWhile )
continue ;
2010-12-31 14:37:34 +01:00
// TODO: are there false negatives for "while ( %varid% ||"
2012-01-25 15:16:22 +01:00
if ( Token : : Match ( scope - > classDef - > next ( ) , " ( %varid% &&|) " , varid ) ) {
2010-12-31 14:37:34 +01:00
// Make sure there is a "break" or "return" inside the loop.
// Without the "break" a null pointer could be dereferenced in the
// for statement.
2012-01-25 15:16:22 +01:00
for ( const Token * tok4 = scope - > classStart ; tok4 ; tok4 = tok4 - > next ( ) ) {
2012-08-12 12:13:07 +02:00
if ( tok4 = = i - > classEnd ) {
2017-05-15 20:05:11 +02:00
const ValueFlow : : Value v ( scope - > classDef , 0LL ) ;
nullPointerError ( tok1 , var - > name ( ) , & v , false ) ;
2012-08-12 12:13:07 +02:00
break ;
2010-10-31 11:51:25 +01:00
}
2010-12-31 14:37:34 +01:00
// There is a "break" or "return" inside the loop.
// TODO: there can be false negatives. There could still be
// execution paths that are not properly terminated
2010-10-31 11:51:25 +01:00
else if ( tok4 - > str ( ) = = " break " | | tok4 - > str ( ) = = " return " )
break ;
}
}
}
}
}
}
}
void CheckNullPointer : : nullPointerByDeRefAndChec ( )
{
2015-04-10 14:18:52 +02:00
const bool printInconclusive = ( _settings - > inconclusive ) ;
2015-04-07 07:07:08 +02:00
2014-01-17 17:47:59 +01:00
for ( const Token * tok = _tokenizer - > tokens ( ) ; tok ; tok = tok - > next ( ) ) {
2015-04-20 13:46:02 +02:00
if ( Token : : Match ( tok , " sizeof|decltype|typeid|typeof ( " ) ) {
tok = tok - > next ( ) - > link ( ) ;
continue ;
}
2014-01-17 17:47:59 +01:00
const Variable * var = tok - > variable ( ) ;
if ( ! var | | ! var - > isPointer ( ) | | tok = = var - > nameToken ( ) )
continue ;
2014-01-08 17:37:39 +01:00
2014-01-17 17:47:59 +01:00
// Can pointer be NULL?
2014-01-20 06:49:45 +01:00
const ValueFlow : : Value * value = tok - > getValue ( 0 ) ;
2014-01-17 17:47:59 +01:00
if ( ! value )
continue ;
2014-01-08 17:37:39 +01:00
2015-04-10 14:18:52 +02:00
if ( ! printInconclusive & & value - > inconclusive )
2014-03-18 17:04:33 +01:00
continue ;
2014-01-17 17:47:59 +01:00
// Is pointer used as function parameter?
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok - > previous ( ) , " [(,] %name% [,)] " ) ) {
2014-01-17 17:47:59 +01:00
const Token * ftok = tok - > previous ( ) ;
while ( ftok & & ftok - > str ( ) ! = " ( " ) {
if ( ftok - > str ( ) = = " ) " )
ftok = ftok - > link ( ) ;
ftok = ftok - > previous ( ) ;
}
if ( ! ftok | | ! ftok - > previous ( ) )
2013-02-06 06:39:58 +01:00
continue ;
2014-01-17 17:47:59 +01:00
std : : list < const Token * > varlist ;
2017-02-24 19:10:34 +01:00
parseFunctionCall ( * ftok - > previous ( ) , varlist , & _settings - > library ) ;
2014-01-17 17:47:59 +01:00
if ( std : : find ( varlist . begin ( ) , varlist . end ( ) , tok ) ! = varlist . end ( ) ) {
2017-05-15 20:05:11 +02:00
nullPointerError ( tok , tok - > str ( ) , value , value - > inconclusive ) ;
2013-05-03 16:18:44 +02:00
}
2014-01-17 17:47:59 +01:00
continue ;
}
2013-05-03 16:18:44 +02:00
2014-01-17 17:47:59 +01:00
// Pointer dereference.
bool unknown = false ;
2014-01-21 19:50:52 +01:00
if ( ! isPointerDeRef ( tok , unknown ) ) {
2017-05-15 20:05:11 +02:00
if ( unknown )
nullPointerError ( tok , tok - > str ( ) , value , true ) ;
2014-01-17 17:47:59 +01:00
continue ;
2014-01-21 19:50:52 +01:00
}
2010-10-31 11:51:25 +01:00
2017-05-15 20:05:11 +02:00
nullPointerError ( tok , tok - > str ( ) , value , value - > inconclusive ) ;
2010-10-31 11:51:25 +01:00
}
}
void CheckNullPointer : : nullPointer ( )
{
nullPointerLinkedList ( ) ;
2014-08-16 12:48:20 +02:00
nullPointerByDeRefAndChec ( ) ;
2010-10-31 11:51:25 +01:00
}
2015-06-13 16:22:43 +02:00
namespace {
2015-11-30 22:13:49 +01:00
const std : : set < std : : string > stl_istream = make_container < std : : set < std : : string > > ( ) < <
2015-06-13 16:22:43 +02:00
" fstream " < < " ifstream " < < " iostream " < < " istream " < <
" istringstream " < < " stringstream " < < " wistringstream " < < " wstringstream " ;
}
2010-12-15 18:45:53 +01:00
/** Dereferencing null constant (simplified token list) */
2010-10-31 11:51:25 +01:00
void CheckNullPointer : : nullConstantDereference ( )
{
2012-01-21 19:55:32 +01:00
const SymbolDatabase * symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
2014-01-30 05:26:48 +01:00
2012-11-25 14:55:31 +01:00
const std : : size_t functions = symbolDatabase - > functionScopes . size ( ) ;
for ( std : : size_t i = 0 ; i < functions ; + + i ) {
const Scope * scope = symbolDatabase - > functionScopes [ i ] ;
2015-01-08 05:45:31 +01:00
if ( scope - > function = = 0 | | ! scope - > function - > hasBody ( ) ) // We only look for functions with a body
2012-01-21 19:55:32 +01:00
continue ;
2010-10-31 11:51:25 +01:00
2012-11-25 14:55:31 +01:00
const Token * tok = scope - > classStart ;
2012-01-26 16:50:59 +01:00
2013-04-10 21:57:22 +02:00
if ( scope - > function & & scope - > function - > isConstructor ( ) )
2012-11-25 14:55:31 +01:00
tok = scope - > function - > token ; // Check initialization list
2012-01-26 16:50:59 +01:00
2012-11-25 14:55:31 +01:00
for ( ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
2013-10-12 15:19:15 +02:00
if ( Token : : Match ( tok , " sizeof|decltype|typeid|typeof ( " ) )
2012-01-25 15:16:22 +01:00
tok = tok - > next ( ) - > link ( ) ;
2010-10-31 11:51:25 +01:00
2011-10-13 20:53:06 +02:00
else if ( Token : : simpleMatch ( tok , " * 0 " ) ) {
2013-02-28 21:50:29 +01:00
if ( Token : : Match ( tok - > previous ( ) , " return|throw|;|{|}|:|[|(|, " ) | | tok - > previous ( ) - > isOp ( ) ) {
2010-10-31 11:51:25 +01:00
nullPointerError ( tok ) ;
}
}
2010-12-26 19:29:58 +01:00
2012-01-25 15:16:22 +01:00
else if ( Token : : Match ( tok , " 0 [ " ) & & ( tok - > previous ( ) - > str ( ) ! = " & " | | ! Token : : Match ( tok - > next ( ) - > link ( ) - > next ( ) , " [.(] " ) ) )
nullPointerError ( tok ) ;
2015-01-31 10:50:39 +01:00
else if ( Token : : Match ( tok - > previous ( ) , " !!. %name% ( " ) & & ( tok - > previous ( ) - > str ( ) ! = " :: " | | tok - > strAt ( - 2 ) = = " std " ) ) {
2017-02-24 19:44:28 +01:00
if ( Token : : Match ( tok - > tokAt ( 2 ) , " 0|NULL|nullptr ) " ) & & tok - > varId ( ) ) { // constructor call
2013-02-06 06:39:58 +01:00
const Variable * var = tok - > variable ( ) ;
2014-09-05 12:03:08 +02:00
if ( var & & ! var - > isPointer ( ) & & ! var - > isArray ( ) & & var - > isStlStringType ( ) )
2012-02-11 12:26:48 +01:00
nullPointerError ( tok ) ;
2012-04-02 12:12:02 +02:00
} else { // function call
std : : list < const Token * > var ;
2017-02-24 19:10:34 +01:00
parseFunctionCall ( * tok , var , & _settings - > library ) ;
2012-04-02 12:12:02 +02:00
// is one of the var items a NULL pointer?
for ( std : : list < const Token * > : : const_iterator it = var . begin ( ) ; it ! = var . end ( ) ; + + it ) {
2017-02-24 19:44:28 +01:00
if ( Token : : Match ( * it , " 0|NULL|nullptr [,)] " ) ) {
2012-04-02 12:12:02 +02:00
nullPointerError ( * it ) ;
}
2010-12-26 19:29:58 +01:00
}
}
2017-02-24 19:44:28 +01:00
} else if ( Token : : Match ( tok , " std :: string|wstring ( 0|NULL|nullptr ) " ) )
2012-01-25 15:16:22 +01:00
nullPointerError ( tok ) ;
2017-02-24 19:44:28 +01:00
else if ( Token : : Match ( tok - > previous ( ) , " >> 0|NULL|nullptr " ) ) { // Only checking input stream operations is safe here, because otherwise 0 can be an integer as well
2012-03-16 17:24:03 +01:00
const Token * tok2 = tok - > previous ( ) ; // Find start of statement
for ( ; tok2 ; tok2 = tok2 - > previous ( ) ) {
2014-05-17 18:18:20 +02:00
if ( Token : : Match ( tok2 - > previous ( ) , " ;|{|}|:|( " ) )
2012-03-16 17:24:03 +01:00
break ;
}
2014-05-12 17:18:51 +02:00
if ( tok2 & & tok2 - > previous ( ) & & tok2 - > previous ( ) - > str ( ) = = " ( " )
2014-05-17 19:14:29 +02:00
continue ;
2012-03-16 17:24:03 +01:00
if ( Token : : simpleMatch ( tok2 , " std :: cin " ) )
nullPointerError ( tok ) ;
if ( tok2 & & tok2 - > varId ( ) ! = 0 ) {
2013-02-06 06:39:58 +01:00
const Variable * var = tok2 - > variable ( ) ;
2015-06-13 16:22:43 +02:00
if ( var & & var - > isStlType ( stl_istream ) )
2012-03-16 17:24:03 +01:00
nullPointerError ( tok ) ;
}
}
2014-02-16 11:47:52 +01:00
const Variable * ovar = nullptr ;
2015-09-13 10:53:05 +02:00
const Token * tokNull = nullptr ;
2017-02-24 19:44:28 +01:00
if ( Token : : Match ( tok , " 0|NULL|nullptr ==|!=|>|>=|<|<= %var% " ) ) {
2015-09-13 10:53:05 +02:00
if ( ! Token : : Match ( tok - > tokAt ( 3 ) , " .|[ " ) ) {
ovar = tok - > tokAt ( 2 ) - > variable ( ) ;
tokNull = tok ;
}
2017-02-24 19:44:28 +01:00
} else if ( Token : : Match ( tok , " %var% ==|!=|>|>=|<|<= 0|NULL|nullptr " ) | |
Token : : Match ( tok , " %var% =|+ 0|NULL|nullptr )|]|,|;|+ " ) ) {
2013-02-06 06:39:58 +01:00
ovar = tok - > variable ( ) ;
2015-09-13 10:53:05 +02:00
tokNull = tok - > tokAt ( 2 ) ;
}
if ( ovar & & ! ovar - > isPointer ( ) & & ! ovar - > isArray ( ) & & ovar - > isStlStringType ( ) & & tokNull & & tokNull - > originalName ( ) ! = " ' \\ 0' " )
nullPointerError ( tokNull ) ;
2010-10-31 11:51:25 +01:00
}
}
}
2017-05-15 20:05:11 +02:00
void CheckNullPointer : : nullPointerError ( const Token * tok , const std : : string & varname , const ValueFlow : : Value * value , bool inconclusive )
2010-10-31 11:51:25 +01:00
{
2017-05-15 20:05:11 +02:00
const std : : string errmsgcond ( ValueFlow : : eitherTheConditionIsRedundant ( value ? value - > condition : nullptr ) + " or there is possible null pointer dereference: " + varname + " . " ) ;
const std : : string errmsgdefarg ( " Possible null pointer dereference if the default parameter value is used: " + varname ) ;
2010-10-31 11:51:25 +01:00
2017-05-15 20:05:11 +02:00
if ( ! tok ) {
reportError ( tok , Severity : : error , " nullPointer " , " Null pointer dereference " , CWE476 , false ) ;
reportError ( tok , Severity : : warning , " nullPointerDefaultArg " , errmsgdefarg , CWE476 , false ) ;
reportError ( tok , Severity : : warning , " nullPointerRedundantCheck " , errmsgcond , CWE476 , false ) ;
return ;
}
2010-10-31 11:51:25 +01:00
2017-05-15 20:35:57 +02:00
if ( ! value ) {
reportError ( tok , Severity : : error , " nullPointer " , " Null pointer dereference " , CWE476 , inconclusive ) ;
return ;
}
2017-05-15 20:05:11 +02:00
if ( ! _settings - > isEnabled ( value , inconclusive ) )
2016-11-22 23:59:39 +01:00
return ;
2017-05-15 20:05:11 +02:00
2017-05-16 22:38:13 +02:00
const ErrorPath errorPath = getErrorPath ( tok , value ) ;
2017-05-15 20:05:11 +02:00
if ( value - > condition ) {
reportError ( errorPath , Severity : : warning , " nullPointerRedundantCheck " , errmsgcond , CWE476 , inconclusive | | value - > inconclusive ) ;
} else if ( value - > defaultArg ) {
reportError ( errorPath , Severity : : warning , " nullPointerDefaultArg " , errmsgdefarg , CWE476 , inconclusive | | value - > inconclusive ) ;
} else {
std : : string errmsg ;
errmsg = std : : string ( value - > isKnown ( ) ? " Null " : " Possible null " ) + " pointer dereference " ;
if ( ! varname . empty ( ) )
errmsg + = " : " + varname ;
reportError ( errorPath ,
value - > isKnown ( ) ? Severity : : error : Severity : : warning ,
" nullPointer " ,
errmsg ,
CWE476 , inconclusive | | value - > inconclusive ) ;
}
2010-10-31 11:51:25 +01:00
}
2016-10-09 15:15:29 +02:00
void CheckNullPointer : : arithmetic ( )
{
const SymbolDatabase * symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
const std : : size_t functions = symbolDatabase - > functionScopes . size ( ) ;
for ( std : : size_t i = 0 ; i < functions ; + + i ) {
const Scope * scope = symbolDatabase - > functionScopes [ i ] ;
for ( const Token * tok = scope - > classStart - > next ( ) ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
if ( ! tok - > astOperand2 ( ) | | tok - > str ( ) ! = " - " )
continue ;
// pointer subtraction
if ( ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > pointer )
continue ;
// Can LHS be NULL?
const ValueFlow : : Value * value = tok - > astOperand1 ( ) - > getValue ( 0 ) ;
if ( ! value )
continue ;
if ( ! _settings - > inconclusive & & value - > inconclusive )
continue ;
2017-04-11 11:49:09 +02:00
if ( value - > condition & & ! _settings - > isEnabled ( Settings : : WARNING ) )
2016-10-09 15:15:29 +02:00
continue ;
arithmeticError ( tok , value ) ;
}
}
}
void CheckNullPointer : : arithmeticError ( const Token * tok , const ValueFlow : : Value * value )
{
std : : string errmsg ;
if ( value & & value - > condition )
errmsg = ValueFlow : : eitherTheConditionIsRedundant ( value - > condition ) + " or there is overflow in pointer subtraction. " ;
else
errmsg = " Overflow in pointer arithmetic, NULL pointer is subtracted. " ;
std : : list < const Token * > callstack ;
callstack . push_back ( tok ) ;
if ( value & & value - > condition )
callstack . push_back ( value - > condition ) ;
reportError ( callstack ,
( value & & value - > condition ) ? Severity : : warning : Severity : : error ,
( value & & value - > condition ) ? " nullPointerArithmeticRedundantCheck " : " nullPointerArithmetic " ,
errmsg ,
2016-12-25 00:07:37 +01:00
CWE682 , // unknown - pointer overflow
2016-10-09 15:15:29 +02:00
value & & value - > inconclusive ) ;
}