2014-08-29 17:06:46 +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-08-29 17:06:46 +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/>.
*/
//---------------------------------------------------------------------------
// Check for condition mismatches
//---------------------------------------------------------------------------
# include "checkcondition.h"
2015-08-02 21:57:32 +02:00
# include "astutils.h"
2014-08-29 17:06:46 +02:00
# include "checkother.h"
# include "symboldatabase.h"
2015-06-18 19:07:51 +02:00
# include <limits>
2015-07-30 13:30:16 +02:00
# include <stack>
2015-06-18 19:07:51 +02:00
2016-02-27 16:03:50 +01:00
// CWE ids used
2016-08-24 12:43:45 +02:00
static const struct CWE CWE398 ( 398U ) ; // Indicator of Poor Code Quality
static const struct CWE CWE570 ( 570U ) ; // Expression is Always False
static const struct CWE CWE571 ( 571U ) ; // Expression is Always True
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
2014-08-29 17:06:46 +02:00
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace {
CheckCondition instance ;
}
void CheckCondition : : assignIf ( )
{
if ( ! _settings - > isEnabled ( " style " ) )
return ;
for ( const Token * tok = _tokenizer - > tokens ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) ! = " = " )
continue ;
if ( Token : : Match ( tok - > tokAt ( - 2 ) , " [;{}] %var% = " ) ) {
const Variable * var = tok - > previous ( ) - > variable ( ) ;
if ( var = = 0 )
continue ;
char bitop = ' \0 ' ;
MathLib : : bigint num = 0 ;
if ( Token : : Match ( tok - > next ( ) , " %num% [&|] " ) ) {
bitop = tok - > strAt ( 2 ) . at ( 0 ) ;
num = MathLib : : toLongNumber ( tok - > next ( ) - > str ( ) ) ;
} else {
const Token * endToken = Token : : findsimplematch ( tok , " ; " ) ;
// Casting address
if ( endToken & & Token : : Match ( endToken - > tokAt ( - 4 ) , " * ) & %any% ; " ) )
endToken = nullptr ;
if ( endToken & & Token : : Match ( endToken - > tokAt ( - 2 ) , " [&|] %num% ; " ) ) {
bitop = endToken - > strAt ( - 2 ) . at ( 0 ) ;
num = MathLib : : toLongNumber ( endToken - > previous ( ) - > str ( ) ) ;
}
}
if ( bitop = = ' \0 ' )
continue ;
if ( num < 0 & & bitop = = ' | ' )
continue ;
assignIfParseScope ( tok , tok - > tokAt ( 4 ) , var - > declarationId ( ) , var - > isLocal ( ) , bitop , num ) ;
}
}
}
2015-07-23 12:06:33 +02:00
static bool isParameterChanged ( const Token * partok )
{
bool addressOf = Token : : Match ( partok , " [(,] & " ) ;
unsigned int argumentNumber = 0 ;
const Token * ftok ;
for ( ftok = partok ; ftok & & ftok - > str ( ) ! = " ( " ; ftok = ftok - > previous ( ) ) {
if ( ftok - > str ( ) = = " ) " )
ftok = ftok - > link ( ) ;
else if ( argumentNumber = = 0U & & ftok - > str ( ) = = " & " )
addressOf = true ;
else if ( ftok - > str ( ) = = " , " )
argumentNumber + + ;
}
ftok = ftok ? ftok - > previous ( ) : nullptr ;
if ( ! ( ftok & & ftok - > function ( ) ) )
return true ;
if ( ftok - > function ( ) - > isConst ( ) )
return false ;
const Variable * par = ftok - > function ( ) - > getArgumentVar ( argumentNumber ) ;
if ( ! par )
return true ;
if ( par - > isConst ( ) )
return false ;
if ( addressOf | | par - > isReference ( ) | | par - > isPointer ( ) )
return true ;
return false ;
}
2014-08-29 17:06:46 +02:00
/** parse scopes recursively */
bool CheckCondition : : assignIfParseScope ( const Token * const assignTok ,
const Token * const startTok ,
const unsigned int varid ,
const bool islocal ,
const char bitop ,
const MathLib : : bigint num )
{
bool ret = false ;
for ( const Token * tok2 = startTok ; tok2 ; tok2 = tok2 - > next ( ) ) {
2014-09-01 10:52:27 +02:00
if ( ( bitop = = ' & ' ) & & Token : : Match ( tok2 - > tokAt ( 2 ) , " %varid% %cop% %num% ; " , varid ) & & tok2 - > strAt ( 3 ) = = std : : string ( 1U , bitop ) ) {
2014-08-29 17:06:46 +02:00
const MathLib : : bigint num2 = MathLib : : toLongNumber ( tok2 - > strAt ( 4 ) ) ;
2014-09-01 10:52:27 +02:00
if ( 0 = = ( num & num2 ) )
2014-08-29 17:06:46 +02:00
mismatchingBitAndError ( assignTok , num , tok2 , num2 ) ;
}
if ( Token : : Match ( tok2 , " %varid% = " , varid ) ) {
return true ;
}
if ( Token : : Match ( tok2 , " ++|-- %varid% " , varid ) | | Token : : Match ( tok2 , " %varid% ++|-- " , varid ) )
return true ;
2015-07-23 12:06:33 +02:00
if ( Token : : Match ( tok2 , " [(,] &| %varid% [,)] " , varid ) & & isParameterChanged ( tok2 ) )
return true ;
2014-08-29 17:06:46 +02:00
if ( tok2 - > str ( ) = = " } " )
return false ;
if ( Token : : Match ( tok2 , " break|continue|return " ) )
ret = true ;
if ( ret & & tok2 - > str ( ) = = " ; " )
return false ;
2015-01-31 10:50:39 +01:00
if ( ! islocal & & Token : : Match ( tok2 , " %name% ( " ) & & ! Token : : simpleMatch ( tok2 - > next ( ) - > link ( ) , " ) { " ) )
2014-08-29 17:06:46 +02:00
return true ;
if ( Token : : Match ( tok2 , " if|while ( " ) ) {
if ( ! islocal & & tok2 - > str ( ) = = " while " )
continue ;
2015-11-11 13:45:28 +01:00
if ( tok2 - > str ( ) = = " while " ) {
// is variable changed in loop?
const Token * bodyStart = tok2 - > linkAt ( 1 ) - > next ( ) ;
const Token * bodyEnd = bodyStart ? bodyStart - > link ( ) : nullptr ;
2016-10-23 13:54:44 +02:00
if ( ! bodyEnd | | bodyEnd - > str ( ) ! = " } " | | isVariableChanged ( bodyStart , bodyEnd , varid , _settings ) )
2015-11-11 13:45:28 +01:00
continue ;
}
2014-08-29 17:06:46 +02:00
// parse condition
const Token * const end = tok2 - > next ( ) - > link ( ) ;
for ( ; tok2 ! = end ; tok2 = tok2 - > next ( ) ) {
if ( Token : : Match ( tok2 , " [(,] &| %varid% [,)] " , varid ) ) {
return true ;
}
if ( Token : : Match ( tok2 , " &&|%oror%|( %varid% %any% %num% &&|%oror%|) " , varid ) ) {
const Token * vartok = tok2 - > next ( ) ;
const std : : string & op ( vartok - > strAt ( 1 ) ) ;
const MathLib : : bigint num2 = MathLib : : toLongNumber ( vartok - > strAt ( 2 ) ) ;
const std : : string condition ( vartok - > str ( ) + op + vartok - > strAt ( 2 ) ) ;
if ( op = = " == " & & ( num & num2 ) ! = ( ( bitop = = ' & ' ) ? num2 : num ) )
assignIfError ( assignTok , tok2 , condition , false ) ;
else if ( op = = " != " & & ( num & num2 ) ! = ( ( bitop = = ' & ' ) ? num2 : num ) )
assignIfError ( assignTok , tok2 , condition , true ) ;
}
2015-06-20 16:23:16 +02:00
if ( Token : : Match ( tok2 , " %varid% %op% " , varid ) & & tok2 - > next ( ) - > isAssignmentOp ( ) ) {
return true ;
}
2014-08-29 17:06:46 +02:00
}
bool ret1 = assignIfParseScope ( assignTok , end - > tokAt ( 2 ) , varid , islocal , bitop , num ) ;
bool ret2 = false ;
if ( Token : : simpleMatch ( end - > next ( ) - > link ( ) , " } else { " ) )
ret2 = assignIfParseScope ( assignTok , end - > next ( ) - > link ( ) - > tokAt ( 3 ) , varid , islocal , bitop , num ) ;
if ( ret1 | | ret2 )
return true ;
}
}
return false ;
}
void CheckCondition : : assignIfError ( const Token * tok1 , const Token * tok2 , const std : : string & condition , bool result )
{
std : : list < const Token * > locations ;
locations . push_back ( tok1 ) ;
locations . push_back ( tok2 ) ;
reportError ( locations ,
Severity : : style ,
" assignIfError " ,
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
" Mismatching assignment and comparison, comparison ' " + condition + " ' is always " + std : : string ( result ? " true " : " false " ) + " . " , CWE398 , false ) ;
2014-08-29 17:06:46 +02:00
}
void CheckCondition : : mismatchingBitAndError ( const Token * tok1 , const MathLib : : bigint num1 , const Token * tok2 , const MathLib : : bigint num2 )
{
std : : list < const Token * > locations ;
locations . push_back ( tok1 ) ;
locations . push_back ( tok2 ) ;
std : : ostringstream msg ;
msg < < " Mismatching bitmasks. Result is always 0 ( "
< < " X = Y & 0x " < < std : : hex < < num1 < < " ; Z = X & 0x " < < std : : hex < < num2 < < " ; => Z=0). " ;
reportError ( locations ,
Severity : : style ,
" mismatchingBitAnd " ,
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
msg . str ( ) , CWE398 , false ) ;
2014-08-29 17:06:46 +02:00
}
static void getnumchildren ( const Token * tok , std : : list < MathLib : : bigint > & numchildren )
{
if ( tok - > astOperand1 ( ) & & tok - > astOperand1 ( ) - > isNumber ( ) )
numchildren . push_back ( MathLib : : toLongNumber ( tok - > astOperand1 ( ) - > str ( ) ) ) ;
else if ( tok - > astOperand1 ( ) & & tok - > str ( ) = = tok - > astOperand1 ( ) - > str ( ) )
getnumchildren ( tok - > astOperand1 ( ) , numchildren ) ;
if ( tok - > astOperand2 ( ) & & tok - > astOperand2 ( ) - > isNumber ( ) )
numchildren . push_back ( MathLib : : toLongNumber ( tok - > astOperand2 ( ) - > str ( ) ) ) ;
else if ( tok - > astOperand2 ( ) & & tok - > str ( ) = = tok - > astOperand2 ( ) - > str ( ) )
getnumchildren ( tok - > astOperand2 ( ) , numchildren ) ;
}
2015-09-02 23:00:29 +02:00
/* Return whether tok is in the body for a function returning a boolean. */
static bool inBooleanFunction ( const Token * tok )
{
const Scope * scope = tok ? tok - > scope ( ) : 0 ;
while ( scope & & scope - > isLocal ( ) )
scope = scope - > nestedIn ;
if ( scope & & scope - > type = = Scope : : eFunction ) {
const Function * func = scope - > function ;
if ( func ) {
const Token * ret = func - > retDef ;
2017-02-06 15:37:12 +01:00
while ( Token : : Match ( ret , " static|const " ) )
2015-09-02 23:00:29 +02:00
ret = ret - > next ( ) ;
2017-03-04 11:13:28 +01:00
return Token : : Match ( ret , " bool|_Bool " ) ;
2015-09-02 23:00:29 +02:00
}
}
return false ;
}
2015-02-22 13:09:39 +01:00
void CheckCondition : : checkBadBitmaskCheck ( )
{
if ( ! _settings - > isEnabled ( " warning " ) )
return ;
for ( const Token * tok = _tokenizer - > tokens ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) = = " | " & & tok - > astOperand1 ( ) & & tok - > astOperand2 ( ) & & tok - > astParent ( ) ) {
2015-02-26 12:34:18 +01:00
const Token * parent = tok - > astParent ( ) ;
const bool isBoolean = Token : : Match ( parent , " &&|%oror% " ) | |
( parent - > str ( ) = = " ? " & & parent - > astOperand1 ( ) = = tok ) | |
2017-03-04 11:13:28 +01:00
( parent - > str ( ) = = " = " & & parent - > astOperand2 ( ) = = tok & & parent - > astOperand1 ( ) & & parent - > astOperand1 ( ) - > variable ( ) & & Token : : Match ( parent - > astOperand1 ( ) - > variable ( ) - > typeStartToken ( ) , " bool|_Bool " ) ) | |
2015-09-02 23:00:29 +02:00
( parent - > str ( ) = = " ( " & & Token : : Match ( parent - > astOperand1 ( ) , " if|while " ) ) | |
( parent - > str ( ) = = " return " & & parent - > astOperand1 ( ) = = tok & & inBooleanFunction ( tok ) ) ;
2015-02-22 13:09:39 +01:00
2016-01-24 13:45:44 +01:00
const bool isTrue = ( tok - > astOperand1 ( ) - > values . size ( ) = = 1 & & tok - > astOperand1 ( ) - > values . front ( ) . intvalue ! = 0 & & tok - > astOperand1 ( ) - > values . front ( ) . isKnown ( ) ) | |
( tok - > astOperand2 ( ) - > values . size ( ) = = 1 & & tok - > astOperand2 ( ) - > values . front ( ) . intvalue ! = 0 & & tok - > astOperand2 ( ) - > values . front ( ) . isKnown ( ) ) ;
2015-02-22 13:09:39 +01:00
if ( isBoolean & & isTrue )
badBitmaskCheckError ( tok ) ;
}
}
}
void CheckCondition : : badBitmaskCheckError ( const Token * tok )
{
2016-08-23 17:48:36 +02:00
reportError ( tok , Severity : : warning , " badBitmaskCheck " , " Result of operator '|' is always true if one operand is non-zero. Did you intend to use '&'? " , CWE571 , false ) ;
2015-02-22 13:09:39 +01:00
}
2014-08-29 17:06:46 +02:00
void CheckCondition : : comparison ( )
{
if ( ! _settings - > isEnabled ( " style " ) )
return ;
for ( const Token * tok = _tokenizer - > tokens ( ) ; tok ; tok = tok - > next ( ) ) {
2016-07-31 22:09:47 +02:00
if ( ! tok - > isComparisonOp ( ) )
continue ;
const Token * expr1 = tok - > astOperand1 ( ) ;
const Token * expr2 = tok - > astOperand2 ( ) ;
if ( ! expr1 | | ! expr2 )
continue ;
if ( expr1 - > isNumber ( ) )
std : : swap ( expr1 , expr2 ) ;
if ( ! expr2 - > isNumber ( ) )
continue ;
const MathLib : : bigint num2 = MathLib : : toLongNumber ( expr2 - > str ( ) ) ;
if ( num2 < 0 )
continue ;
if ( ! Token : : Match ( expr1 , " [&|] " ) )
continue ;
std : : list < MathLib : : bigint > numbers ;
getnumchildren ( expr1 , numbers ) ;
for ( std : : list < MathLib : : bigint > : : const_iterator num = numbers . begin ( ) ; num ! = numbers . end ( ) ; + + num ) {
const MathLib : : bigint num1 = * num ;
if ( num1 < 0 )
2014-08-29 17:06:46 +02:00
continue ;
2016-07-31 22:09:47 +02:00
if ( Token : : Match ( tok , " ==|!= " ) ) {
if ( ( expr1 - > str ( ) = = " & " & & ( num1 & num2 ) ! = num2 ) | |
( expr1 - > str ( ) = = " | " & & ( num1 | num2 ) ! = num2 ) ) {
const std : : string & op ( tok - > str ( ) ) ;
comparisonError ( expr1 , expr1 - > str ( ) , num1 , op , num2 , op = = " == " ? false : true ) ;
}
} else if ( expr1 - > str ( ) = = " & " ) {
const bool or_equal = Token : : Match ( tok , " >=|<= " ) ;
const std : : string & op ( tok - > str ( ) ) ;
if ( ( Token : : Match ( tok , " >=|< " ) ) & & ( num1 < num2 ) ) {
comparisonError ( expr1 , expr1 - > str ( ) , num1 , op , num2 , or_equal ? false : true ) ;
} else if ( ( Token : : Match ( tok , " <=|> " ) ) & & ( num1 < = num2 ) ) {
comparisonError ( expr1 , expr1 - > str ( ) , num1 , op , num2 , or_equal ? true : false ) ;
}
} else if ( expr1 - > str ( ) = = " | " ) {
if ( ( expr1 - > astOperand1 ( ) - > valueType ( ) ) & &
( expr1 - > astOperand1 ( ) - > valueType ( ) - > sign = = ValueType : : Sign : : UNSIGNED ) ) {
const bool or_equal = Token : : Match ( tok , " >=|<= " ) ;
const std : : string & op ( tok - > str ( ) ) ;
if ( ( Token : : Match ( tok , " >=|< " ) ) & & ( num1 > = num2 ) ) {
//"(a | 0x07) >= 7U" is always true for unsigned a
//"(a | 0x07) < 7U" is always false for unsigned a
comparisonError ( expr1 , expr1 - > str ( ) , num1 , op , num2 , or_equal ? true : false ) ;
} else if ( ( Token : : Match ( tok , " <=|> " ) ) & & ( num1 > num2 ) ) {
//"(a | 0x08) <= 7U" is always false for unsigned a
//"(a | 0x07) > 6U" is always true for unsigned a
comparisonError ( expr1 , expr1 - > str ( ) , num1 , op , num2 , or_equal ? false : true ) ;
2016-04-13 15:21:31 +02:00
}
2014-08-29 17:06:46 +02:00
}
}
}
}
}
void CheckCondition : : comparisonError ( const Token * tok , const std : : string & bitop , MathLib : : bigint value1 , const std : : string & op , MathLib : : bigint value2 , bool result )
{
std : : ostringstream expression ;
expression < < std : : hex < < " (X " < < bitop < < " 0x " < < value1 < < " ) " < < op < < " 0x " < < value2 ;
const std : : string errmsg ( " Expression ' " + expression . str ( ) + " ' is always " + ( result ? " true " : " false " ) + " . \n "
" The expression ' " + expression . str ( ) + " ' is always " + ( result ? " true " : " false " ) +
" . Check carefully constants and operators used, these errors might be hard to "
" spot sometimes. In case of complex expression it might help to split it to "
" separate expressions. " ) ;
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
reportError ( tok , Severity : : style , " comparisonError " , errmsg , CWE398 , false ) ;
2014-08-29 17:06:46 +02:00
}
2016-12-06 12:31:16 +01:00
bool CheckCondition : : isOverlappingCond ( const Token * const cond1 , const Token * const cond2 , bool pure ) const
2014-08-29 17:06:46 +02:00
{
if ( ! cond1 | | ! cond2 )
return false ;
// same expressions
2016-12-06 12:31:16 +01:00
if ( isSameExpression ( _tokenizer - > isCPP ( ) , true , cond1 , cond2 , _settings - > library , pure ) )
2014-08-29 17:06:46 +02:00
return true ;
// bitwise overlap for example 'x&7' and 'x==1'
if ( cond1 - > str ( ) = = " & " & & cond1 - > astOperand1 ( ) & & cond2 - > astOperand2 ( ) ) {
const Token * expr1 = cond1 - > astOperand1 ( ) ;
const Token * num1 = cond1 - > astOperand2 ( ) ;
if ( ! num1 ) // unary operator&
return false ;
if ( ! num1 - > isNumber ( ) )
std : : swap ( expr1 , num1 ) ;
if ( ! num1 - > isNumber ( ) | | MathLib : : isNegative ( num1 - > str ( ) ) )
return false ;
if ( ! Token : : Match ( cond2 , " &|== " ) | | ! cond2 - > astOperand1 ( ) | | ! cond2 - > astOperand2 ( ) )
return false ;
const Token * expr2 = cond2 - > astOperand1 ( ) ;
const Token * num2 = cond2 - > astOperand2 ( ) ;
if ( ! num2 - > isNumber ( ) )
std : : swap ( expr2 , num2 ) ;
if ( ! num2 - > isNumber ( ) | | MathLib : : isNegative ( num2 - > str ( ) ) )
return false ;
2016-12-06 12:31:16 +01:00
if ( ! isSameExpression ( _tokenizer - > isCPP ( ) , true , expr1 , expr2 , _settings - > library , pure ) )
2014-08-29 17:06:46 +02:00
return false ;
const MathLib : : bigint value1 = MathLib : : toLongNumber ( num1 - > str ( ) ) ;
const MathLib : : bigint value2 = MathLib : : toLongNumber ( num2 - > str ( ) ) ;
2015-02-01 13:03:38 +01:00
return ( ( value1 & value2 ) > 0 ) ;
2014-08-29 17:06:46 +02:00
}
return false ;
}
void CheckCondition : : multiCondition ( )
{
if ( ! _settings - > isEnabled ( " style " ) )
return ;
const SymbolDatabase * const symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
for ( std : : list < Scope > : : const_iterator i = symbolDatabase - > scopeList . begin ( ) ; i ! = symbolDatabase - > scopeList . end ( ) ; + + i ) {
2015-03-15 10:06:56 +01:00
if ( i - > type ! = Scope : : eIf )
2014-08-29 17:06:46 +02:00
continue ;
const Token * const cond1 = i - > classDef - > next ( ) - > astOperand2 ( ) ;
const Token * tok2 = i - > classDef - > next ( ) ;
2015-03-15 10:06:56 +01:00
for ( ; ; ) {
2014-08-29 17:06:46 +02:00
tok2 = tok2 - > link ( ) ;
if ( ! Token : : simpleMatch ( tok2 , " ) { " ) )
break ;
tok2 = tok2 - > linkAt ( 1 ) ;
if ( ! Token : : simpleMatch ( tok2 , " } else { if ( " ) )
break ;
tok2 = tok2 - > tokAt ( 4 ) ;
2016-12-06 12:31:16 +01:00
if ( isOverlappingCond ( cond1 , tok2 - > astOperand2 ( ) , true ) )
2014-08-29 17:06:46 +02:00
multiConditionError ( tok2 , cond1 - > linenr ( ) ) ;
}
}
}
void CheckCondition : : multiConditionError ( const Token * tok , unsigned int line1 )
{
std : : ostringstream errmsg ;
errmsg < < " Expression is always false because 'else if' condition matches previous condition at line "
< < line1 < < " . " ;
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
reportError ( tok , Severity : : style , " multiCondition " , errmsg . str ( ) , CWE398 , false ) ;
2014-08-29 17:06:46 +02:00
}
//---------------------------------------------------------------------------
// Detect oppositing inner and outer conditions
//---------------------------------------------------------------------------
void CheckCondition : : oppositeInnerCondition ( )
{
if ( ! _settings - > isEnabled ( " warning " ) )
return ;
const SymbolDatabase * symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
for ( std : : list < Scope > : : const_iterator scope = symbolDatabase - > scopeList . begin ( ) ; scope ! = symbolDatabase - > scopeList . end ( ) ; + + scope ) {
if ( scope - > type ! = Scope : : eIf )
continue ;
if ( ! Token : : simpleMatch ( scope - > classDef - > linkAt ( 1 ) , " ) { " ) )
continue ;
bool nonlocal = false ; // nonlocal variable used in condition
std : : set < unsigned int > vars ; // variables used in condition
2015-06-14 20:06:05 +02:00
for ( const Token * cond = scope - > classDef - > linkAt ( 1 ) ; cond ! = scope - > classDef ; cond = cond - > previous ( ) ) {
2014-08-29 17:06:46 +02:00
if ( cond - > varId ( ) ) {
vars . insert ( cond - > varId ( ) ) ;
const Variable * var = cond - > variable ( ) ;
nonlocal | = ( var & & ( ! var - > isLocal ( ) | | var - > isStatic ( ) ) & & ! var - > isArgument ( ) ) ;
// TODO: if var is pointer check what it points at
nonlocal | = ( var & & ( var - > isPointer ( ) | | var - > isReference ( ) ) ) ;
2015-08-07 15:25:47 +02:00
} else if ( ! nonlocal & & cond - > isName ( ) ) {
2014-08-29 17:06:46 +02:00
// varid is 0. this is possibly a nonlocal variable..
2015-08-07 15:25:47 +02:00
nonlocal = Token : : Match ( cond - > astParent ( ) , " %cop%|( " ) ;
2014-08-29 17:06:46 +02:00
}
}
// parse until inner condition is reached..
const Token * ifToken = nullptr ;
for ( const Token * tok = scope - > classStart ; tok & & tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
if ( Token : : simpleMatch ( tok , " if ( " ) ) {
ifToken = tok ;
break ;
}
if ( Token : : Match ( tok , " %type% ( " ) & & nonlocal ) // function call -> bailout if there are nonlocal variables
break ;
2017-02-28 18:46:28 +01:00
// bailout if loop is seen.
// TODO: handle loops.
if ( Token : : Match ( tok , " for|while|do " ) )
break ;
if ( ( tok - > varId ( ) & & vars . find ( tok - > varId ( ) ) ! = vars . end ( ) ) | |
( ! tok - > varId ( ) & & nonlocal ) ) {
2015-12-31 01:15:49 +01:00
if ( Token : : Match ( tok , " %name% %assign%|++|-- " ) )
2014-08-29 17:06:46 +02:00
break ;
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok , " %name% [ " ) ) {
2014-08-29 17:06:46 +02:00
const Token * tok2 = tok - > linkAt ( 1 ) ;
while ( Token : : simpleMatch ( tok2 , " ] [ " ) )
tok2 = tok2 - > linkAt ( 1 ) ;
2015-12-31 01:15:49 +01:00
if ( Token : : Match ( tok2 , " ] %assign%|++|-- " ) )
2014-08-29 17:06:46 +02:00
break ;
}
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok - > previous ( ) , " ++|--|& %name% " ) )
2014-08-29 17:06:46 +02:00
break ;
2014-12-31 18:19:10 +01:00
if ( tok - > variable ( ) & &
2015-08-07 15:25:47 +02:00
! tok - > variable ( ) - > isConst ( ) & &
Token : : Match ( tok , " %name% . %name% ( " ) ) {
2015-02-26 12:34:18 +01:00
const Function * function = tok - > tokAt ( 2 ) - > function ( ) ;
if ( ! function | | ! function - > isConst ( ) )
break ;
}
2015-07-23 12:06:33 +02:00
if ( Token : : Match ( tok - > previous ( ) , " [(,] %name% [,)] " ) & & isParameterChanged ( tok ) )
break ;
2014-08-29 17:06:46 +02:00
}
}
if ( ! ifToken )
continue ;
// Condition..
const Token * cond1 = scope - > classDef - > next ( ) - > astOperand2 ( ) ;
const Token * cond2 = ifToken - > next ( ) - > astOperand2 ( ) ;
2016-12-06 12:31:16 +01:00
if ( isOppositeCond ( false , _tokenizer - > isCPP ( ) , cond1 , cond2 , _settings - > library , true ) )
2014-08-29 17:06:46 +02:00
oppositeInnerConditionError ( scope - > classDef , cond2 ) ;
}
}
void CheckCondition : : oppositeInnerConditionError ( const Token * tok1 , const Token * tok2 )
{
std : : list < const Token * > callstack ;
callstack . push_back ( tok1 ) ;
callstack . push_back ( tok2 ) ;
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
reportError ( callstack , Severity : : warning , " oppositeInnerCondition " , " Opposite conditions in nested 'if' blocks lead to a dead code block. " , CWE398 , false ) ;
2014-08-29 17:06:46 +02:00
}
//---------------------------------------------------------------------------
// if ((x != 1) || (x != 3)) // expression always true
// if ((x == 1) && (x == 3)) // expression always false
// if ((x < 1) && (x > 3)) // expression always false
// if ((x > 3) || (x < 10)) // expression always true
// if ((x > 5) && (x != 1)) // second comparison always true
//
// Check for suspect logic for an expression consisting of 2 comparison
// expressions with a shared variable and constants and a logical operator
// between them.
//
// Suggest a different logical operator when the logical operator between
// the comparisons is probably wrong.
//
// Inform that second comparison is always true when first comparison is true.
//---------------------------------------------------------------------------
static std : : string invertOperatorForOperandSwap ( std : : string s )
{
if ( s [ 0 ] = = ' < ' )
s [ 0 ] = ' > ' ;
else if ( s [ 0 ] = = ' > ' )
s [ 0 ] = ' < ' ;
return s ;
}
2015-06-13 18:08:13 +02:00
template < typename T >
static bool checkIntRelation ( const std : : string & op , const T value1 , const T value2 )
2014-08-29 17:06:46 +02:00
{
return ( op = = " == " & & value1 = = value2 ) | |
( op = = " != " & & value1 ! = value2 ) | |
( op = = " > " & & value1 > value2 ) | |
( op = = " >= " & & value1 > = value2 ) | |
( op = = " < " & & value1 < value2 ) | |
( op = = " <= " & & value1 < = value2 ) ;
}
static bool checkFloatRelation ( const std : : string & op , const double value1 , const double value2 )
{
return ( op = = " > " & & value1 > value2 ) | |
( op = = " >= " & & value1 > = value2 ) | |
( op = = " < " & & value1 < value2 ) | |
( op = = " <= " & & value1 < = value2 ) ;
}
2015-06-14 20:25:52 +02:00
template < class T >
T getvalue3 ( const T value1 , const T value2 )
{
2015-06-28 19:20:16 +02:00
const T min = std : : min ( value1 , value2 ) ;
if ( min = = std : : numeric_limits < T > : : max ( ) )
return min ;
else
return min + 1 ; // see #5895
2015-06-14 20:25:52 +02:00
}
template < >
double getvalue3 ( const double value1 , const double value2 )
{
return ( value1 + value2 ) / 2.0f ;
}
2015-06-13 18:08:13 +02:00
template < class T >
static inline T getvalue ( const int test , const T value1 , const T value2 )
2014-08-29 17:06:46 +02:00
{
// test:
// 1 => return value that is less than both value1 and value2
// 2 => return value1
// 3 => return value that is between value1 and value2
// 4 => return value2
// 5 => return value that is larger than both value1 and value2
switch ( test ) {
2016-07-19 12:14:55 +02:00
case 1 :
return std : : numeric_limits < T > : : lowest ( ) ;
2014-08-29 17:06:46 +02:00
case 2 :
return value1 ;
case 3 :
2015-06-14 20:25:52 +02:00
return getvalue3 < T > ( value1 , value2 ) ;
2014-08-29 17:06:46 +02:00
case 4 :
return value2 ;
2016-07-19 12:14:55 +02:00
case 5 :
return std : : numeric_limits < T > : : max ( ) ;
2014-08-29 17:06:46 +02:00
} ;
return 0 ;
}
2016-05-24 15:08:07 +02:00
static bool parseComparison ( const Token * comp , bool * not1 , std : : string * op , std : : string * value , const Token * * expr , bool * inconclusive )
2015-07-13 20:53:49 +02:00
{
* not1 = false ;
while ( comp & & comp - > str ( ) = = " ! " ) {
* not1 = ! ( * not1 ) ;
comp = comp - > astOperand1 ( ) ;
}
2015-07-21 22:26:22 +02:00
if ( ! comp )
2015-07-13 20:53:49 +02:00
return false ;
2015-07-21 22:26:22 +02:00
if ( ! comp - > isComparisonOp ( ) | | ! comp - > astOperand1 ( ) | | ! comp - > astOperand2 ( ) ) {
* op = " != " ;
* value = " 0 " ;
* expr = comp ;
} else if ( comp - > astOperand1 ( ) - > isLiteral ( ) ) {
if ( comp - > astOperand1 ( ) - > isExpandedMacro ( ) )
return false ;
2015-07-13 20:53:49 +02:00
* op = invertOperatorForOperandSwap ( comp - > str ( ) ) ;
* value = comp - > astOperand1 ( ) - > str ( ) ;
* expr = comp - > astOperand2 ( ) ;
} else if ( comp - > astOperand2 ( ) - > isLiteral ( ) ) {
2015-07-21 22:26:22 +02:00
if ( comp - > astOperand2 ( ) - > isExpandedMacro ( ) )
return false ;
2015-07-13 20:53:49 +02:00
* op = comp - > str ( ) ;
* value = comp - > astOperand2 ( ) - > str ( ) ;
* expr = comp - > astOperand1 ( ) ;
} else {
2015-07-21 22:26:22 +02:00
* op = " != " ;
* value = " 0 " ;
* expr = comp ;
2015-07-13 20:53:49 +02:00
}
2016-05-24 15:08:07 +02:00
* inconclusive = * inconclusive | | ( ( * value ) [ 0 ] = = ' \' ' & & ! ( * op = = " != " | | * op = = " == " ) ) ;
2015-07-13 20:53:49 +02:00
// Only float and int values are currently handled
2016-05-24 15:08:07 +02:00
if ( ! MathLib : : isInt ( * value ) & & ! MathLib : : isFloat ( * value ) & & ( * value ) [ 0 ] ! = ' \' ' )
2015-07-13 20:53:49 +02:00
return false ;
return true ;
}
2015-07-21 22:26:22 +02:00
static std : : string conditionString ( bool not1 , const Token * expr1 , const std : : string & op , const std : : string & value1 )
{
if ( expr1 - > astParent ( ) - > isComparisonOp ( ) )
return std : : string ( not1 ? " !( " : " " ) +
( expr1 - > isName ( ) ? expr1 - > str ( ) : std : : string ( " EXPR " ) ) +
" " +
op +
" " +
value1 +
( not1 ? " ) " : " " ) ;
return std : : string ( not1 ? " ! " : " " ) +
( expr1 - > isName ( ) ? expr1 - > str ( ) : std : : string ( " EXPR " ) ) ;
}
2014-08-29 17:06:46 +02:00
void CheckCondition : : checkIncorrectLogicOperator ( )
{
2015-04-10 14:18:52 +02:00
const bool printStyle = _settings - > isEnabled ( " style " ) ;
const bool printWarning = _settings - > isEnabled ( " warning " ) ;
if ( ! printWarning & & ! printStyle )
2014-08-29 17:06:46 +02:00
return ;
2016-12-05 13:17:58 +01:00
const bool printInconclusive = _settings - > inconclusive ;
2014-08-29 17:06:46 +02:00
const SymbolDatabase * symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
const std : : size_t functions = symbolDatabase - > functionScopes . size ( ) ;
for ( std : : size_t ii = 0 ; ii < functions ; + + ii ) {
const Scope * scope = symbolDatabase - > functionScopes [ ii ] ;
for ( const Token * tok = scope - > classStart - > next ( ) ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
2015-07-13 20:53:49 +02:00
if ( ! Token : : Match ( tok , " %oror%|&& " ) | | ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
// Opposite comparisons around || or && => always true or always false
if ( ( tok - > astOperand1 ( ) - > isName ( ) | | tok - > astOperand2 ( ) - > isName ( ) ) & &
2016-12-06 12:31:16 +01:00
isOppositeCond ( true , _tokenizer - > isCPP ( ) , tok - > astOperand1 ( ) , tok - > astOperand2 ( ) , _settings - > library , true ) ) {
2014-08-29 17:06:46 +02:00
const bool alwaysTrue ( tok - > str ( ) = = " || " ) ;
2016-05-24 15:08:07 +02:00
incorrectLogicOperatorError ( tok , tok - > expressionString ( ) , alwaysTrue , false ) ;
2015-07-13 20:53:49 +02:00
continue ;
2014-08-29 17:06:46 +02:00
}
2016-12-05 13:17:58 +01:00
// 'A && (!A || B)' is equivalent to 'A && B'
// 'A || (!A && B)' is equivalent to 'A || B'
2015-11-05 19:27:10 +01:00
if ( printStyle & &
2015-08-05 11:15:54 +02:00
( ( tok - > str ( ) = = " || " & & tok - > astOperand2 ( ) - > str ( ) = = " && " ) | |
( tok - > str ( ) = = " && " & & tok - > astOperand2 ( ) - > str ( ) = = " || " ) ) ) {
2015-07-13 20:53:49 +02:00
const Token * tok2 = tok - > astOperand2 ( ) - > astOperand1 ( ) ;
2016-12-06 12:31:16 +01:00
if ( isOppositeCond ( true , _tokenizer - > isCPP ( ) , tok - > astOperand1 ( ) , tok2 , _settings - > library , true ) ) {
2015-08-05 11:15:54 +02:00
std : : string expr1 ( tok - > astOperand1 ( ) - > expressionString ( ) ) ;
std : : string expr2 ( tok - > astOperand2 ( ) - > astOperand1 ( ) - > expressionString ( ) ) ;
std : : string expr3 ( tok - > astOperand2 ( ) - > astOperand2 ( ) - > expressionString ( ) ) ;
2016-12-05 13:17:58 +01:00
// make copy for later because the original string might get overwritten
const std : : string expr1VerboseMsg = expr1 ;
const std : : string expr2VerboseMsg = expr2 ;
const std : : string expr3VerboseMsg = expr3 ;
2015-08-05 11:15:54 +02:00
if ( expr1 . length ( ) + expr2 . length ( ) + expr3 . length ( ) > 50U ) {
if ( expr1 [ 0 ] = = ' ! ' & & expr2 [ 0 ] ! = ' ! ' ) {
expr1 = " !A " ;
expr2 = " A " ;
} else {
2015-08-05 11:30:58 +02:00
expr1 = " A " ;
expr2 = " !A " ;
2015-08-05 11:15:54 +02:00
}
expr3 = " B " ;
}
const std : : string cond1 = expr1 + " " + tok - > str ( ) + " ( " + expr2 + " " + tok - > astOperand2 ( ) - > str ( ) + " " + expr3 + " ) " ;
const std : : string cond2 = expr1 + " " + tok - > str ( ) + " " + expr3 ;
2016-12-05 13:17:58 +01:00
const std : : string cond1VerboseMsg = expr1VerboseMsg + " " + tok - > str ( ) + " " + expr2VerboseMsg + " " + tok - > astOperand2 ( ) - > str ( ) + " " + expr3VerboseMsg ;
const std : : string cond2VerboseMsg = expr1VerboseMsg + " " + tok - > str ( ) + " " + expr3VerboseMsg ;
2016-12-05 17:30:06 +01:00
// for the --verbose message, transform the actual condition and print it
2016-12-05 13:17:58 +01:00
const std : : string msg = tok2 - > expressionString ( ) + " . ' " + cond1 + " ' is equivalent to ' " + cond2 + " ' \n "
" The condition ' " + cond1VerboseMsg + " ' is equivalent to ' " + cond2VerboseMsg + " '. " ;
redundantConditionError ( tok , msg , false ) ;
2014-08-29 17:06:46 +02:00
continue ;
}
2015-07-13 20:53:49 +02:00
}
2014-08-29 17:06:46 +02:00
2015-07-13 20:53:49 +02:00
// Comparison #1 (LHS)
const Token * comp1 = tok - > astOperand1 ( ) ;
if ( comp1 & & comp1 - > str ( ) = = tok - > str ( ) )
comp1 = comp1 - > astOperand2 ( ) ;
2014-08-29 17:06:46 +02:00
2015-07-13 20:53:49 +02:00
// Comparison #2 (RHS)
const Token * comp2 = tok - > astOperand2 ( ) ;
2014-08-29 17:06:46 +02:00
2016-05-24 15:08:07 +02:00
bool inconclusive = false ;
2015-07-13 20:53:49 +02:00
// Parse LHS
bool not1 ;
std : : string op1 , value1 ;
const Token * expr1 ;
2016-05-24 15:08:07 +02:00
if ( ! parseComparison ( comp1 , & not1 , & op1 , & value1 , & expr1 , & inconclusive ) )
2015-07-13 20:53:49 +02:00
continue ;
2014-08-29 17:06:46 +02:00
2015-07-13 20:53:49 +02:00
// Parse RHS
bool not2 ;
std : : string op2 , value2 ;
const Token * expr2 ;
2016-05-24 15:08:07 +02:00
if ( ! parseComparison ( comp2 , & not2 , & op2 , & value2 , & expr2 , & inconclusive ) )
continue ;
2016-12-05 13:17:58 +01:00
if ( inconclusive & & ! printInconclusive )
2015-07-13 20:53:49 +02:00
continue ;
2014-08-29 17:06:46 +02:00
2016-12-06 12:31:16 +01:00
if ( isSameExpression ( _tokenizer - > isCPP ( ) , true , comp1 , comp2 , _settings - > library , true ) )
2015-07-13 20:53:49 +02:00
continue ; // same expressions => only report that there are same expressions
2016-12-06 12:31:16 +01:00
if ( ! isSameExpression ( _tokenizer - > isCPP ( ) , true , expr1 , expr2 , _settings - > library , true ) )
2015-07-13 20:53:49 +02:00
continue ;
2014-08-29 17:06:46 +02:00
2015-07-13 20:53:49 +02:00
const bool isfloat = astIsFloat ( expr1 , true ) | | MathLib : : isFloat ( value1 ) | | astIsFloat ( expr2 , true ) | | MathLib : : isFloat ( value2 ) ;
// don't check floating point equality comparisons. that is bad
// and deserves different warnings.
if ( isfloat & & ( op1 = = " == " | | op1 = = " != " | | op2 = = " == " | | op2 = = " != " ) )
continue ;
2014-08-29 17:06:46 +02:00
2015-07-13 20:53:49 +02:00
const double d1 = ( isfloat ) ? MathLib : : toDoubleNumber ( value1 ) : 0 ;
const double d2 = ( isfloat ) ? MathLib : : toDoubleNumber ( value2 ) : 0 ;
const MathLib : : bigint i1 = ( isfloat ) ? 0 : MathLib : : toLongNumber ( value1 ) ;
const MathLib : : bigint i2 = ( isfloat ) ? 0 : MathLib : : toLongNumber ( value2 ) ;
const bool useUnsignedInt = ( std : : numeric_limits < MathLib : : bigint > : : max ( ) = = i1 ) | | ( std : : numeric_limits < MathLib : : bigint > : : max ( ) = = i2 ) ;
const MathLib : : biguint u1 = ( useUnsignedInt ) ? MathLib : : toLongNumber ( value1 ) : 0 ;
const MathLib : : biguint u2 = ( useUnsignedInt ) ? MathLib : : toLongNumber ( value2 ) : 0 ;
// evaluate if expression is always true/false
bool alwaysTrue = true , alwaysFalse = true ;
bool firstTrue = true , secondTrue = true ;
for ( int test = 1 ; test < = 5 ; + + test ) {
// test:
// 1 => testvalue is less than both value1 and value2
// 2 => testvalue is value1
// 3 => testvalue is between value1 and value2
// 4 => testvalue value2
// 5 => testvalue is larger than both value1 and value2
bool result1 , result2 ;
if ( isfloat ) {
const double testvalue = getvalue < double > ( test , d1 , d2 ) ;
result1 = checkFloatRelation ( op1 , testvalue , d1 ) ;
result2 = checkFloatRelation ( op2 , testvalue , d2 ) ;
} else if ( useUnsignedInt ) {
const MathLib : : biguint testvalue = getvalue < MathLib : : biguint > ( test , u1 , u2 ) ;
result1 = checkIntRelation ( op1 , testvalue , u1 ) ;
result2 = checkIntRelation ( op2 , testvalue , u2 ) ;
} else {
const MathLib : : bigint testvalue = getvalue < MathLib : : bigint > ( test , i1 , i2 ) ;
result1 = checkIntRelation ( op1 , testvalue , i1 ) ;
result2 = checkIntRelation ( op2 , testvalue , i2 ) ;
}
if ( not1 )
result1 = ! result1 ;
if ( not2 )
result2 = ! result2 ;
if ( tok - > str ( ) = = " && " ) {
alwaysTrue & = ( result1 & & result2 ) ;
alwaysFalse & = ! ( result1 & & result2 ) ;
} else {
alwaysTrue & = ( result1 | | result2 ) ;
alwaysFalse & = ! ( result1 | | result2 ) ;
2014-08-29 17:06:46 +02:00
}
2015-07-13 20:53:49 +02:00
firstTrue & = ! ( ! result1 & & result2 ) ;
secondTrue & = ! ( result1 & & ! result2 ) ;
}
2015-07-21 22:26:22 +02:00
const std : : string cond1str = conditionString ( not1 , expr1 , op1 , value1 ) ;
const std : : string cond2str = conditionString ( not2 , expr2 , op2 , value2 ) ;
2015-07-13 20:53:49 +02:00
if ( printWarning & & ( alwaysTrue | | alwaysFalse ) ) {
const std : : string text = cond1str + " " + tok - > str ( ) + " " + cond2str ;
2016-05-24 15:08:07 +02:00
incorrectLogicOperatorError ( tok , text , alwaysTrue , inconclusive ) ;
2015-07-13 20:53:49 +02:00
} else if ( printStyle & & secondTrue ) {
2015-07-30 16:24:56 +02:00
const std : : string text = " If ' " + cond1str + " ', the comparison ' " + cond2str +
" ' is always " + ( secondTrue ? " true " : " false " ) + " . " ;
2016-05-24 15:08:07 +02:00
redundantConditionError ( tok , text , inconclusive ) ;
2015-07-13 20:53:49 +02:00
} else if ( printStyle & & firstTrue ) {
//const std::string text = "The comparison " + cond1str + " is always " +
// (firstTrue ? "true" : "false") + " when " +
// cond2str + ".";
2015-07-30 16:24:56 +02:00
const std : : string text = " If ' " + cond2str + " ', the comparison ' " + cond1str +
" ' is always " + ( firstTrue ? " true " : " false " ) + " . " ;
2016-05-24 15:08:07 +02:00
redundantConditionError ( tok , text , inconclusive ) ;
2014-08-29 17:06:46 +02:00
}
}
}
}
2016-05-24 15:08:07 +02:00
void CheckCondition : : incorrectLogicOperatorError ( const Token * tok , const std : : string & condition , bool always , bool inconclusive )
2014-08-29 17:06:46 +02:00
{
if ( always )
reportError ( tok , Severity : : warning , " incorrectLogicOperator " ,
" Logical disjunction always evaluates to true: " + condition + " . \n "
" Logical disjunction always evaluates to true: " + condition + " . "
2016-05-24 15:08:07 +02:00
" Are these conditions necessary? Did you intend to use && instead? Are the numbers correct? Are you comparing the correct variables? " , CWE571 , inconclusive ) ;
2014-08-29 17:06:46 +02:00
else
reportError ( tok , Severity : : warning , " incorrectLogicOperator " ,
" Logical conjunction always evaluates to false: " + condition + " . \n "
" Logical conjunction always evaluates to false: " + condition + " . "
2016-05-24 15:08:07 +02:00
" Are these conditions necessary? Did you intend to use || instead? Are the numbers correct? Are you comparing the correct variables? " , CWE570 , inconclusive ) ;
2014-08-29 17:06:46 +02:00
}
2016-05-24 15:08:07 +02:00
void CheckCondition : : redundantConditionError ( const Token * tok , const std : : string & text , bool inconclusive )
2014-08-29 17:06:46 +02:00
{
2016-05-24 15:08:07 +02:00
reportError ( tok , Severity : : style , " redundantCondition " , " Redundant condition: " + text , CWE398 , inconclusive ) ;
2014-08-29 17:06:46 +02:00
}
//-----------------------------------------------------------------------------
// Detect "(var % val1) > val2" where val2 is >= val1.
//-----------------------------------------------------------------------------
void CheckCondition : : checkModuloAlwaysTrueFalse ( )
{
if ( ! _settings - > isEnabled ( " warning " ) )
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 ( ) ) {
if ( ! tok - > isComparisonOp ( ) )
continue ;
const Token * num , * modulo ;
if ( Token : : simpleMatch ( tok - > astOperand1 ( ) , " % " ) & & Token : : Match ( tok - > astOperand2 ( ) , " %num% " ) ) {
modulo = tok - > astOperand1 ( ) ;
num = tok - > astOperand2 ( ) ;
} else if ( Token : : Match ( tok - > astOperand1 ( ) , " %num% " ) & & Token : : simpleMatch ( tok - > astOperand2 ( ) , " % " ) ) {
num = tok - > astOperand1 ( ) ;
modulo = tok - > astOperand2 ( ) ;
} else {
continue ;
}
if ( Token : : Match ( modulo - > astOperand2 ( ) , " %num% " ) & &
MathLib : : isLessEqual ( modulo - > astOperand2 ( ) - > str ( ) , num - > str ( ) ) )
moduloAlwaysTrueFalseError ( tok , modulo - > astOperand2 ( ) - > str ( ) ) ;
}
}
}
void CheckCondition : : moduloAlwaysTrueFalseError ( const Token * tok , const std : : string & maxVal )
{
reportError ( tok , Severity : : warning , " moduloAlwaysTrueFalse " ,
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
" Comparison of modulo result is predetermined, because it is always less than " + maxVal + " . " , CWE398 , false ) ;
2014-08-29 17:06:46 +02:00
}
2016-07-29 18:46:43 +02:00
static int countPar ( const Token * tok1 , const Token * tok2 )
{
int par = 0 ;
for ( const Token * tok = tok1 ; tok & & tok ! = tok2 ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) = = " ( " )
+ + par ;
else if ( tok - > str ( ) = = " ) " )
- - par ;
else if ( tok - > str ( ) = = " ; " )
return - 1 ;
}
return par ;
}
2014-08-29 17:06:46 +02:00
//---------------------------------------------------------------------------
// Clarify condition '(x = a < 0)' into '((x = a) < 0)' or '(x = (a < 0))'
// Clarify condition '(a & b == c)' into '((a & b) == c)' or '(a & (b == c))'
//---------------------------------------------------------------------------
void CheckCondition : : clarifyCondition ( )
{
if ( ! _settings - > isEnabled ( " style " ) )
return ;
const bool isC = _tokenizer - > isC ( ) ;
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-01-31 10:50:39 +01:00
if ( Token : : Match ( tok , " ( %name% [=&|^] " ) ) {
2014-08-29 17:06:46 +02:00
for ( const Token * tok2 = tok - > tokAt ( 3 ) ; tok2 ; tok2 = tok2 - > next ( ) ) {
if ( tok2 - > str ( ) = = " ( " | | tok2 - > str ( ) = = " [ " )
tok2 = tok2 - > link ( ) ;
2015-08-14 20:46:13 +02:00
else if ( tok2 - > tokType ( ) = = Token : : eComparisonOp ) {
2014-08-29 17:06:46 +02:00
// This might be a template
if ( ! isC & & tok2 - > link ( ) )
break ;
2015-02-05 20:17:30 +01:00
if ( Token : : simpleMatch ( tok2 - > astParent ( ) , " ? " ) )
break ;
2014-08-29 17:06:46 +02:00
clarifyConditionError ( tok , tok - > strAt ( 2 ) = = " = " , false ) ;
break ;
} else if ( ! tok2 - > isName ( ) & & ! tok2 - > isNumber ( ) & & tok2 - > str ( ) ! = " . " )
break ;
}
2016-05-06 17:39:41 +02:00
} else if ( tok - > tokType ( ) = = Token : : eBitOp & & ( tok - > str ( ) ! = " & " | | tok - > astOperand2 ( ) ) ) {
if ( tok - > astOperand2 ( ) & & tok - > astOperand2 ( ) - > variable ( ) & & tok - > astOperand2 ( ) - > variable ( ) - > nameToken ( ) = = tok - > astOperand2 ( ) )
continue ;
2016-05-06 15:22:45 +02:00
// using boolean result in bitwise operation ! x [&|^]
const ValueType * vt1 = tok - > astOperand1 ( ) ? tok - > astOperand1 ( ) - > valueType ( ) : nullptr ;
const ValueType * vt2 = tok - > astOperand2 ( ) ? tok - > astOperand2 ( ) - > valueType ( ) : nullptr ;
2016-08-29 04:09:36 +02:00
if ( vt1 & & vt1 - > type = = ValueType : : BOOL & & ! Token : : Match ( tok - > astOperand1 ( ) , " %name%|(|[|::|. " ) & & countPar ( tok - > astOperand1 ( ) , tok ) = = 0 )
2016-05-06 15:22:45 +02:00
clarifyConditionError ( tok , false , true ) ;
2016-08-29 04:09:36 +02:00
else if ( vt2 & & vt2 - > type = = ValueType : : BOOL & & ! Token : : Match ( tok - > astOperand1 ( ) , " %name%|(|[|::|. " ) & & countPar ( tok , tok - > astOperand2 ( ) ) = = 0 )
2014-08-29 17:06:46 +02:00
clarifyConditionError ( tok , false , true ) ;
}
}
}
}
void CheckCondition : : clarifyConditionError ( const Token * tok , bool assign , bool boolop )
{
std : : string errmsg ;
if ( assign )
errmsg = " Suspicious condition (assignment + comparison); Clarify expression with parentheses. " ;
else if ( boolop )
errmsg = " Boolean result is used in bitwise operation. Clarify expression with parentheses. \n "
" Suspicious expression. Boolean result is used in bitwise operation. The operator '!' "
" and the comparison operators have higher precedence than bitwise operators. "
" It is recommended that the expression is clarified with parentheses. " ;
else
errmsg = " Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses. \n "
" Suspicious condition. Comparison operators have higher precedence than bitwise operators. "
" Please clarify the condition with parentheses. " ;
reportError ( tok ,
Severity : : style ,
" clarifyCondition " ,
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
errmsg , CWE398 , false ) ;
2014-08-29 17:06:46 +02:00
}
2015-07-17 15:30:23 +02:00
void CheckCondition : : alwaysTrueFalse ( )
{
if ( ! _settings - > isEnabled ( " style " ) )
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 ( ) ) {
2016-10-08 01:57:09 +02:00
2016-10-09 15:20:43 +02:00
const bool constValCond = Token : : Match ( tok - > tokAt ( - 2 ) , " if|while ( %num%|%char% ) " ) & & ! Token : : Match ( tok , " 0|1 " ) ; // just one number or char inside if|while
2016-10-08 01:57:09 +02:00
const bool constValExpr = Token : : Match ( tok , " %num%|%char% " ) & & tok - > astParent ( ) & & Token : : Match ( tok - > astParent ( ) , " &&|%oror%|? " ) ; // just one number or char in boolean expression
const bool compExpr = Token : : Match ( tok , " %comp%|! " ) ; // a compare expression
if ( ! ( constValCond | | constValExpr | | compExpr ) )
2015-07-17 15:30:23 +02:00
continue ;
if ( tok - > link ( ) ) // don't write false positives when templates are used
continue ;
2017-03-03 19:53:16 +01:00
if ( ! tok - > hasKnownIntValue ( ) )
2015-07-17 15:30:23 +02:00
continue ;
2016-02-06 14:37:44 +01:00
// Don't warn in assertions. Condition is often 'always true' by intention.
// If platform,defines,etc cause 'always false' then that is not dangerous neither.
2016-11-23 19:59:00 +01:00
bool assertFound = false ;
for ( const Token * tok2 = tok - > astParent ( ) ; tok2 ; tok2 = tok2 - > astParent ( ) ) { // move backwards and try to find "assert"
if ( tok2 - > str ( ) = = " ( " & & tok2 - > astOperand2 ( ) ) {
const std : : string & str = tok2 - > previous ( ) - > str ( ) ;
if ( ( str . find ( " assert " ) ! = std : : string : : npos | | str . find ( " ASSERT " ) ! = std : : string : : npos ) )
assertFound = true ;
break ;
}
}
if ( assertFound )
2016-02-06 14:37:44 +01:00
continue ;
2015-07-30 13:30:16 +02:00
// Don't warn when there are expanded macros..
bool isExpandedMacro = false ;
std : : stack < const Token * > tokens ;
tokens . push ( tok ) ;
while ( ! tokens . empty ( ) ) {
const Token * tok2 = tokens . top ( ) ;
tokens . pop ( ) ;
if ( ! tok2 )
continue ;
tokens . push ( tok2 - > astOperand1 ( ) ) ;
tokens . push ( tok2 - > astOperand2 ( ) ) ;
if ( tok2 - > isExpandedMacro ( ) ) {
isExpandedMacro = true ;
break ;
}
}
2017-03-03 19:53:16 +01:00
for ( const Token * parent = tok ; parent ; parent = parent - > astParent ( ) ) {
isExpandedMacro | = parent - > isExpandedMacro ( ) ;
}
2015-07-30 13:30:16 +02:00
if ( isExpandedMacro )
continue ;
alwaysTrueFalseError ( tok , tok - > values . front ( ) . intvalue ! = 0 ) ;
2015-07-17 15:30:23 +02:00
}
}
}
void CheckCondition : : alwaysTrueFalseError ( const Token * tok , bool knownResult )
{
const std : : string expr = tok ? tok - > expressionString ( ) : std : : string ( " x " ) ;
reportError ( tok ,
Severity : : style ,
" knownConditionTrueFalse " ,
2016-08-23 17:48:36 +02:00
" Condition ' " + expr + " ' is always " + ( knownResult ? " true " : " false " ) ,
2016-08-24 12:43:45 +02:00
( knownResult ? CWE571 : CWE570 ) , false ) ;
2015-07-17 15:30:23 +02:00
}
2015-11-30 08:51:15 +01:00
void CheckCondition : : checkInvalidTestForOverflow ( )
{
if ( ! _settings - > isEnabled ( " warning " ) )
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 ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
if ( ! tok - > isComparisonOp ( ) | | ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
const Token * calcToken , * exprToken ;
2015-11-30 11:12:51 +01:00
bool result ;
if ( Token : : Match ( tok , " <|>= " ) & & tok - > astOperand1 ( ) - > str ( ) = = " + " ) {
2015-11-30 08:51:15 +01:00
calcToken = tok - > astOperand1 ( ) ;
exprToken = tok - > astOperand2 ( ) ;
2015-11-30 11:12:51 +01:00
result = ( tok - > str ( ) = = " >= " ) ;
} else if ( Token : : Match ( tok , " >|<= " ) & & tok - > astOperand2 ( ) - > str ( ) = = " + " ) {
2015-11-30 08:51:15 +01:00
calcToken = tok - > astOperand2 ( ) ;
exprToken = tok - > astOperand1 ( ) ;
2015-11-30 11:12:51 +01:00
result = ( tok - > str ( ) = = " <= " ) ;
2015-11-30 08:51:15 +01:00
} else
continue ;
// Only warn for signed integer overflows and pointer overflows.
if ( ! ( calcToken - > valueType ( ) & & ( calcToken - > valueType ( ) - > pointer | | calcToken - > valueType ( ) - > sign = = ValueType : : Sign : : SIGNED ) ) )
continue ;
if ( ! ( exprToken - > valueType ( ) & & ( exprToken - > valueType ( ) - > pointer | | exprToken - > valueType ( ) - > sign = = ValueType : : Sign : : SIGNED ) ) )
continue ;
const Token * termToken ;
2016-12-06 12:31:16 +01:00
if ( isSameExpression ( _tokenizer - > isCPP ( ) , true , exprToken , calcToken - > astOperand1 ( ) , _settings - > library , true ) )
2015-11-30 08:51:15 +01:00
termToken = calcToken - > astOperand2 ( ) ;
2016-12-06 12:31:16 +01:00
else if ( isSameExpression ( _tokenizer - > isCPP ( ) , true , exprToken , calcToken - > astOperand2 ( ) , _settings - > library , true ) )
2015-11-30 08:51:15 +01:00
termToken = calcToken - > astOperand1 ( ) ;
else
continue ;
if ( ! termToken )
continue ;
// Only warn when termToken is always positive
if ( termToken - > valueType ( ) & & termToken - > valueType ( ) - > sign = = ValueType : : Sign : : UNSIGNED )
2015-11-30 11:12:51 +01:00
invalidTestForOverflow ( tok , result ) ;
2015-11-30 08:51:15 +01:00
else if ( termToken - > isNumber ( ) & & MathLib : : isPositive ( termToken - > str ( ) ) )
2015-11-30 11:12:51 +01:00
invalidTestForOverflow ( tok , result ) ;
2015-11-30 08:51:15 +01:00
}
}
}
2015-11-30 11:12:51 +01:00
void CheckCondition : : invalidTestForOverflow ( const Token * tok , bool result )
2015-11-30 08:51:15 +01:00
{
std : : string errmsg ;
errmsg = " Invalid test for overflow ' " +
( tok ? tok - > expressionString ( ) : std : : string ( " x + u < x " ) ) +
2015-11-30 11:12:51 +01:00
" '. Condition is always " +
std : : string ( result ? " true " : " false " ) +
" unless there is overflow, and overflow is UB. " ;
2016-08-23 17:48:36 +02:00
reportError ( tok , Severity : : warning , " invalidTestForOverflow " , errmsg , ( result ? CWE571 : CWE570 ) , false ) ;
2015-11-30 08:51:15 +01:00
}