2013-04-10 18:25:50 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2019-06-29 07:49:14 +02:00
* Copyright ( C ) 2007 - 2019 Cppcheck team .
2013-04-10 18:25:50 +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 "checkbool.h"
2017-05-27 04:33:47 +02:00
# include "astutils.h"
# include "errorlogger.h"
2013-04-10 18:25:50 +02:00
# include "mathlib.h"
2017-05-27 04:33:47 +02:00
# include "settings.h"
2013-04-10 18:25:50 +02:00
# include "symboldatabase.h"
2017-05-27 04:33:47 +02:00
# include "token.h"
# include "tokenize.h"
2018-11-01 11:08:16 +01:00
# include "valueflow.h"
2017-05-27 04:33:47 +02:00
# include <cstddef>
# include <list>
2013-04-10 18:25:50 +02:00
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace {
CheckBool instance ;
}
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
static const CWE CWE398 ( 398U ) ; // Indicator of Poor Code Quality
static const CWE CWE571 ( 571U ) ; // Expression is Always True
static const CWE CWE587 ( 587U ) ; // Assignment of a Fixed Address to a Pointer
2016-08-15 18:04:55 +02:00
static const CWE CWE704 ( 704U ) ; // Incorrect Type Conversion or Cast
2014-03-27 05:39:48 +01:00
2017-03-04 11:13:28 +01:00
static bool isBool ( const Variable * var )
{
return ( var & & Token : : Match ( var - > typeEndToken ( ) , " bool|_Bool " ) ) ;
}
2013-04-10 18:25:50 +02:00
//---------------------------------------------------------------------------
void CheckBool : : checkIncrementBoolean ( )
{
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > isEnabled ( Settings : : STYLE ) )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
const SymbolDatabase * symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2018-04-23 07:46:37 +02:00
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( const Token * tok = scope - > bodyStart - > next ( ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok , " %var% ++ " ) ) {
2014-09-11 16:55:00 +02:00
const Variable * var = tok - > variable ( ) ;
2017-03-04 11:13:28 +01:00
if ( isBool ( var ) )
2014-09-11 16:55:00 +02:00
incrementBooleanError ( tok ) ;
2013-04-10 18:25:50 +02:00
}
}
}
}
void CheckBool : : incrementBooleanError ( const Token * tok )
{
reportError (
tok ,
Severity : : style ,
" incrementboolean " ,
" Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead. \n "
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
" The operand of a postfix increment operator may be of type bool but it is deprecated by C++ Standard (Annex D-1) and the operand is always set to true. You should assign it the value 'true' instead. " ,
CWE398 , false
2013-04-10 18:25:50 +02:00
) ;
}
//---------------------------------------------------------------------------
// if (bool & bool) -> if (bool && bool)
// if (bool | bool) -> if (bool || bool)
//---------------------------------------------------------------------------
void CheckBool : : checkBitwiseOnBoolean ( )
{
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > isEnabled ( Settings : : STYLE ) )
2013-04-10 18:25:50 +02:00
return ;
// danmar: this is inconclusive because I don't like that there are
// warnings for calculations. Example: set_flag(a & b);
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > inconclusive )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
const SymbolDatabase * symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2018-04-23 07:46:37 +02:00
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( const Token * tok = scope - > bodyStart - > next ( ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2013-04-10 18:25:50 +02:00
if ( Token : : Match ( tok , " (|.|return|&&|%oror%|throw|, %var% [&|] " ) ) {
const Variable * var = tok - > next ( ) - > variable ( ) ;
2017-03-04 11:13:28 +01:00
if ( isBool ( var ) ) {
2013-04-10 18:25:50 +02:00
bitwiseOnBooleanError ( tok - > next ( ) , var - > name ( ) , tok - > strAt ( 2 ) = = " & " ? " && " : " || " ) ;
tok = tok - > tokAt ( 2 ) ;
}
} else if ( Token : : Match ( tok , " [&|] %var% ) | . | return | & & | % oror % | throw | , " ) && (!tok->previous() || !tok->previous()->isExtendedOp() || tok->strAt(-1) == " ) " || tok->strAt(-1) == " ] " )) {
const Variable * var = tok - > next ( ) - > variable ( ) ;
2017-03-04 11:13:28 +01:00
if ( isBool ( var ) ) {
2013-04-10 18:25:50 +02:00
bitwiseOnBooleanError ( tok - > next ( ) , var - > name ( ) , tok - > str ( ) = = " & " ? " && " : " || " ) ;
tok = tok - > tokAt ( 2 ) ;
}
}
}
}
}
void CheckBool : : bitwiseOnBooleanError ( const Token * tok , const std : : string & varname , const std : : string & op )
{
reportError ( tok , Severity : : style , " bitwiseOnBoolean " ,
2015-04-25 17:48:11 +02:00
" Boolean variable ' " + varname + " ' is used in bitwise operation. Did you mean ' " + op + " '? " ,
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
CWE398 ,
2015-04-25 17:48:11 +02:00
true ) ;
2013-04-10 18:25:50 +02:00
}
//---------------------------------------------------------------------------
// if (!x==3) <- Probably meant to be "x!=3"
//---------------------------------------------------------------------------
void CheckBool : : checkComparisonOfBoolWithInt ( )
{
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > isEnabled ( Settings : : WARNING ) | | ! mTokenizer - > isCPP ( ) )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
const SymbolDatabase * const symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2018-04-23 07:46:37 +02:00
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( const Token * tok = scope - > bodyStart - > next ( ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2018-07-13 23:06:45 +02:00
if ( ! tok - > isComparisonOp ( ) | | ! tok - > isBinaryOp ( ) )
continue ;
2015-03-12 20:07:48 +01:00
const Token * const left = tok - > astOperand1 ( ) ;
const Token * const right = tok - > astOperand2 ( ) ;
2018-07-13 23:06:45 +02:00
if ( left - > isBoolean ( ) & & right - > varId ( ) ) { // Comparing boolean constant with variable
if ( tok - > str ( ) ! = " == " & & tok - > str ( ) ! = " != " ) {
comparisonOfBoolWithInvalidComparator ( right , left - > str ( ) ) ;
}
} else if ( left - > varId ( ) & & right - > isBoolean ( ) ) { // Comparing variable with boolean constant
if ( tok - > str ( ) ! = " == " & & tok - > str ( ) ! = " != " ) {
comparisonOfBoolWithInvalidComparator ( right , left - > str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
}
}
}
}
void CheckBool : : comparisonOfBoolWithInvalidComparator ( const Token * tok , const std : : string & expression )
{
reportError ( tok , Severity : : warning , " comparisonOfBoolWithInvalidComparator " ,
" Comparison of a boolean value using relational operator (<, >, <= or >=). \n "
" The result of the expression ' " + expression + " ' is of type 'bool'. "
" Comparing 'bool' value using relational (<, >, <= or >=) "
" operator could cause unexpected results. " ) ;
}
//-------------------------------------------------------------------------------
// Comparing functions which are returning value of type bool
//-------------------------------------------------------------------------------
2014-08-25 09:49:17 +02:00
static bool tokenIsFunctionReturningBool ( const Token * tok )
{
2014-09-11 16:55:00 +02:00
const Function * func = tok - > function ( ) ;
2015-01-31 10:50:39 +01:00
if ( func & & Token : : Match ( tok , " %name% ( " ) ) {
2017-03-04 11:13:28 +01:00
if ( func - > tokenDef & & Token : : Match ( func - > tokenDef - > previous ( ) , " bool|_Bool " ) ) {
2014-08-25 09:49:17 +02:00
return true ;
}
}
return false ;
}
2013-04-10 18:25:50 +02:00
void CheckBool : : checkComparisonOfFuncReturningBool ( )
{
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > isEnabled ( Settings : : STYLE ) )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
if ( ! mTokenizer - > isCPP ( ) )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
const SymbolDatabase * const symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2013-04-10 18:25:50 +02:00
2018-04-23 07:46:37 +02:00
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( const Token * tok = scope - > bodyStart - > next ( ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2018-11-09 06:30:41 +01:00
if ( ! tok - > isComparisonOp ( ) | | tok - > str ( ) = = " == " | | tok - > str ( ) = = " != " )
2013-04-10 18:25:50 +02:00
continue ;
2014-08-25 09:49:17 +02:00
const Token * firstToken = tok - > previous ( ) ;
2013-04-10 18:25:50 +02:00
if ( tok - > strAt ( - 1 ) = = " ) " ) {
2014-08-25 09:49:17 +02:00
firstToken = firstToken - > link ( ) - > previous ( ) ;
2013-04-10 18:25:50 +02:00
}
2014-08-25 09:49:17 +02:00
const Token * secondToken = tok - > next ( ) ;
while ( secondToken - > str ( ) = = " ! " ) {
secondToken = secondToken - > next ( ) ;
2013-04-10 18:25:50 +02:00
}
2014-08-25 09:49:17 +02:00
const bool firstIsFunctionReturningBool = tokenIsFunctionReturningBool ( firstToken ) ;
const bool secondIsFunctionReturningBool = tokenIsFunctionReturningBool ( secondToken ) ;
if ( firstIsFunctionReturningBool & & secondIsFunctionReturningBool ) {
comparisonOfTwoFuncsReturningBoolError ( firstToken - > next ( ) , firstToken - > str ( ) , secondToken - > str ( ) ) ;
} else if ( firstIsFunctionReturningBool ) {
comparisonOfFuncReturningBoolError ( firstToken - > next ( ) , firstToken - > str ( ) ) ;
} else if ( secondIsFunctionReturningBool ) {
comparisonOfFuncReturningBoolError ( secondToken - > previous ( ) , secondToken - > str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
}
}
}
void CheckBool : : comparisonOfFuncReturningBoolError ( const Token * tok , const std : : string & expression )
{
reportError ( tok , Severity : : style , " comparisonOfFuncReturningBoolError " ,
" Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. \n "
" The return type of function ' " + expression + " ' is 'bool' "
" and result is of type 'bool'. Comparing 'bool' value using relational (<, >, <= or >=) "
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
" operator could cause unexpected results. " , CWE398 , false ) ;
2013-04-10 18:25:50 +02:00
}
void CheckBool : : comparisonOfTwoFuncsReturningBoolError ( const Token * tok , const std : : string & expression1 , const std : : string & expression2 )
{
reportError ( tok , Severity : : style , " comparisonOfTwoFuncsReturningBoolError " ,
" Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. \n "
" The return type of function ' " + expression1 + " ' and function ' " + expression2 + " ' is 'bool' "
" and result is of type 'bool'. Comparing 'bool' value using relational (<, >, <= or >=) "
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
" operator could cause unexpected results. " , CWE398 , false ) ;
2013-04-10 18:25:50 +02:00
}
//-------------------------------------------------------------------------------
// Comparison of bool with bool
//-------------------------------------------------------------------------------
void CheckBool : : checkComparisonOfBoolWithBool ( )
{
// FIXME: This checking is "experimental" because of the false positives
// when self checking lib/tokenize.cpp (#2617)
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > experimental )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > isEnabled ( Settings : : STYLE ) )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
if ( ! mTokenizer - > isCPP ( ) )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
const SymbolDatabase * const symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2013-04-10 18:25:50 +02:00
2018-04-23 07:46:37 +02:00
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( const Token * tok = scope - > bodyStart - > next ( ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2018-11-09 06:30:41 +01:00
if ( ! tok - > isComparisonOp ( ) | | tok - > str ( ) = = " == " | | tok - > str ( ) = = " != " )
2013-04-10 18:25:50 +02:00
continue ;
2014-12-01 16:22:56 +01:00
bool firstTokenBool = false ;
2013-04-10 18:25:50 +02:00
2014-12-01 16:22:56 +01:00
const Token * firstToken = tok - > previous ( ) ;
if ( firstToken - > varId ( ) ) {
if ( isBool ( firstToken - > variable ( ) ) ) {
firstTokenBool = true ;
2013-04-10 18:25:50 +02:00
}
}
2014-12-01 16:22:56 +01:00
if ( ! firstTokenBool )
2014-06-09 11:35:30 +02:00
continue ;
2014-12-01 16:22:56 +01:00
bool secondTokenBool = false ;
const Token * secondToken = tok - > next ( ) ;
if ( secondToken - > varId ( ) ) {
if ( isBool ( secondToken - > variable ( ) ) ) {
secondTokenBool = true ;
2013-04-10 18:25:50 +02:00
}
}
2014-12-01 16:22:56 +01:00
if ( secondTokenBool ) {
comparisonOfBoolWithBoolError ( firstToken - > next ( ) , secondToken - > str ( ) ) ;
2013-04-10 18:25:50 +02:00
}
}
}
}
void CheckBool : : comparisonOfBoolWithBoolError ( const Token * tok , const std : : string & expression )
{
reportError ( tok , Severity : : style , " comparisonOfBoolWithBoolError " ,
" Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. \n "
" The variable ' " + expression + " ' is of type 'bool' "
" and comparing 'bool' value using relational (<, >, <= or >=) "
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
" operator could cause unexpected results. " , CWE398 , false ) ;
2013-04-10 18:25:50 +02:00
}
//-----------------------------------------------------------------------------
void CheckBool : : checkAssignBoolToPointer ( )
{
2018-06-16 16:10:28 +02:00
const SymbolDatabase * symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2018-04-23 07:46:37 +02:00
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( const Token * tok = scope - > bodyStart ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2019-09-21 08:24:54 +02:00
if ( tok - > str ( ) = = " = " & & astIsPointer ( tok - > astOperand1 ( ) ) & & astIsBool ( tok - > astOperand2 ( ) ) ) {
assignBoolToPointerError ( tok ) ;
}
2013-04-10 18:25:50 +02:00
}
}
}
void CheckBool : : assignBoolToPointerError ( const Token * tok )
{
reportError ( tok , Severity : : error , " assignBoolToPointer " ,
2016-01-25 20:01:48 +01:00
" Boolean value assigned to pointer. " , CWE587 , false ) ;
2013-04-10 18:25:50 +02:00
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CheckBool : : checkComparisonOfBoolExpressionWithInt ( )
{
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > isEnabled ( Settings : : WARNING ) )
2013-04-10 18:25:50 +02:00
return ;
2018-06-16 16:10:28 +02:00
const SymbolDatabase * symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2013-04-10 18:25:50 +02:00
2018-04-23 07:46:37 +02:00
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( const Token * tok = scope - > bodyStart - > next ( ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2014-03-27 16:10:43 +01:00
if ( ! tok - > isComparisonOp ( ) )
continue ;
2017-07-28 15:20:43 +02:00
const Token * numTok = nullptr ;
const Token * boolExpr = nullptr ;
2014-03-27 16:10:43 +01:00
bool numInRhs ;
if ( astIsBool ( tok - > astOperand1 ( ) ) ) {
boolExpr = tok - > astOperand1 ( ) ;
numTok = tok - > astOperand2 ( ) ;
numInRhs = true ;
} else if ( astIsBool ( tok - > astOperand2 ( ) ) ) {
boolExpr = tok - > astOperand2 ( ) ;
numTok = tok - > astOperand1 ( ) ;
numInRhs = false ;
} else {
continue ;
2013-04-10 18:25:50 +02:00
}
2014-06-09 11:35:30 +02:00
if ( ! numTok | | ! boolExpr )
2014-03-27 16:10:43 +01:00
continue ;
2013-09-07 11:32:11 +02:00
2014-03-27 16:10:43 +01:00
if ( boolExpr - > isOp ( ) & & numTok - > isName ( ) & & Token : : Match ( tok , " ==|!= " ) )
// there is weird code such as: ((a<b)==c)
// but it is probably written this way by design.
continue ;
2013-09-07 11:32:11 +02:00
2019-06-27 07:48:44 +02:00
if ( astIsBool ( numTok ) )
continue ;
2014-03-27 16:10:43 +01:00
if ( numTok - > isNumber ( ) ) {
2016-02-19 21:38:54 +01:00
const MathLib : : bigint num = MathLib : : toLongNumber ( numTok - > str ( ) ) ;
if ( num = = 0 & &
2015-01-09 23:43:45 +01:00
( numInRhs ? Token : : Match ( tok , " >|==|!= " )
: Token : : Match ( tok , " <|==|!= " ) ) )
2014-03-27 16:10:43 +01:00
continue ;
2016-02-19 21:38:54 +01:00
if ( num = = 1 & &
2015-01-09 23:43:45 +01:00
( numInRhs ? Token : : Match ( tok , " <|==|!= " )
: Token : : Match ( tok , " >|==|!= " ) ) )
2014-03-27 16:10:43 +01:00
continue ;
comparisonOfBoolExpressionWithIntError ( tok , true ) ;
2019-06-27 07:48:44 +02:00
} else if ( astIsIntegral ( numTok , false ) & & mTokenizer - > isCPP ( ) )
2014-03-27 16:10:43 +01:00
comparisonOfBoolExpressionWithIntError ( tok , false ) ;
2013-04-10 18:25:50 +02:00
}
}
}
void CheckBool : : comparisonOfBoolExpressionWithIntError ( const Token * tok , bool n0o1 )
{
if ( n0o1 )
reportError ( tok , Severity : : warning , " compareBoolExpressionWithInt " ,
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 a boolean expression with an integer other than 0 or 1. " , CWE398 , false ) ;
2013-04-10 18:25:50 +02:00
else
reportError ( tok , Severity : : warning , " compareBoolExpressionWithInt " ,
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 a boolean expression with an integer. " , CWE398 , false ) ;
2013-04-10 18:25:50 +02:00
}
2013-12-23 18:39:05 +01:00
void CheckBool : : pointerArithBool ( )
{
2018-06-16 16:10:28 +02:00
const SymbolDatabase * symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2013-12-23 18:39:05 +01:00
2018-04-24 18:17:47 +02:00
for ( const Scope & scope : symbolDatabase - > scopeList ) {
if ( scope . type ! = Scope : : eIf & & scope . type ! = Scope : : eWhile & & scope . type ! = Scope : : eDo & & scope . type ! = Scope : : eFor )
2015-03-12 20:07:48 +01:00
continue ;
2018-04-24 18:17:47 +02:00
const Token * tok = scope . classDef - > next ( ) - > astOperand2 ( ) ;
if ( scope . type = = Scope : : eFor ) {
tok = Token : : findsimplematch ( scope . classDef - > tokAt ( 2 ) , " ; " ) ;
2015-03-12 20:07:48 +01:00
if ( tok )
tok = tok - > astOperand2 ( ) ;
if ( tok )
tok = tok - > astOperand1 ( ) ;
2018-04-24 18:17:47 +02:00
} else if ( scope . type = = Scope : : eDo )
2018-04-27 22:36:30 +02:00
tok = ( scope . bodyEnd - > tokAt ( 2 ) ) ? scope . bodyEnd - > tokAt ( 2 ) - > astOperand2 ( ) : nullptr ;
2015-03-12 20:07:48 +01:00
pointerArithBoolCond ( tok ) ;
2013-12-23 18:39:05 +01:00
}
}
void CheckBool : : pointerArithBoolCond ( const Token * tok )
{
if ( ! tok )
return ;
if ( Token : : Match ( tok , " &&|%oror% " ) ) {
pointerArithBoolCond ( tok - > astOperand1 ( ) ) ;
pointerArithBoolCond ( tok - > astOperand2 ( ) ) ;
return ;
}
2015-03-12 20:07:48 +01:00
if ( tok - > str ( ) ! = " + " & & tok - > str ( ) ! = " - " )
2013-12-23 18:39:05 +01:00
return ;
2018-07-13 23:06:45 +02:00
if ( tok - > isBinaryOp ( ) & &
2013-12-23 18:39:05 +01:00
tok - > astOperand1 ( ) - > isName ( ) & &
tok - > astOperand1 ( ) - > variable ( ) & &
tok - > astOperand1 ( ) - > variable ( ) - > isPointer ( ) & &
tok - > astOperand2 ( ) - > isNumber ( ) )
pointerArithBoolError ( tok ) ;
}
void CheckBool : : pointerArithBoolError ( const Token * tok )
{
reportError ( tok ,
Severity : : error ,
" pointerArithBool " ,
2013-12-25 19:56:00 +01:00
" Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour. \n "
2016-01-25 20:01:48 +01:00
" Converting pointer arithmetic result to bool. The boolean result is always true unless there is pointer arithmetic overflow, and overflow is undefined behaviour. Probably a dereference is forgotten. " , CWE571 , false ) ;
2013-12-23 18:39:05 +01:00
}
2014-05-24 18:35:49 +02:00
void CheckBool : : checkAssignBoolToFloat ( )
{
2018-06-16 16:10:28 +02:00
if ( ! mTokenizer - > isCPP ( ) )
2014-05-24 18:35:49 +02:00
return ;
2018-06-16 16:10:28 +02:00
if ( ! mSettings - > isEnabled ( Settings : : STYLE ) )
2014-05-24 18:35:49 +02:00
return ;
2018-06-16 16:10:28 +02:00
const SymbolDatabase * symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
2018-04-23 07:46:37 +02:00
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( const Token * tok = scope - > bodyStart ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2019-09-21 08:24:54 +02:00
if ( tok - > str ( ) = = " = " & & astIsFloat ( tok - > astOperand1 ( ) , false ) & & astIsBool ( tok - > astOperand2 ( ) ) ) {
assignBoolToFloatError ( tok ) ;
2014-05-24 18:35:49 +02:00
}
}
}
}
void CheckBool : : assignBoolToFloatError ( const Token * tok )
{
reportError ( tok , Severity : : style , " assignBoolToFloat " ,
2016-08-12 15:19:27 +02:00
" Boolean value assigned to floating point variable. " , CWE704 , false ) ;
2014-05-24 18:35:49 +02:00
}
2018-11-01 11:08:16 +01:00
void CheckBool : : returnValueOfFunctionReturningBool ( void )
{
if ( ! mSettings - > isEnabled ( Settings : : STYLE ) )
return ;
const SymbolDatabase * const symbolDatabase = mTokenizer - > getSymbolDatabase ( ) ;
for ( const Scope * scope : symbolDatabase - > functionScopes ) {
if ( ! ( scope - > function & & Token : : Match ( scope - > function - > retDef , " bool|_Bool " ) ) )
continue ;
for ( const Token * tok = scope - > bodyStart - > next ( ) ; tok & & ( tok ! = scope - > bodyEnd ) ; tok = tok - > next ( ) ) {
// Skip lambdas
const Token * tok2 = findLambdaEndToken ( tok ) ;
if ( tok2 )
tok = tok2 ;
2019-05-14 08:56:28 +02:00
else if ( tok - > scope ( ) & & tok - > scope ( ) - > isClassOrStruct ( ) )
tok = tok - > scope ( ) - > bodyEnd ;
2018-11-01 11:08:16 +01:00
else if ( Token : : simpleMatch ( tok , " return " ) & & tok - > astOperand1 ( ) & &
2019-05-02 07:00:27 +02:00
( tok - > astOperand1 ( ) - > getValueGE ( 2 , mSettings ) | | tok - > astOperand1 ( ) - > getValueLE ( - 1 , mSettings ) ) & &
! ( tok - > astOperand1 ( ) - > astOperand1 ( ) & & Token : : Match ( tok - > astOperand1 ( ) , " &|%or% " ) ) )
2018-11-01 11:08:16 +01:00
returnValueBoolError ( tok ) ;
}
}
}
void CheckBool : : returnValueBoolError ( const Token * tok )
{
reportError ( tok , Severity : : style , " returnNonBoolInBooleanFunction " , " Non-boolean value returned from function returning bool " ) ;
}