2010-10-31 11:51:25 +01:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2014-02-15 07:45:39 +01:00
* Copyright ( C ) 2007 - 2014 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/>.
*/
//---------------------------------------------------------------------------
# include "checknullpointer.h"
# include "mathlib.h"
2011-03-06 21:23:33 +01:00
# include "symboldatabase.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
}
//---------------------------------------------------------------------------
/**
* @ 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
* @ param value 0 = > invalid with null pointers as parameter .
2013-06-07 06:37:53 +02:00
* 1 - . . = > only invalid with uninitialized data .
2010-10-31 11:51:25 +01:00
*/
2013-07-15 18:55:40 +02:00
void CheckNullPointer : : parseFunctionCall ( const Token & tok , std : : list < const Token * > & var , const Library * library , unsigned char value )
2010-10-31 11:51:25 +01:00
{
// standard functions that dereference first parameter..
2013-06-07 06:37:53 +02:00
static std : : set < std : : string > functionNames1_all ; // used no matter what 'value' is
static std : : set < std : : string > functionNames1_nullptr ; // used only when 'value' is 0
static std : : set < std : : string > functionNames1_uninit ; // used only when 'value' is non-zero
2011-11-28 21:32:07 +01:00
if ( functionNames1_all . empty ( ) ) {
2012-04-26 17:53:21 +02:00
// cstdlib
functionNames1_all . insert ( " atoi " ) ;
functionNames1_all . insert ( " atof " ) ;
functionNames1_all . insert ( " atol " ) ;
functionNames1_all . insert ( " qsort " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " strtof " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " strtod " ) ;
functionNames1_all . insert ( " strtol " ) ;
functionNames1_all . insert ( " strtoul " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " strtold " ) ;
functionNames1_all . insert ( " strtoll " ) ;
functionNames1_all . insert ( " strtoull " ) ;
functionNames1_all . insert ( " wcstof " ) ;
functionNames1_all . insert ( " wcstod " ) ;
functionNames1_all . insert ( " wcstol " ) ;
functionNames1_all . insert ( " wcstoul " ) ;
functionNames1_all . insert ( " wcstold " ) ;
functionNames1_all . insert ( " wcstoll " ) ;
functionNames1_all . insert ( " wcstoull " ) ;
2012-04-26 17:53:21 +02:00
// cstring
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " strcat " ) ;
functionNames1_all . insert ( " strncat " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " strcoll " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " strchr " ) ;
functionNames1_all . insert ( " strrchr " ) ;
functionNames1_all . insert ( " strcmp " ) ;
functionNames1_all . insert ( " strncmp " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " strcspn " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " strdup " ) ;
functionNames1_all . insert ( " strndup " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " strpbrk " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " strlen " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " strspn " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " strstr " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " wcscat " ) ;
functionNames1_all . insert ( " wcsncat " ) ;
functionNames1_all . insert ( " wcscoll " ) ;
functionNames1_all . insert ( " wcschr " ) ;
functionNames1_all . insert ( " wcsrchr " ) ;
functionNames1_all . insert ( " wcscmp " ) ;
functionNames1_all . insert ( " wcsncmp " ) ;
functionNames1_all . insert ( " wcscspn " ) ;
functionNames1_all . insert ( " wcsdup " ) ;
functionNames1_all . insert ( " wcsndup " ) ;
functionNames1_all . insert ( " wcspbrk " ) ;
functionNames1_all . insert ( " wcslen " ) ;
functionNames1_all . insert ( " wcsspn " ) ;
functionNames1_all . insert ( " wcsstr " ) ;
2012-04-26 17:53:21 +02:00
// cstdio
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " fclose " ) ;
functionNames1_all . insert ( " feof " ) ;
functionNames1_all . insert ( " fwrite " ) ;
functionNames1_all . insert ( " fseek " ) ;
functionNames1_all . insert ( " ftell " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " fputs " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " fputws " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " ferror " ) ;
functionNames1_all . insert ( " fgetc " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " fgetwc " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " fgetpos " ) ;
functionNames1_all . insert ( " fsetpos " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " fscanf " ) ;
functionNames1_all . insert ( " fprintf " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " fwscanf " ) ;
functionNames1_all . insert ( " fwprintf " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " fopen " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " rewind " ) ;
functionNames1_all . insert ( " printf " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " wprintf " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " scanf " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " wscanf " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_all . insert ( " fscanf " ) ;
functionNames1_all . insert ( " sscanf " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_all . insert ( " fwscanf " ) ;
functionNames1_all . insert ( " swscanf " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_all . insert ( " setbuf " ) ;
functionNames1_all . insert ( " setvbuf " ) ;
functionNames1_all . insert ( " rename " ) ;
functionNames1_all . insert ( " remove " ) ;
functionNames1_all . insert ( " puts " ) ;
functionNames1_all . insert ( " getc " ) ;
functionNames1_all . insert ( " clearerr " ) ;
// ctime
functionNames1_all . insert ( " asctime " ) ;
functionNames1_all . insert ( " ctime " ) ;
functionNames1_all . insert ( " mktime " ) ;
2011-11-28 21:32:07 +01:00
functionNames1_nullptr . insert ( " strcpy " ) ;
functionNames1_nullptr . insert ( " sprintf " ) ;
functionNames1_nullptr . insert ( " vsprintf " ) ;
functionNames1_nullptr . insert ( " vprintf " ) ;
functionNames1_nullptr . insert ( " fprintf " ) ;
functionNames1_nullptr . insert ( " vfprintf " ) ;
2012-11-06 19:54:52 +01:00
functionNames1_nullptr . insert ( " wcscpy " ) ;
functionNames1_nullptr . insert ( " swprintf " ) ;
functionNames1_nullptr . insert ( " vswprintf " ) ;
functionNames1_nullptr . insert ( " vwprintf " ) ;
functionNames1_nullptr . insert ( " fwprintf " ) ;
functionNames1_nullptr . insert ( " vfwprintf " ) ;
2012-04-26 17:53:21 +02:00
functionNames1_nullptr . insert ( " fread " ) ;
functionNames1_nullptr . insert ( " gets " ) ;
functionNames1_nullptr . insert ( " gmtime " ) ;
functionNames1_nullptr . insert ( " localtime " ) ;
2012-04-27 10:17:07 +02:00
functionNames1_nullptr . insert ( " strftime " ) ;
2012-05-16 09:56:39 +02:00
2013-12-13 07:51:33 +01:00
functionNames1_uninit . insert ( " itoa " ) ; // value to convert
2012-05-16 09:56:39 +02:00
functionNames1_uninit . insert ( " perror " ) ;
functionNames1_uninit . insert ( " fflush " ) ;
2013-11-08 13:22:07 +01:00
functionNames1_uninit . insert ( " freopen " ) ;
2010-10-31 11:51:25 +01:00
}
// standard functions that dereference second parameter..
2013-06-07 06:37:53 +02:00
static std : : set < std : : string > functionNames2_all ; // used no matter what 'value' is
static std : : set < std : : string > functionNames2_nullptr ; // used only if 'value' is 0
2011-11-28 21:32:07 +01:00
if ( functionNames2_all . empty ( ) ) {
2012-04-26 17:53:21 +02:00
functionNames2_all . insert ( " mbstowcs " ) ;
functionNames2_all . insert ( " wcstombs " ) ;
2011-11-28 21:32:07 +01:00
functionNames2_all . insert ( " strcat " ) ;
functionNames2_all . insert ( " strncat " ) ;
functionNames2_all . insert ( " strcmp " ) ;
functionNames2_all . insert ( " strncmp " ) ;
2012-04-26 17:53:21 +02:00
functionNames2_all . insert ( " strcoll " ) ;
2011-11-28 21:32:07 +01:00
functionNames2_all . insert ( " strcpy " ) ;
2012-04-26 17:53:21 +02:00
functionNames2_all . insert ( " strcspn " ) ;
2011-11-28 21:32:07 +01:00
functionNames2_all . insert ( " strncpy " ) ;
2012-04-26 17:53:21 +02:00
functionNames2_all . insert ( " strpbrk " ) ;
functionNames2_all . insert ( " strspn " ) ;
2011-11-28 21:32:07 +01:00
functionNames2_all . insert ( " strstr " ) ;
2012-04-26 17:53:21 +02:00
functionNames2_all . insert ( " strxfrm " ) ;
2012-11-06 19:54:52 +01:00
functionNames2_all . insert ( " wcscat " ) ;
functionNames2_all . insert ( " wcsncat " ) ;
functionNames2_all . insert ( " wcscmp " ) ;
functionNames2_all . insert ( " wcsncmp " ) ;
functionNames2_all . insert ( " wcscoll " ) ;
functionNames2_all . insert ( " wcscpy " ) ;
functionNames2_all . insert ( " wcscspn " ) ;
functionNames2_all . insert ( " wcsncpy " ) ;
functionNames2_all . insert ( " wcspbrk " ) ;
functionNames2_all . insert ( " wcsspn " ) ;
functionNames2_all . insert ( " wcsstr " ) ;
functionNames2_all . insert ( " wcsxfrm " ) ;
2011-11-28 21:32:07 +01:00
functionNames2_all . insert ( " sprintf " ) ;
functionNames2_all . insert ( " fprintf " ) ;
functionNames2_all . insert ( " fscanf " ) ;
functionNames2_all . insert ( " sscanf " ) ;
2012-11-06 19:54:52 +01:00
functionNames2_all . insert ( " swprintf " ) ;
functionNames2_all . insert ( " fwprintf " ) ;
functionNames2_all . insert ( " fwscanf " ) ;
functionNames2_all . insert ( " swscanf " ) ;
2012-04-26 17:53:21 +02:00
functionNames2_all . insert ( " fputs " ) ;
functionNames2_all . insert ( " fputc " ) ;
functionNames2_all . insert ( " ungetc " ) ;
2012-11-06 19:54:52 +01:00
functionNames2_all . insert ( " fputws " ) ;
functionNames2_all . insert ( " fputwc " ) ;
functionNames2_all . insert ( " ungetwc " ) ;
2012-04-26 17:53:21 +02:00
functionNames2_all . insert ( " rename " ) ;
functionNames2_all . insert ( " putc " ) ;
2012-11-06 19:54:52 +01:00
functionNames2_all . insert ( " putwc " ) ;
2012-04-26 17:53:21 +02:00
functionNames2_all . insert ( " freopen " ) ;
2011-11-28 21:32:07 +01:00
2013-12-13 07:51:33 +01:00
functionNames2_nullptr . insert ( " itoa " ) ; // destination buffer
2011-11-28 21:32:07 +01:00
functionNames2_nullptr . insert ( " frexp " ) ;
functionNames2_nullptr . insert ( " modf " ) ;
2012-04-26 17:53:21 +02:00
functionNames2_nullptr . insert ( " fgetpos " ) ;
2010-10-31 11:51:25 +01:00
}
2012-02-11 12:26:48 +01:00
if ( Token : : Match ( & tok , " %var% ( ) " ) | | ! tok . tokAt ( 2 ) )
return ;
const Token * firstParam = tok . tokAt ( 2 ) ;
const Token * secondParam = firstParam - > nextArgument ( ) ;
2010-10-31 11:51:25 +01:00
// 1st parameter..
2012-02-11 12:26:48 +01:00
if ( ( Token : : Match ( firstParam , " %var% ,|) " ) & & firstParam - > varId ( ) > 0 ) | |
2013-06-07 06:37:53 +02:00
( value = = 0 & & Token : : Match ( firstParam , " 0|NULL ,|) " ) ) ) {
2011-11-28 21:32:07 +01:00
if ( functionNames1_all . find ( tok . str ( ) ) ! = functionNames1_all . end ( ) )
2012-02-11 12:26:48 +01:00
var . push_back ( firstParam ) ;
2011-11-28 21:32:07 +01:00
else if ( value = = 0 & & functionNames1_nullptr . find ( tok . str ( ) ) ! = functionNames1_nullptr . end ( ) )
2012-02-11 12:26:48 +01:00
var . push_back ( firstParam ) ;
2012-05-16 09:56:39 +02:00
else if ( value ! = 0 & & functionNames1_uninit . find ( tok . str ( ) ) ! = functionNames1_uninit . end ( ) )
2012-02-11 12:26:48 +01:00
var . push_back ( firstParam ) ;
else if ( value = = 0 & & Token : : Match ( & tok , " snprintf|vsnprintf|fnprintf|vfnprintf " ) & & secondParam & & secondParam - > str ( ) ! = " 0 " ) // Only if length (second parameter) is not zero
var . push_back ( firstParam ) ;
2014-02-15 16:12:37 +01:00
else if ( value = = 0 & & library ! = nullptr & & library - > isnullargbad ( tok . str ( ) , 1 ) )
2013-07-15 21:57:10 +02:00
var . push_back ( firstParam ) ;
2014-02-15 16:12:37 +01:00
else if ( value = = 1 & & library ! = nullptr & & library - > isuninitargbad ( tok . str ( ) , 1 ) )
2013-07-15 21:57:10 +02:00
var . push_back ( firstParam ) ;
}
2010-10-31 11:51:25 +01:00
// 2nd parameter..
2013-11-04 23:09:15 +01:00
if ( ( value = = 0 & & Token : : Match ( secondParam , " 0|NULL ,|) " ) ) | | ( secondParam & & secondParam - > varId ( ) > 0 & & Token : : Match ( secondParam - > next ( ) , " [,)] " ) ) ) {
2012-02-11 12:26:48 +01:00
if ( functionNames2_all . find ( tok . str ( ) ) ! = functionNames2_all . end ( ) )
var . push_back ( secondParam ) ;
else if ( value = = 0 & & functionNames2_nullptr . find ( tok . str ( ) ) ! = functionNames2_nullptr . end ( ) )
var . push_back ( secondParam ) ;
2014-02-15 16:12:37 +01:00
else if ( value = = 0 & & library ! = nullptr & & library - > isnullargbad ( tok . str ( ) , 2 ) )
2013-07-15 21:57:10 +02:00
var . push_back ( secondParam ) ;
2014-02-15 16:12:37 +01:00
else if ( value = = 1 & & library ! = nullptr & & library - > isuninitargbad ( tok . str ( ) , 2 ) )
2013-07-15 21:57:10 +02:00
var . push_back ( secondParam ) ;
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)
2012-04-25 09:56:07 +02:00
if ( formatStringTok & & formatStringTok - > type ( ) = = 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)
2012-04-25 09:56:07 +02:00
if ( formatStringTok & & formatStringTok - > type ( ) = = 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 ;
2011-11-28 21:32:07 +01:00
while ( ! std : : isalpha ( * 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
2011-10-16 07:06:18 +02:00
if ( ( * i = = ' n ' | | * i = = ' s ' | | scan ) & & ( ! scan | | value = = 0 ) ) {
2013-11-04 23:09:15 +01:00
if ( ( value = = 0 & & argListTok - > str ( ) = = " 0 " ) | | ( argListTok - > varId ( ) > 0 & & Token : : Match ( argListTok , " %var% [,)] " ) ) ) {
2011-10-12 22:11:27 +02:00
var . push_back ( argListTok ) ;
}
}
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
}
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
{
2014-01-30 05:26:48 +01:00
// THIS ARRAY MUST BE ORDERED ALPHABETICALLY
static const char * const stl_stream [ ] = {
" fstream " , " ifstream " , " iostream " , " istream " ,
" istringstream " , " ofstream " , " ostream " , " ostringstream " ,
" stringstream " , " wistringstream " , " wostringstream " , " wstringstream "
} ;
2011-12-12 07:35:53 +01:00
const bool inconclusive = unknown ;
2010-10-31 15:32:19 +01:00
unknown = false ;
2013-04-03 10:27:08 +02:00
const Token * prev = tok - > previous ( ) ;
while ( prev - > str ( ) = = " . " | | prev - > str ( ) = = " :: " ) { // Skip over previous member dereferences
prev = prev - > previous ( ) ;
while ( prev - > link ( ) & & ( prev - > str ( ) = = " ] " | | prev - > str ( ) = = " > " ) )
prev = prev - > link ( ) - > previous ( ) ;
prev = prev - > previous ( ) ;
}
while ( prev - > str ( ) = = " ) " ) // Skip over casts
prev = prev - > link ( ) - > previous ( ) ;
2010-10-31 15:32:19 +01:00
// Dereferencing pointer..
2013-10-12 15:19:15 +02:00
if ( prev - > str ( ) = = " * " & & ( Token : : Match ( prev - > previous ( ) , " return|throw|;|{|}|:|[|(|, " ) | | prev - > previous ( ) - > isOp ( ) ) & & ! Token : : Match ( prev - > tokAt ( - 2 ) , " sizeof|decltype|typeof " ) )
2010-10-31 15:32:19 +01:00
return true ;
2011-10-18 19:34:14 +02:00
// read/write member variable
2013-10-12 15:19:15 +02:00
if ( ! Token : : simpleMatch ( prev - > previous ( ) , " & ( " ) & & ! Token : : Match ( prev - > previous ( ) , " sizeof|decltype|typeof ( " ) & & prev - > str ( ) ! = " & " & & Token : : Match ( tok - > next ( ) , " . %var% " ) ) {
2011-10-18 19:34:14 +02:00
if ( tok - > strAt ( 3 ) ! = " ( " )
return true ;
unknown = true ;
return false ;
}
2010-10-31 15:32:19 +01:00
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( tok , " %var% [ " ) & & ( prev - > str ( ) ! = " & " | | Token : : Match ( tok - > next ( ) - > link ( ) - > next ( ) , " [.(] " ) ) )
2010-10-31 15:32:19 +01:00
return true ;
if ( Token : : Match ( tok , " %var% ( " ) )
return true ;
2010-10-31 15:46:08 +01:00
if ( Token : : Match ( tok , " %var% = %var% . " ) & &
tok - > varId ( ) > 0 & &
tok - > varId ( ) = = tok - > tokAt ( 2 ) - > varId ( ) )
return true ;
2012-01-25 15:16:22 +01:00
// std::string dereferences nullpointers
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev - > tokAt ( - 3 ) , " std :: string|wstring ( " ) & & tok - > strAt ( 1 ) = = " ) " )
2012-01-25 15:16:22 +01:00
return true ;
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev - > previous ( ) , " %var% ( " ) & & tok - > strAt ( 1 ) = = " ) " ) {
2013-02-01 19:10:14 +01:00
const Variable * var = tok - > tokAt ( - 2 ) - > variable ( ) ;
2012-11-10 19:53:20 +01:00
if ( var & & ! var - > isPointer ( ) & & ! var - > isArray ( ) & & Token : : Match ( var - > typeStartToken ( ) , " std :: string|wstring !!:: " ) )
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
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev , " <<|>> " ) ) {
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
2013-04-03 10:27:08 +02:00
const Token * tok2 = prev ; // 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 ;
2012-01-25 15:16:22 +01:00
if ( Token : : Match ( tok , " %var% ==|!= %var% " ) )
2013-02-01 19:10:14 +01:00
ovar = tok - > tokAt ( 2 ) - > variable ( ) ;
2013-04-03 10:27:08 +02:00
else if ( Token : : Match ( prev - > previous ( ) , " %var% ==|!= " ) )
2013-02-01 19:10:14 +01:00
ovar = tok - > tokAt ( - 2 ) - > variable ( ) ;
2013-04-03 10:27:08 +02:00
else if ( Token : : Match ( prev - > previous ( ) , " %var% =|+ " ) & & Token : : Match ( tok - > next ( ) , " )|]|,| ; | + " ))
2013-02-01 19:10:14 +01:00
ovar = tok - > tokAt ( - 2 ) - > variable ( ) ;
if ( ovar & & ! ovar - > isPointer ( ) & & ! ovar - > isArray ( ) & & Token : : Match ( ovar - > typeStartToken ( ) , " std :: string|wstring !!:: " ) )
return true ;
2012-01-25 15:16:22 +01:00
2011-12-12 07:35:53 +01:00
// Check if it's NOT a pointer dereference.
// This is most useful in inconclusive checking
if ( inconclusive ) {
// Not a dereference..
2012-01-21 19:55:32 +01:00
if ( Token : : Match ( tok , " %var% = " ) )
2011-12-12 07:35:53 +01:00
return false ;
2010-10-31 15:32:19 +01:00
2011-12-12 07:35:53 +01:00
// OK to delete a null
2014-03-14 16:26:37 +01:00
if ( Token : : simpleMatch ( prev , " delete " ) | | Token : : Match ( prev - > tokAt ( - 2 ) , " delete [ ] " ) )
2011-12-12 07:35:53 +01:00
return false ;
2011-11-13 22:35:13 +01:00
2011-12-12 07:35:53 +01:00
// OK to check if pointer is null
// OK to take address of pointer
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev , " !|& " ) )
2011-12-12 07:35:53 +01:00
return false ;
2011-12-11 08:48:55 +01:00
2013-01-21 19:38:59 +01:00
// OK to check pointer in "= p ? : "
2013-01-22 13:16:23 +01:00
if ( tok - > next ( ) - > str ( ) = = " ? " & &
2013-04-03 10:27:08 +02:00
( Token : : Match ( prev , " return|throw|;|{|}|:|[|(|, " ) | | prev - > isAssignmentOp ( ) ) )
2013-01-21 19:38:59 +01:00
return false ;
2011-12-12 07:35:53 +01:00
// OK to pass pointer to function
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev , " [(,] " ) & & Token : : Match ( tok - > next ( ) , " [,)] " ) & &
( prev - > str ( ) ! = " ( " | |
Token : : Match ( prev - > previous ( ) , " %var% ( " ) ) )
2011-12-15 17:01:39 +01:00
return false ;
// Compare pointer
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev , " (|&&|%oror%|==|!= " ) )
2012-01-21 19:55:32 +01:00
return false ;
if ( Token : : Match ( tok , " %var% &&|%oror%|==|!=|) " ) )
2011-12-15 17:01:39 +01:00
return false ;
// Taking address
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev , " return|= " ) & & tok - > strAt ( 1 ) = = " ; " )
2011-12-12 07:35:53 +01:00
return false ;
2011-12-11 08:48:55 +01:00
2012-04-30 12:36:41 +02:00
// (void)var
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev , " [{;}] " ) & & tok - > strAt ( 1 ) = = " ; " )
2012-04-30 12:36:41 +02:00
return false ;
2012-11-10 19:53:20 +01:00
// Shift pointer (e.g. to cout, but its no char* (see above))
2013-04-03 10:27:08 +02:00
if ( Token : : Match ( prev , " <<|>> " ) )
2012-11-10 19:53:20 +01:00
return false ;
2011-12-12 07:35:53 +01:00
// unknown if it's a dereference
2012-01-21 19:55:32 +01:00
unknown = true ;
2011-12-12 07:35:53 +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
// check if function can assign pointer
2013-03-29 17:55:29 +01:00
bool CheckNullPointer : : CanFunctionAssignPointer ( const Token * functiontoken , unsigned int varid , bool & unknown )
2011-12-25 17:01:45 +01:00
{
2012-01-21 19:55:32 +01:00
if ( Token : : Match ( functiontoken , " if|while|for|switch|sizeof|catch " ) )
2011-12-25 17:01:45 +01:00
return false ;
2012-09-16 16:41:15 +02:00
unsigned int argumentNumber = 0 ;
2011-12-25 17:01:45 +01:00
for ( const Token * arg = functiontoken - > tokAt ( 2 ) ; arg ; arg = arg - > nextArgument ( ) ) {
if ( Token : : Match ( arg , " %varid% [,)] " , varid ) ) {
2013-01-31 06:41:18 +01:00
const Function * func = functiontoken - > function ( ) ;
2012-09-06 18:33:15 +02:00
if ( ! func ) { // Unknown function
unknown = true ;
return true ; // assume that the function might assign the pointer
2011-12-25 17:01:45 +01:00
}
2012-09-06 18:33:15 +02:00
const Variable * var = func - > getArgumentVar ( argumentNumber ) ;
if ( ! var ) { // Unknown variable
unknown = true ;
return true ;
} else if ( var - > isReference ( ) ) // Assume every pointer passed by reference is assigned
return true ;
else
2011-12-25 17:01:45 +01:00
return false ;
}
2012-09-06 18:33:15 +02:00
+ + argumentNumber ;
2011-12-25 17:01:45 +01:00
}
// pointer is not passed
return false ;
}
2010-10-31 11:51:25 +01:00
void CheckNullPointer : : nullPointerLinkedList ( )
{
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..
2011-11-26 21:02:04 +01:00
if ( Token : : Match ( tok2 , " %var% . %var% " ) ) {
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 ( ) ) ;
2013-10-12 10:50:59 +02:00
// We don't support variables without a varid
if ( varid = = 0 )
continue ;
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..
2012-01-25 15:16:22 +01:00
for ( std : : list < Scope * > : : const_iterator j = i - > nestedList . begin ( ) ; j ! = i - > nestedList . end ( ) ; + + j ) {
Scope * scope = * j ;
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 ) {
nullPointerError ( tok1 , var - > name ( ) , scope - > classDef ) ;
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 ( )
{
2014-01-17 17:47:59 +01:00
for ( const Token * tok = _tokenizer - > tokens ( ) ; tok ; tok = tok - > next ( ) ) {
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
2014-01-17 17:47:59 +01:00
// Is pointer used as function parameter?
if ( Token : : Match ( tok - > previous ( ) , " [(,] %var% [,)] " ) ) {
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 ;
parseFunctionCall ( * ftok - > previous ( ) , varlist , & _settings - > library , 0 ) ;
if ( std : : find ( varlist . begin ( ) , varlist . end ( ) , tok ) ! = varlist . end ( ) ) {
2014-02-15 16:12:37 +01:00
if ( value - > condition = = nullptr )
2014-01-22 06:38:25 +01:00
nullPointerError ( tok , tok - > str ( ) ) ;
2014-01-17 17:47:59 +01:00
else if ( _settings - > isEnabled ( " warning " ) )
nullPointerError ( tok , tok - > str ( ) , value - > condition , 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 ) ) {
2014-01-22 20:16:31 +01:00
if ( _settings - > inconclusive & & unknown ) {
2014-02-15 16:12:37 +01:00
if ( value - > condition = = nullptr )
2014-01-22 20:16:31 +01:00
nullPointerError ( tok , tok - > str ( ) , true ) ;
else
nullPointerError ( tok , tok - > str ( ) , value - > condition , 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
2014-02-15 16:12:37 +01:00
if ( value - > condition = = nullptr )
2014-01-22 20:16:31 +01:00
nullPointerError ( tok , tok - > str ( ) , value - > inconclusive ) ;
2014-01-17 17:47:59 +01:00
else if ( _settings - > isEnabled ( " warning " ) )
nullPointerError ( tok , tok - > str ( ) , value - > condition , value - > inconclusive ) ;
2010-10-31 11:51:25 +01:00
}
}
void CheckNullPointer : : nullPointerByCheckAndDeRef ( )
{
2012-01-21 19:55:32 +01:00
const SymbolDatabase * symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
2011-01-06 13:18:49 +01:00
2012-01-21 19:55:32 +01:00
// Check if pointer is NULL and then dereference it..
for ( std : : list < Scope > : : const_iterator i = symbolDatabase - > scopeList . begin ( ) ; i ! = symbolDatabase - > scopeList . end ( ) ; + + i ) {
if ( i - > type ! = Scope : : eIf & & i - > type ! = Scope : : eElseIf & & i - > type ! = Scope : : eWhile )
continue ;
if ( ! i - > classDef | | i - > classDef - > isExpandedMacro ( ) )
continue ;
2011-01-17 19:23:00 +01:00
2012-01-26 16:50:59 +01:00
const Token * const tok = i - > type ! = Scope : : eElseIf ? i - > classDef - > next ( ) : i - > classDef - > tokAt ( 2 ) ;
2012-01-21 19:55:32 +01:00
// TODO: investigate false negatives:
// - handle "while"?
// - if there are logical operators
// - if (x) { } else { ... }
2010-11-04 21:22:29 +01:00
2012-01-21 19:55:32 +01:00
// If the if-body ends with a unknown macro then bailout
2012-06-21 19:00:53 +02:00
if ( Token : : Match ( i - > classEnd - > tokAt ( - 3 ) , " [;{}] %var% ; " ) & & i - > classEnd - > tokAt ( - 2 ) - > isUpperCaseName ( ) )
2012-01-21 19:55:32 +01:00
continue ;
2011-01-06 13:18:49 +01:00
2012-01-21 19:55:32 +01:00
// vartok : token for the variable
2014-02-16 11:47:52 +01:00
const Token * vartok = nullptr ;
const Token * checkConditionStart = nullptr ;
2012-02-11 16:15:38 +01:00
if ( Token : : Match ( tok , " ( ! %var% )|&& " ) ) {
2012-01-21 19:55:32 +01:00
vartok = tok - > tokAt ( 2 ) ;
2012-02-11 16:15:38 +01:00
checkConditionStart = vartok - > next ( ) ;
} else if ( Token : : Match ( tok , " ( %var% ) | & & " )) {
2012-01-26 16:50:59 +01:00
vartok = tok - > next ( ) ;
2012-02-11 16:15:38 +01:00
} else if ( Token : : Match ( tok , " ( ! ( %var% = " ) ) {
2012-01-26 16:50:59 +01:00
vartok = tok - > tokAt ( 3 ) ;
2012-09-07 12:36:40 +02:00
if ( Token : : simpleMatch ( tok - > linkAt ( 2 ) , " ) && " ) )
checkConditionStart = tok - > linkAt ( 2 ) ;
2012-02-11 16:15:38 +01:00
} else
2012-01-21 19:55:32 +01:00
continue ;
2011-03-28 18:48:27 +02:00
2012-01-21 19:55:32 +01:00
// Check if variable is a pointer
2013-02-06 06:39:58 +01:00
const Variable * var = vartok - > variable ( ) ;
2012-01-21 19:55:32 +01:00
if ( ! var | | ! var - > isPointer ( ) )
continue ;
2011-01-06 13:18:49 +01:00
2013-02-06 06:39:58 +01:00
// variable id for pointer
const unsigned int varid ( vartok - > varId ( ) ) ;
2012-08-05 11:07:38 +02:00
const Scope * declScope = & * i ;
while ( declScope - > nestedIn & & var - > scope ( ) ! = declScope & & declScope - > type ! = Scope : : eFunction )
declScope = declScope - > nestedIn ;
2012-01-21 19:55:32 +01:00
if ( Token : : Match ( vartok - > next ( ) , " && ( %varid% = " , varid ) )
continue ;
2011-01-09 20:16:16 +01:00
2012-02-11 16:15:38 +01:00
// Name and line of the pointer
const std : : string & pointerName = vartok - > str ( ) ;
// Check the condition (eg. ( !x && x->i )
if ( checkConditionStart ) {
const Token * const conditionEnd = tok - > link ( ) ;
for ( const Token * tok2 = checkConditionStart ; tok2 ! = conditionEnd ; tok2 = tok2 - > next ( ) ) {
// If we hit a || operator, abort
2012-08-05 11:07:38 +02:00
if ( tok2 - > str ( ) = = " || " )
2012-02-11 16:15:38 +01:00
break ;
2012-08-05 11:07:38 +02:00
2012-02-11 16:15:38 +01:00
// Pointer is used
2012-08-05 11:07:38 +02:00
bool unknown = _settings - > inconclusive ;
2013-02-01 19:10:14 +01:00
if ( tok2 - > varId ( ) = = varid & & ( isPointerDeRef ( tok2 , unknown ) | | unknown ) ) {
2012-08-05 11:07:38 +02:00
nullPointerError ( tok2 , pointerName , vartok , unknown ) ;
2012-02-11 16:15:38 +01:00
break ;
}
}
}
2012-01-21 19:55:32 +01:00
// start token = inside the if-body
const Token * tok1 = i - > classStart ;
2011-01-09 20:16:16 +01:00
2012-01-26 16:50:59 +01:00
if ( Token : : Match ( tok , " ( %var% )|&& " ) ) {
2012-01-21 19:55:32 +01:00
// start token = first token after the if/while body
tok1 = i - > classEnd - > next ( ) ;
if ( ! tok1 )
continue ;
}
2011-01-09 20:16:16 +01:00
2012-08-05 11:07:38 +02:00
int indentlevel = 0 ;
2011-10-29 14:55:52 +02:00
2012-01-21 19:55:32 +01:00
// Set to true if we would normally bail out the check.
bool inconclusive = false ;
2011-03-27 21:29:49 +02:00
2012-01-21 19:55:32 +01:00
// Count { and } for tok2
2012-08-05 11:07:38 +02:00
for ( const Token * tok2 = tok1 ; tok2 ! = declScope - > classEnd ; tok2 = tok2 - > next ( ) ) {
2012-01-21 19:55:32 +01:00
if ( tok2 - > str ( ) = = " { " )
+ + indentlevel ;
else if ( tok2 - > str ( ) = = " } " ) {
2012-08-05 11:07:38 +02:00
if ( indentlevel = = 0 ) {
if ( _settings - > inconclusive )
inconclusive = true ;
else
break ;
}
2012-01-21 19:55:32 +01:00
- - indentlevel ;
2011-12-28 08:17:58 +01:00
2012-01-21 19:55:32 +01:00
// calling exit function?
bool unknown = false ;
if ( _tokenizer - > IsScopeNoReturn ( tok2 , & unknown ) ) {
if ( _settings - > inconclusive & & unknown )
inconclusive = true ;
else
break ;
2010-10-31 11:51:25 +01:00
}
2012-08-05 11:07:38 +02:00
if ( indentlevel < = 0 ) {
2012-01-21 19:55:32 +01:00
// skip all "else" blocks because they are not executed in this execution path
2012-08-05 11:07:38 +02:00
while ( Token : : simpleMatch ( tok2 , " } else if ( " ) )
tok2 = tok2 - > linkAt ( 3 ) - > linkAt ( 1 ) ;
if ( Token : : simpleMatch ( tok2 , " } else { " ) )
2012-01-21 19:55:32 +01:00
tok2 = tok2 - > linkAt ( 2 ) ;
2010-10-31 11:51:25 +01:00
}
2012-01-21 19:55:32 +01:00
}
2010-10-31 11:51:25 +01:00
2012-01-25 15:16:22 +01:00
if ( tok2 - > str ( ) = = " return " | | tok2 - > str ( ) = = " throw " ) {
bool unknown = _settings - > inconclusive ;
for ( ; tok2 & & tok2 - > str ( ) ! = " ; " ; tok2 = tok2 - > next ( ) ) {
if ( tok2 - > varId ( ) = = varid ) {
2013-02-01 19:10:14 +01:00
if ( CheckNullPointer : : isPointerDeRef ( tok2 , unknown ) )
2012-08-05 10:38:48 +02:00
nullPointerError ( tok2 , pointerName , vartok , inconclusive ) ;
2012-01-25 15:16:22 +01:00
else if ( unknown )
2012-08-05 10:38:48 +02:00
nullPointerError ( tok2 , pointerName , vartok , true ) ;
2012-12-25 14:23:50 +01:00
if ( Token : : Match ( tok2 , " %var% ? " ) )
break ;
2012-01-25 15:16:22 +01:00
}
}
2012-01-21 19:55:32 +01:00
break ;
}
2011-12-17 12:44:11 +01:00
2012-09-18 19:16:28 +02:00
// Bailout for "if".
if ( tok2 - > str ( ) = = " if " ) {
if ( _settings - > inconclusive )
inconclusive = true ;
else
break ;
}
2012-08-05 11:07:38 +02:00
if ( Token : : Match ( tok2 , " goto|continue|break|switch|for " ) )
2012-01-25 15:16:22 +01:00
break ;
2012-01-21 19:55:32 +01:00
// parameters to sizeof are not dereferenced
2013-10-12 15:19:15 +02:00
if ( Token : : Match ( tok2 , " decltype|sizeof|typeof " ) ) {
2012-01-21 19:55:32 +01:00
if ( tok2 - > strAt ( 1 ) ! = " ( " )
2012-01-26 16:50:59 +01:00
tok2 = tok2 - > next ( ) ;
else
tok2 = tok2 - > next ( ) - > link ( ) ;
2012-01-21 19:55:32 +01:00
continue ;
}
2011-07-31 17:19:23 +02:00
2012-01-21 19:55:32 +01:00
// function call, check if pointer is dereferenced
2012-08-05 11:07:38 +02:00
if ( Token : : Match ( tok2 , " %var% ( " ) & & ! Token : : Match ( tok2 , " if|while " ) ) {
2012-01-21 19:55:32 +01:00
std : : list < const Token * > vars ;
2013-07-15 18:55:40 +02:00
parseFunctionCall ( * tok2 , vars , & _settings - > library , 0 ) ;
2012-01-21 19:55:32 +01:00
for ( std : : list < const Token * > : : const_iterator it = vars . begin ( ) ; it ! = vars . end ( ) ; + + it ) {
if ( Token : : Match ( * it , " %varid% [,)] " , varid ) ) {
2012-08-05 10:38:48 +02:00
nullPointerError ( * it , pointerName , vartok , inconclusive ) ;
2012-01-21 19:55:32 +01:00
break ;
2011-10-29 10:35:31 +02:00
}
2012-01-21 19:55:32 +01:00
}
}
2011-07-31 17:32:25 +02:00
2012-01-21 19:55:32 +01:00
// calling unknown function (abort/init)..
2012-01-26 16:50:59 +01:00
else if ( Token : : simpleMatch ( tok2 , " ) ; " ) &&
( Token : : Match ( tok2 - > link ( ) - > tokAt ( - 2 ) , " [;{}.] %var% ( " ) | |
Token : : Match ( tok2 - > link ( ) - > tokAt ( - 5 ) , " [;{}] ( * %var% ) ( " ) ) ) {
2012-01-21 19:55:32 +01:00
// noreturn function?
bool unknown = false ;
if ( _tokenizer - > IsScopeNoReturn ( tok2 - > tokAt ( 2 ) , & unknown ) ) {
if ( ! unknown | | ! _settings - > inconclusive ) {
2011-07-31 17:32:25 +02:00
break ;
2012-01-21 19:55:32 +01:00
}
2012-01-26 16:50:59 +01:00
inconclusive = _settings - > inconclusive ;
2010-10-31 11:51:25 +01:00
}
2012-01-21 19:55:32 +01:00
// init function (global variables)
if ( ! var | | ! ( var - > isLocal ( ) | | var - > isArgument ( ) ) )
break ;
}
2010-10-31 15:32:19 +01:00
2012-01-21 19:55:32 +01:00
if ( tok2 - > varId ( ) = = varid ) {
// unknown: this is set to true by isPointerDeRef if
// the function fails to determine if there
// is a dereference or not
bool unknown = _settings - > inconclusive ;
2010-10-31 11:51:25 +01:00
2012-01-21 19:55:32 +01:00
if ( Token : : Match ( tok2 - > previous ( ) , " [;{}=] %var% = 0 ; " ) )
;
2010-10-31 11:51:25 +01:00
2013-02-01 19:10:14 +01:00
else if ( CheckNullPointer : : isPointerDeRef ( tok2 , unknown ) )
2012-08-05 10:38:48 +02:00
nullPointerError ( tok2 , pointerName , vartok , inconclusive ) ;
2011-10-29 15:48:54 +02:00
2012-01-21 19:55:32 +01:00
else if ( unknown & & _settings - > inconclusive )
2012-08-05 10:38:48 +02:00
nullPointerError ( tok2 , pointerName , vartok , true ) ;
2012-01-21 19:55:32 +01:00
else
break ;
2010-10-31 11:51:25 +01:00
}
}
}
}
void CheckNullPointer : : nullPointer ( )
{
nullPointerLinkedList ( ) ;
2013-02-02 15:46:29 +01:00
if ( _settings - > isEnabled ( " warning " ) ) {
nullPointerByDeRefAndChec ( ) ;
nullPointerByCheckAndDeRef ( ) ;
2013-04-26 09:19:39 +02:00
nullPointerDefaultArgument ( ) ;
2013-02-02 15:46:29 +01:00
}
2010-10-31 11:51:25 +01:00
}
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 ( ) ;
2010-10-31 11:51:25 +01:00
2014-01-30 05:26:48 +01:00
// THIS ARRAY MUST BE ORDERED ALPHABETICALLY
static const char * const stl_stream [ ] = {
" fstream " , " ifstream " , " iostream " , " istream " ,
" istringstream " , " stringstream " , " wistringstream " , " wstringstream "
} ;
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 ] ;
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 ) ;
2012-02-11 12:26:48 +01:00
else if ( Token : : Match ( tok - > previous ( ) , " !!. %var% ( " ) & & ( tok - > previous ( ) - > str ( ) ! = " :: " | | tok - > strAt ( - 2 ) = = " std " ) ) {
2012-04-02 12:12:02 +02:00
if ( Token : : simpleMatch ( tok - > tokAt ( 2 ) , " 0 ) " ) & & tok - > varId ( ) ) { // constructor call
2013-02-06 06:39:58 +01:00
const Variable * var = tok - > variable ( ) ;
2012-11-06 19:54:52 +01:00
if ( var & & ! var - > isPointer ( ) & & ! var - > isArray ( ) & & Token : : Match ( var - > typeStartToken ( ) , " std :: string|wstring !!:: " ) )
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 ;
2013-07-15 18:55:40 +02:00
parseFunctionCall ( * tok , var , & _settings - > library , 0 ) ;
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 ) {
2013-06-07 06:37:53 +02:00
if ( Token : : Match ( * it , " 0|NULL [,)] " ) ) {
2012-04-02 12:12:02 +02:00
nullPointerError ( * it ) ;
}
2010-12-26 19:29:58 +01:00
}
}
2012-11-06 19:54:52 +01:00
} else if ( Token : : Match ( tok , " std :: string|wstring ( 0 ) " ) )
2012-01-25 15:16:22 +01:00
nullPointerError ( tok ) ;
2012-03-16 17:24:03 +01:00
else if ( Token : : simpleMatch ( tok - > previous ( ) , " >> 0 " ) ) { // Only checking input stream operations is safe here, because otherwise 0 can be an integer as well
const Token * tok2 = tok - > previous ( ) ; // Find start of statement
for ( ; tok2 ; tok2 = tok2 - > previous ( ) ) {
if ( Token : : Match ( tok2 - > previous ( ) , " ;|{|}|: " ) )
break ;
}
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 ( ) ;
2014-01-30 05:26:48 +01:00
if ( var & & var - > isStlType ( stl_stream ) )
2012-03-16 17:24:03 +01:00
nullPointerError ( tok ) ;
}
}
2014-02-16 11:47:52 +01:00
const Variable * ovar = nullptr ;
2013-08-24 21:50:19 +02:00
if ( Token : : Match ( tok , " 0 ==|!= %var% !!. " ) )
2013-02-06 06:39:58 +01:00
ovar = tok - > tokAt ( 2 ) - > variable ( ) ;
2012-04-02 12:12:02 +02:00
else if ( Token : : Match ( tok , " %var% ==|!= 0 " ) )
2013-02-06 06:39:58 +01:00
ovar = tok - > variable ( ) ;
2012-11-30 07:08:16 +01:00
else if ( Token : : Match ( tok , " %var% =|+ 0 ) | ] | , | ; | + " ))
2013-02-06 06:39:58 +01:00
ovar = tok - > variable ( ) ;
if ( ovar & & ! ovar - > isPointer ( ) & & ! ovar - > isArray ( ) & & Token : : Match ( ovar - > typeStartToken ( ) , " std :: string|wstring !!:: " ) )
nullPointerError ( tok ) ;
2010-10-31 11:51:25 +01:00
}
}
}
2013-04-25 09:25:56 +02:00
/**
* @ brief If tok is a function call that passes in a pointer such that
* the pointer may be modified , this function will remove that
* pointer from pointerArgs .
*/
void CheckNullPointer : : removeAssignedVarFromSet ( const Token * tok , std : : set < unsigned int > & pointerArgs )
{
// If a pointer's address is passed into a function, stop considering it
if ( Token : : Match ( tok - > previous ( ) , " [;{}] %var% ( " ) ) {
2013-04-27 05:06:17 +02:00
// Common functions that are known NOT to modify their pointer argument
const char safeFunctions [ ] = " printf|sprintf|fprintf|vprintf " ;
2013-04-25 09:25:56 +02:00
const Token * endParen = tok - > next ( ) - > link ( ) ;
for ( const Token * tok2 = tok - > next ( ) ; tok2 ! = endParen ; tok2 = tok2 - > next ( ) ) {
if ( tok2 - > isName ( ) & & tok2 - > varId ( ) > 0 & & ! Token : : Match ( tok , safeFunctions ) ) {
pointerArgs . erase ( tok2 - > varId ( ) ) ;
}
}
}
}
2010-10-31 11:51:25 +01:00
2012-11-21 08:56:17 +01:00
/**
* @ brief Does one part of the check for nullPointer ( ) .
* - # default argument that sets a pointer to 0
* - # dereference pointer
*/
void CheckNullPointer : : nullPointerDefaultArgument ( )
{
const SymbolDatabase * symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
2012-11-25 12:40:14 +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 ] ;
if ( scope - > function = = 0 | | ! scope - > function - > hasBody ) // We only look for functions with a body
2012-11-21 08:56:17 +01:00
continue ;
// Scan the argument list for default arguments that are pointers and
// which default to a NULL pointer if no argument is specified.
std : : set < unsigned int > pointerArgs ;
2012-11-25 12:40:14 +01:00
for ( const Token * tok = scope - > function - > arg ; tok ! = scope - > function - > arg - > link ( ) ; tok = tok - > next ( ) ) {
2012-11-21 08:56:17 +01:00
if ( Token : : Match ( tok , " %var% = 0 ,|) " ) & & tok - > varId ( ) ! = 0 ) {
2013-02-06 06:39:58 +01:00
const Variable * var = tok - > variable ( ) ;
2012-11-21 08:56:17 +01:00
if ( var & & var - > isPointer ( ) )
pointerArgs . insert ( tok - > varId ( ) ) ;
}
}
// Report an error if any of the default-NULL arguments are dereferenced
if ( ! pointerArgs . empty ( ) ) {
2012-11-25 12:40:14 +01:00
for ( const Token * tok = scope - > classStart ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
2013-04-25 09:25:56 +02:00
2012-11-21 08:56:17 +01:00
// If we encounter a possible NULL-pointer check, skip over its body
2012-12-01 03:17:55 +01:00
if ( Token : : simpleMatch ( tok , " if ( " ) ) {
2012-11-21 08:56:17 +01:00
bool dependsOnPointer = false ;
const Token * endOfCondition = tok - > next ( ) - > link ( ) ;
2013-02-28 08:45:21 +01:00
if ( ! endOfCondition )
continue ;
const Token * startOfIfBlock =
2014-02-15 16:12:37 +01:00
Token : : simpleMatch ( endOfCondition , " ) { " ) ? endOfCondition - > next ( ) : nullptr ;
2013-02-28 08:45:21 +01:00
if ( ! startOfIfBlock )
continue ;
// If this if() statement may return, it may be a null
// pointer check for the pointers referenced in its condition
const Token * endOfIf = startOfIfBlock - > link ( ) ;
bool isExitOrReturn =
2014-02-15 16:12:37 +01:00
Token : : findmatch ( startOfIfBlock , " exit|return|throw " , endOfIf ) ! = nullptr ;
2013-02-28 08:45:21 +01:00
2013-04-25 09:25:56 +02:00
if ( Token : : Match ( tok , " if ( %var% == 0 ) " ) ) {
const unsigned int var = tok - > tokAt ( 2 ) - > varId ( ) ;
if ( var > 0 & & pointerArgs . count ( var ) > 0 ) {
2013-02-28 08:45:21 +01:00
if ( isExitOrReturn )
2013-04-25 09:25:56 +02:00
pointerArgs . erase ( var ) ;
2013-02-28 08:45:21 +01:00
else
dependsOnPointer = true ;
2012-11-21 08:56:17 +01:00
}
2013-04-25 09:25:56 +02:00
} else {
for ( const Token * tok2 = tok - > next ( ) ; tok2 ! = endOfCondition ; tok2 = tok2 - > next ( ) ) {
if ( tok2 - > isName ( ) & & tok2 - > varId ( ) > 0 & &
pointerArgs . count ( tok2 - > varId ( ) ) > 0 ) {
// If the if() depends on a pointer and may return, stop
// considering that pointer because it may be a NULL-pointer
// check that returns if the pointer is NULL.
if ( isExitOrReturn )
pointerArgs . erase ( tok2 - > varId ( ) ) ;
else
dependsOnPointer = true ;
}
}
2012-11-21 08:56:17 +01:00
}
2013-04-25 09:25:56 +02:00
2013-02-28 08:45:21 +01:00
if ( dependsOnPointer & & endOfIf ) {
2013-01-10 08:22:54 +01:00
for ( ; tok ! = endOfIf ; tok = tok - > next ( ) ) {
// If a pointer is assigned a new value, stop considering it.
if ( Token : : Match ( tok , " %var% = " ) )
pointerArgs . erase ( tok - > varId ( ) ) ;
2013-04-25 09:25:56 +02:00
else
removeAssignedVarFromSet ( tok , pointerArgs ) ;
2013-01-10 08:22:54 +01:00
}
2012-11-21 08:56:17 +01:00
continue ;
}
}
2013-04-25 09:25:56 +02:00
// If there is a noreturn function (e.g. exit()), stop considering the rest of
// this function.
bool unknown = false ;
if ( Token : : Match ( tok , " return|throw|exit " ) | |
( _tokenizer - > IsScopeNoReturn ( tok , & unknown ) & & ! unknown ) )
break ;
removeAssignedVarFromSet ( tok , pointerArgs ) ;
2012-11-21 08:56:17 +01:00
if ( tok - > varId ( ) = = 0 | | pointerArgs . count ( tok - > varId ( ) ) = = 0 )
continue ;
// If a pointer is assigned a new value, stop considering it.
2013-01-10 08:22:54 +01:00
if ( Token : : Match ( tok , " %var% = " ) )
2012-11-21 08:56:17 +01:00
pointerArgs . erase ( tok - > varId ( ) ) ;
2013-02-28 08:45:21 +01:00
// If a pointer dereference is preceded by an && or ||,
// they serve as a sequence point so the dereference
// may not be executed.
2013-04-25 09:25:56 +02:00
if ( isPointerDeRef ( tok , unknown ) & & ! unknown & &
2013-02-28 08:45:21 +01:00
tok - > strAt ( - 1 ) ! = " && " & & tok - > strAt ( - 1 ) ! = " || " & &
tok - > strAt ( - 2 ) ! = " && " & & tok - > strAt ( - 2 ) ! = " || " )
2012-11-21 08:56:17 +01:00
nullPointerDefaultArgError ( tok , tok - > str ( ) ) ;
}
}
}
}
2010-10-31 11:51:25 +01:00
void CheckNullPointer : : nullPointerError ( const Token * tok )
{
reportError ( tok , Severity : : error , " nullPointer " , " Null pointer dereference " ) ;
}
2014-01-22 20:16:31 +01:00
void CheckNullPointer : : nullPointerError ( const Token * tok , const std : : string & varname , bool inconclusive )
2010-10-31 11:51:25 +01:00
{
2014-01-22 20:16:31 +01:00
reportError ( tok , Severity : : error , " nullPointer " , " Possible null pointer dereference: " + varname , inconclusive ) ;
2010-10-31 11:51:25 +01:00
}
2012-08-05 10:38:48 +02:00
void CheckNullPointer : : nullPointerError ( const Token * tok , const std : : string & varname , const Token * nullCheck , bool inconclusive )
2010-10-31 11:51:25 +01:00
{
2012-08-05 10:38:48 +02:00
std : : list < const Token * > callstack ;
callstack . push_back ( tok ) ;
callstack . push_back ( nullCheck ) ;
const std : : string errmsg ( " Possible null pointer dereference: " + varname + " - otherwise it is redundant to check it against null. " ) ;
2013-04-07 03:41:02 +02:00
reportError ( callstack , Severity : : warning , " nullPointer " , errmsg , inconclusive ) ;
2010-10-31 11:51:25 +01:00
}
2012-11-21 08:56:17 +01:00
void CheckNullPointer : : nullPointerDefaultArgError ( const Token * tok , const std : : string & varname )
{
reportError ( tok , Severity : : warning , " nullPointer " , " Possible null pointer dereference if the default parameter value is used: " + varname ) ;
}
2014-02-09 20:46:49 +01:00