2014-09-11 18:10:19 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2016-01-01 14:34:45 +01:00
* Copyright ( C ) 2007 - 2016 Cppcheck team .
2014-09-11 18:10:19 +02: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 "checktype.h"
# include "mathlib.h"
# include "symboldatabase.h"
# include <stack>
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace {
CheckType instance ;
}
//---------------------------------------------------------------------------
// Checking for shift by too many bits
//---------------------------------------------------------------------------
void CheckType : : checkTooBigBitwiseShift ( )
{
2015-04-07 07:07:08 +02:00
const bool printWarnings = _settings - > isEnabled ( " warning " ) ;
2015-04-10 14:18:52 +02:00
const bool printInconclusive = _settings - > inconclusive ;
2015-04-07 07:07:08 +02:00
2014-09-11 18:10:19 +02:00
// unknown sizeof(int) => can't run this checker
if ( _settings - > platformType = = Settings : : Unspecified )
return ;
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 ( ) ) {
2015-12-31 12:05:23 +01:00
if ( ! Token : : Match ( tok , " <<|>>|<<=|>>= " ) )
2014-09-11 18:10:19 +02:00
continue ;
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
// get number of bits of lhs
2015-12-31 01:51:21 +01:00
const ValueType * lhstype = tok - > astOperand1 ( ) - > valueType ( ) ;
if ( ! lhstype | | ! lhstype - > isIntegral ( ) | | lhstype - > pointer > = 1U )
2014-09-11 18:10:19 +02:00
continue ;
int lhsbits = 0 ;
2015-12-31 01:51:21 +01:00
if ( lhstype - > type < = ValueType : : Type : : INT )
lhsbits = 8 * _settings - > sizeof_int ;
else if ( lhstype - > type = = ValueType : : Type : : LONG )
lhsbits = 8 * _settings - > sizeof_long ;
else if ( lhstype - > type = = ValueType : : Type : : LONGLONG )
lhsbits = 8 * _settings - > sizeof_long_long ;
else
2014-09-11 18:10:19 +02:00
continue ;
// Get biggest rhs value. preferably a value which doesn't have 'condition'.
const ValueFlow : : Value * value = tok - > astOperand2 ( ) - > getValueGE ( lhsbits , _settings ) ;
if ( ! value )
continue ;
2015-04-07 07:07:08 +02:00
if ( value - > condition & & ! printWarnings )
2014-09-11 18:10:19 +02:00
continue ;
2015-04-10 14:18:52 +02:00
if ( value - > inconclusive & & ! printInconclusive )
2014-09-11 18:10:19 +02:00
continue ;
tooBigBitwiseShiftError ( tok , lhsbits , * value ) ;
}
}
}
void CheckType : : tooBigBitwiseShiftError ( const Token * tok , int lhsbits , const ValueFlow : : Value & rhsbits )
{
std : : list < const Token * > callstack ;
callstack . push_back ( tok ) ;
if ( rhsbits . condition )
callstack . push_back ( rhsbits . condition ) ;
std : : ostringstream errmsg ;
errmsg < < " Shifting " < < lhsbits < < " -bit value by " < < rhsbits . intvalue < < " bits is undefined behaviour " ;
if ( rhsbits . condition )
errmsg < < " . See condition at line " < < rhsbits . condition - > linenr ( ) < < " . " ;
2015-04-25 17:48:11 +02:00
reportError ( callstack , rhsbits . condition ? Severity : : warning : Severity : : error , " shiftTooManyBits " , errmsg . str ( ) , 0U , rhsbits . inconclusive ) ;
2014-09-11 18:10:19 +02:00
}
//---------------------------------------------------------------------------
// Checking for integer overflow
//---------------------------------------------------------------------------
void CheckType : : checkIntegerOverflow ( )
{
// unknown sizeof(int) => can't run this checker
if ( _settings - > platformType = = Settings : : Unspecified )
return ;
// max int value according to platform settings.
const MathLib : : bigint maxint = ( 1LL < < ( 8 * _settings - > sizeof_int - 1 ) ) - 1 ;
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 - > isArithmeticalOp ( ) )
continue ;
// is there a overflow result value
const ValueFlow : : Value * value = tok - > getValueGE ( maxint + 1 , _settings ) ;
if ( ! value )
value = tok - > getValueLE ( - maxint - 2 , _settings ) ;
if ( ! value )
continue ;
2015-12-31 12:05:23 +01:00
// is result signed integer?
const ValueType * vt = tok - > valueType ( ) ;
if ( vt & & vt - > type = = ValueType : : Type : : INT & & vt - > sign = = ValueType : : Sign : : SIGNED )
integerOverflowError ( tok , * value ) ;
2014-09-11 18:10:19 +02:00
}
}
}
void CheckType : : integerOverflowError ( const Token * tok , const ValueFlow : : Value & value )
{
const std : : string expr ( tok ? tok - > expressionString ( ) : " " ) ;
2015-12-31 12:05:23 +01:00
std : : string msg ;
if ( value . condition )
msg = ValueFlow : : eitherTheConditionIsRedundant ( value . condition ) +
" or there is signed integer overflow for expression ' " + expr + " '. " ;
else
msg = " Signed integer overflow for expression ' " + expr + " '. " ;
2014-09-11 18:10:19 +02:00
reportError ( tok ,
value . condition ? Severity : : warning : Severity : : error ,
" integerOverflow " ,
2015-12-31 12:05:23 +01:00
msg ,
2015-04-25 17:48:11 +02:00
0U ,
2014-09-11 18:10:19 +02:00
value . inconclusive ) ;
}
//---------------------------------------------------------------------------
// Checking for sign conversion when operand can be negative
//---------------------------------------------------------------------------
void CheckType : : checkSignConversion ( )
{
if ( ! _settings - > isEnabled ( " warning " ) )
return ;
2014-09-11 19:45:52 +02:00
2014-09-11 18:10:19 +02:00
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 ( ) ) {
2014-09-12 18:58:31 +02:00
if ( ! tok - > isArithmeticalOp ( ) | | Token : : Match ( tok , " +|- " ) )
2014-09-11 18:10:19 +02:00
continue ;
2015-12-31 12:05:23 +01:00
// Is result unsigned?
if ( ! ( tok - > valueType ( ) & & tok - > valueType ( ) - > sign = = ValueType : : Sign : : UNSIGNED ) )
2014-09-11 18:10:19 +02:00
continue ;
2014-09-11 19:45:52 +02:00
2015-12-31 12:05:23 +01:00
// Check if an operand can be negative..
2014-09-11 18:10:19 +02:00
std : : stack < const Token * > tokens ;
2014-09-14 10:29:58 +02:00
tokens . push ( tok - > astOperand1 ( ) ) ;
tokens . push ( tok - > astOperand2 ( ) ) ;
2014-09-11 18:10:19 +02:00
while ( ! tokens . empty ( ) ) {
const Token * tok1 = tokens . top ( ) ;
tokens . pop ( ) ;
if ( ! tok1 )
continue ;
2015-12-31 12:05:23 +01:00
if ( ! tok1 - > getValueLE ( - 1 , _settings ) )
continue ;
if ( tok1 - > valueType ( ) & & tok1 - > valueType ( ) - > sign ! = ValueType : : Sign : : UNSIGNED )
2016-01-02 22:56:15 +01:00
signConversionError ( tok1 , tok1 - > isNumber ( ) ) ;
2014-09-11 18:10:19 +02:00
}
}
}
}
2016-01-02 22:56:15 +01:00
void CheckType : : signConversionError ( const Token * tok , const bool constvalue )
2014-09-11 18:10:19 +02:00
{
const std : : string varname ( tok ? tok - > str ( ) : " var " ) ;
2014-09-11 19:45:52 +02:00
2014-09-11 18:10:19 +02:00
reportError ( tok ,
Severity : : warning ,
" signConversion " ,
2016-01-02 22:56:15 +01:00
( constvalue ) ?
" Suspicious code: sign conversion of " + varname + " in calculation because ' " + varname + " ' has a negative value " :
2014-09-11 18:10:19 +02:00
" Suspicious code: sign conversion of " + varname + " in calculation, even though " + varname + " can have a negative value " ) ;
}
2015-05-25 10:02:17 +02:00
//---------------------------------------------------------------------------
// Checking for long cast of int result const long x = var1 * var2;
//---------------------------------------------------------------------------
void CheckType : : checkLongCast ( )
{
if ( ! _settings - > isEnabled ( " style " ) )
return ;
// Assignments..
for ( const Token * tok = _tokenizer - > tokens ( ) ; tok ; tok = tok - > next ( ) ) {
2015-12-31 12:05:23 +01:00
if ( tok - > str ( ) ! = " = " | | ! Token : : Match ( tok - > astOperand2 ( ) , " *|<< " ) )
2015-05-25 18:19:40 +02:00
continue ;
2015-12-31 12:05:23 +01:00
const ValueType * lhstype = tok - > astOperand1 ( ) ? tok - > astOperand1 ( ) - > valueType ( ) : nullptr ;
const ValueType * rhstype = tok - > astOperand2 ( ) - > valueType ( ) ;
if ( ! lhstype | | ! rhstype )
2015-05-25 10:02:17 +02:00
continue ;
2015-12-31 12:05:23 +01:00
// assign int result to long/longlong const nonpointer?
if ( rhstype - > type = = ValueType : : Type : : INT & &
rhstype - > pointer = = 0U & &
rhstype - > originalTypeName . empty ( ) & &
( lhstype - > type = = ValueType : : Type : : LONG | | lhstype - > type = = ValueType : : Type : : LONGLONG ) & &
lhstype - > pointer = = 0U & &
lhstype - > constness = = 1U & &
lhstype - > originalTypeName . empty ( ) )
2015-05-25 10:02:17 +02:00
longCastAssignError ( tok ) ;
}
// Return..
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 ] ;
// function must return long data
const Token * def = scope - > classDef ;
bool islong = false ;
while ( Token : : Match ( def , " %type%|:: " ) ) {
2015-05-25 18:19:40 +02:00
if ( def - > str ( ) = = " long " & & def - > originalName ( ) . empty ( ) ) {
2015-05-25 10:02:17 +02:00
islong = true ;
break ;
}
def = def - > previous ( ) ;
}
if ( ! islong )
continue ;
2015-12-31 12:05:23 +01:00
// return statements
2015-05-25 10:02:17 +02:00
const Token * ret = nullptr ;
for ( const Token * tok = scope - > classStart ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) = = " return " ) {
2015-12-31 12:05:23 +01:00
if ( Token : : Match ( tok - > astOperand1 ( ) , " <<|* " ) ) {
const ValueType * type = tok - > astOperand1 ( ) - > valueType ( ) ;
2015-12-31 14:07:38 +01:00
if ( type & & type - > type = = ValueType : : Type : : INT & & type - > pointer = = 0U & & type - > originalTypeName . empty ( ) )
2015-12-31 12:05:23 +01:00
ret = tok ;
}
// All return statements must have problem otherwise no warning
if ( ret ! = tok ) {
ret = nullptr ;
2015-05-25 10:02:17 +02:00
break ;
}
}
}
2015-12-31 12:05:23 +01:00
if ( ret )
2015-05-25 10:02:17 +02:00
longCastReturnError ( ret ) ;
}
}
void CheckType : : longCastAssignError ( const Token * tok )
{
reportError ( tok ,
Severity : : style ,
" truncLongCastAssignment " ,
2015-06-01 21:22:47 +02:00
" int result is assigned to long variable. If the variable is long to avoid loss of information, then you have loss of information. \n "
" int result is assigned to long variable. If the variable is long to avoid loss of information, then there is loss of information. To avoid loss of information you must cast a calculation operand to long, for example 'l = a * b;' => 'l = (long)a * b;'. " ) ;
2015-05-25 10:02:17 +02:00
}
void CheckType : : longCastReturnError ( const Token * tok )
{
reportError ( tok ,
Severity : : style ,
" truncLongCastReturn " ,
2015-06-01 21:22:47 +02:00
" int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information. \n "
" int result is returned as long value. If the return value is long to avoid loss of information, then there is loss of information. To avoid loss of information you must cast a calculation operand to long, for example 'return a*b;' => 'return (long)a*b'. " ) ;
2015-05-25 10:02:17 +02:00
}