2014-01-04 20:57:02 +01:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2022-02-05 11:45:17 +01:00
* Copyright ( C ) 2007 - 2022 Cppcheck team .
2014-01-04 20:57:02 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2018-05-05 23:03:04 +02:00
/**
* @ brief This is the ValueFlow component in Cppcheck .
*
2018-05-07 22:06:23 +02:00
* Each @ sa Token in the token list has a list of values . These are
2018-05-05 23:03:04 +02:00
* the " possible " values for the Token at runtime .
*
* In the - - debug and - - debug - normal output you can see the ValueFlow data . For example :
*
* int f ( )
* {
* int x = 10 ;
* return 4 * x + 2 ;
* }
*
* The - - debug - normal output says :
*
* # # Value flow
* Line 3
* 10 always 10
* Line 4
* 4 always 4
* * always 40
* x always 10
* + always 42
* 2 always 2
*
2019-09-26 10:32:25 +02:00
* All value flow analysis is executed in the ValueFlow : : setValues ( ) function . The ValueFlow analysis is executed after
* the tokenizer / ast / symboldatabase / etc . . The ValueFlow analysis is done in a series of valueFlow * function calls , where
* each such function call can only use results from previous function calls . The function calls should be arranged so
* that valueFlow * that do not require previous ValueFlow information should be first .
2018-05-05 23:03:04 +02:00
*
* Type of analysis
* = = = = = = = = = = = = = = = =
*
* This is " flow sensitive " value flow analysis . We _usually_ track the value for 1 variable at a time .
*
* How are calculations handled
* = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*
* Here is an example code :
*
* x = 3 + 4 ;
*
* The valueFlowNumber set the values for the " 3 " and " 4 " tokens by calling setTokenValue ( ) .
2019-09-26 10:32:25 +02:00
* The setTokenValue ( ) handle the calculations automatically . When both " 3 " and " 4 " have values , the " + " can be
* calculated . setTokenValue ( ) recursively calls itself when parents in calculations can be calculated .
2018-05-05 23:03:04 +02:00
*
* Forward / Reverse flow analysis
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*
2019-09-26 10:32:25 +02:00
* In forward value flow analysis we know a value and see what happens when we are stepping the program forward . Like
2021-07-18 07:46:31 +02:00
* normal execution . The valueFlowForward is used in this analysis .
2018-05-05 23:03:04 +02:00
*
2019-09-26 10:32:25 +02:00
* In reverse value flow analysis we know the value of a variable at line X . And try to " execute backwards " to determine
* possible values before line X . The valueFlowReverse is used in this analysis .
2018-05-05 23:03:04 +02:00
*
*
*/
2014-01-04 20:57:02 +01:00
# include "valueflow.h"
2017-05-27 04:33:47 +02:00
2020-11-10 16:00:55 +01:00
# include "analyzer.h"
2015-11-11 13:45:28 +01:00
# include "astutils.h"
2021-10-04 07:53:58 +02:00
# include "calculate.h"
2021-07-01 22:08:00 +02:00
# include "checkuninitvar.h"
2021-08-09 07:45:41 +02:00
# include "config.h"
2014-01-05 20:06:46 +01:00
# include "errorlogger.h"
2021-01-10 13:30:00 +01:00
# include "errortypes.h"
2020-02-13 16:27:06 +01:00
# include "forwardanalyzer.h"
2021-10-09 16:19:06 +02:00
# include "infer.h"
2017-05-27 04:33:47 +02:00
# include "library.h"
2014-01-04 20:57:02 +01:00
# include "mathlib.h"
2020-02-13 16:27:06 +01:00
# include "path.h"
2017-05-27 04:33:47 +02:00
# include "platform.h"
2019-10-30 17:57:46 +01:00
# include "programmemory.h"
2020-11-10 16:00:55 +01:00
# include "reverseanalyzer.h"
2014-01-05 20:06:46 +01:00
# include "settings.h"
2017-05-27 04:33:47 +02:00
# include "standards.h"
2014-01-05 20:06:46 +01:00
# include "symboldatabase.h"
# include "token.h"
# include "tokenlist.h"
2017-05-23 15:01:56 +02:00
# include "utils.h"
2020-05-10 16:45:45 +02:00
# include "valueptr.h"
2017-05-27 04:33:47 +02:00
# include <algorithm>
2021-11-01 19:18:18 +01:00
# include <array>
2019-09-03 17:15:58 +02:00
# include <cassert>
2022-01-27 19:03:20 +01:00
# include <climits>
2021-08-27 05:46:33 +02:00
# include <cstdlib>
2020-05-26 20:13:56 +02:00
# include <cstring>
2022-01-27 19:03:20 +01:00
# include <exception>
2018-08-10 18:05:23 +02:00
# include <functional>
2022-01-27 19:03:20 +01:00
# include <initializer_list>
# include <iosfwd>
2018-08-10 18:05:23 +02:00
# include <iterator>
2017-05-27 04:33:47 +02:00
# include <limits>
# include <map>
2022-01-27 19:03:20 +01:00
# include <memory>
2017-05-27 04:33:47 +02:00
# include <set>
2021-01-08 22:55:04 +01:00
# include <string>
2021-08-09 07:45:41 +02:00
# include <type_traits>
2021-09-09 07:49:56 +02:00
# include <unordered_map>
2021-08-21 18:52:11 +02:00
# include <unordered_set>
2017-05-27 04:33:47 +02:00
# include <vector>
2014-01-06 16:37:52 +01:00
2021-01-08 22:55:04 +01:00
static void bailoutInternal ( const std : : string & type , TokenList * tokenlist , ErrorLogger * errorLogger , const Token * tok , const std : : string & what , const std : : string & file , int line , std : : string function )
2014-01-06 07:44:58 +01:00
{
2021-01-08 22:55:04 +01:00
if ( function . find ( " operator " ) ! = std : : string : : npos )
function = " (valueFlow) " ;
2020-05-23 07:16:49 +02:00
std : : list < ErrorMessage : : FileLocation > callstack ( 1 , ErrorMessage : : FileLocation ( tok , tokenlist ) ) ;
ErrorMessage errmsg ( callstack , tokenlist - > getSourceFilePath ( ) , Severity : : debug ,
2021-02-24 22:00:06 +01:00
Path : : stripDirectoryPart ( file ) + " : " + MathLib : : toString ( line ) + " : " + function + " bailout: " + what , type , Certainty : : normal ) ;
2014-01-06 07:44:58 +01:00
errorLogger - > reportErr ( errmsg ) ;
}
2020-12-26 13:48:22 +01:00
# define bailout2(type, tokenlist, errorLogger, tok, what) bailoutInternal(type, tokenlist, errorLogger, tok, what, __FILE__, __LINE__, __func__)
2017-10-05 23:03:13 +02:00
2020-12-26 13:48:22 +01:00
# define bailout(tokenlist, errorLogger, tok, what) bailout2("valueFlowBailout", tokenlist, errorLogger, tok, what)
# define bailoutIncompleteVar(tokenlist, errorLogger, tok, what) bailout2("valueFlowBailoutIncompleteVar", tokenlist, errorLogger, tok, what)
2019-08-30 18:32:45 +02:00
static void changeKnownToPossible ( std : : list < ValueFlow : : Value > & values , int indirect = - 1 )
2017-11-17 23:04:54 +01:00
{
2019-08-30 18:32:45 +02:00
for ( ValueFlow : : Value & v : values ) {
if ( indirect > = 0 & & v . indirect ! = indirect )
continue ;
v . changeKnownToPossible ( ) ;
}
2017-11-17 23:04:54 +01:00
}
2019-09-20 15:06:37 +02:00
static void removeImpossible ( std : : list < ValueFlow : : Value > & values , int indirect = - 1 )
{
values . remove_if ( [ & ] ( const ValueFlow : : Value & v ) {
if ( indirect > = 0 & & v . indirect ! = indirect )
return false ;
return v . isImpossible ( ) ;
} ) ;
}
static void lowerToPossible ( std : : list < ValueFlow : : Value > & values , int indirect = - 1 )
{
changeKnownToPossible ( values , indirect ) ;
removeImpossible ( values , indirect ) ;
}
static void changePossibleToKnown ( std : : list < ValueFlow : : Value > & values , int indirect = - 1 )
{
for ( ValueFlow : : Value & v : values ) {
if ( indirect > = 0 & & v . indirect ! = indirect )
continue ;
if ( ! v . isPossible ( ) )
continue ;
if ( v . bound ! = ValueFlow : : Value : : Bound : : Point )
continue ;
v . setKnown ( ) ;
}
}
2019-09-28 20:50:56 +02:00
static void setValueUpperBound ( ValueFlow : : Value & value , bool upper )
2019-09-20 15:06:37 +02:00
{
if ( upper )
value . bound = ValueFlow : : Value : : Bound : : Upper ;
else
value . bound = ValueFlow : : Value : : Bound : : Lower ;
}
2019-09-28 20:50:56 +02:00
static void setValueBound ( ValueFlow : : Value & value , const Token * tok , bool invert )
2019-09-20 15:06:37 +02:00
{
if ( Token : : Match ( tok , " <|<= " ) ) {
setValueUpperBound ( value , ! invert ) ;
} else if ( Token : : Match ( tok , " >|>= " ) ) {
setValueUpperBound ( value , invert ) ;
}
}
2021-08-19 22:01:55 +02:00
static void setConditionalValues ( const Token * tok ,
bool lhs ,
2019-09-03 17:15:58 +02:00
MathLib : : bigint value ,
2021-08-19 22:01:55 +02:00
ValueFlow : : Value & true_value ,
ValueFlow : : Value & false_value )
2019-09-03 17:15:58 +02:00
{
if ( Token : : Match ( tok , " ==|!=|>=|<= " ) ) {
true_value = ValueFlow : : Value { tok , value } ;
2019-09-20 15:06:37 +02:00
const char * greaterThan = " >= " ;
const char * lessThan = " <= " ;
2021-08-19 22:01:55 +02:00
if ( lhs )
2019-09-20 15:06:37 +02:00
std : : swap ( greaterThan , lessThan ) ;
2020-05-26 20:13:56 +02:00
if ( Token : : simpleMatch ( tok , greaterThan , strlen ( greaterThan ) ) ) {
2019-09-20 15:06:37 +02:00
false_value = ValueFlow : : Value { tok , value - 1 } ;
2020-05-26 20:13:56 +02:00
} else if ( Token : : simpleMatch ( tok , lessThan , strlen ( lessThan ) ) ) {
2019-09-20 15:06:37 +02:00
false_value = ValueFlow : : Value { tok , value + 1 } ;
} else {
false_value = ValueFlow : : Value { tok , value } ;
}
} else {
const char * greaterThan = " > " ;
const char * lessThan = " < " ;
2021-08-19 22:01:55 +02:00
if ( lhs )
2019-09-20 15:06:37 +02:00
std : : swap ( greaterThan , lessThan ) ;
2020-05-26 20:13:56 +02:00
if ( Token : : simpleMatch ( tok , greaterThan , strlen ( greaterThan ) ) ) {
2019-09-20 15:06:37 +02:00
true_value = ValueFlow : : Value { tok , value + 1 } ;
false_value = ValueFlow : : Value { tok , value } ;
2020-05-26 20:13:56 +02:00
} else if ( Token : : simpleMatch ( tok , lessThan , strlen ( lessThan ) ) ) {
2019-09-20 15:06:37 +02:00
true_value = ValueFlow : : Value { tok , value - 1 } ;
false_value = ValueFlow : : Value { tok , value } ;
}
}
2021-08-19 22:01:55 +02:00
setValueBound ( true_value , tok , lhs ) ;
setValueBound ( false_value , tok , ! lhs ) ;
2019-09-20 15:06:37 +02:00
}
static bool isSaturated ( MathLib : : bigint value )
{
return value = = std : : numeric_limits < MathLib : : bigint > : : max ( ) | | value = = std : : numeric_limits < MathLib : : bigint > : : min ( ) ;
2019-09-03 17:15:58 +02:00
}
2021-01-11 08:00:13 +01:00
const Token * parseCompareInt ( const Token * tok , ValueFlow : : Value & true_value , ValueFlow : : Value & false_value , const std : : function < std : : vector < MathLib : : bigint > ( const Token * ) > & evaluate )
2019-09-03 17:15:58 +02:00
{
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
return nullptr ;
2020-05-19 21:03:28 +02:00
if ( tok - > isComparisonOp ( ) ) {
2021-01-11 08:00:13 +01:00
std : : vector < MathLib : : bigint > value1 = evaluate ( tok - > astOperand1 ( ) ) ;
std : : vector < MathLib : : bigint > value2 = evaluate ( tok - > astOperand2 ( ) ) ;
2021-06-09 09:20:43 +02:00
if ( ! value1 . empty ( ) & & ! value2 . empty ( ) ) {
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) )
value2 . clear ( ) ;
if ( tok - > astOperand2 ( ) - > hasKnownIntValue ( ) )
value1 . clear ( ) ;
}
2021-01-11 08:00:13 +01:00
if ( ! value1 . empty ( ) ) {
2022-04-07 10:42:57 +02:00
if ( isSaturated ( value1 . front ( ) ) | | astIsFloat ( tok - > astOperand2 ( ) , /*unknown*/ false ) )
2019-09-20 15:06:37 +02:00
return nullptr ;
2021-01-11 08:00:13 +01:00
setConditionalValues ( tok , true , value1 . front ( ) , true_value , false_value ) ;
2019-09-03 17:15:58 +02:00
return tok - > astOperand2 ( ) ;
2021-01-11 08:00:13 +01:00
} else if ( ! value2 . empty ( ) ) {
2022-04-07 10:42:57 +02:00
if ( isSaturated ( value2 . front ( ) ) | | astIsFloat ( tok - > astOperand1 ( ) , /*unknown*/ false ) )
2019-09-20 15:06:37 +02:00
return nullptr ;
2021-01-11 08:00:13 +01:00
setConditionalValues ( tok , false , value2 . front ( ) , true_value , false_value ) ;
2019-09-03 17:15:58 +02:00
return tok - > astOperand1 ( ) ;
}
}
return nullptr ;
}
2021-01-11 08:00:13 +01:00
const Token * parseCompareInt ( const Token * tok , ValueFlow : : Value & true_value , ValueFlow : : Value & false_value )
{
return parseCompareInt ( tok , true_value , false_value , [ ] ( const Token * t ) - > std : : vector < MathLib : : bigint > {
if ( t - > hasKnownIntValue ( ) )
return { t - > values ( ) . front ( ) . intvalue } ;
return std : : vector < MathLib : : bigint > { } ;
} ) ;
}
2022-04-23 06:19:07 +02:00
static bool isInConstructorList ( const Token * tok )
{
if ( ! tok )
return false ;
if ( ! astIsRHS ( tok ) )
return false ;
const Token * parent = tok - > astParent ( ) ;
if ( ! Token : : Match ( parent , " {|( " ) )
return false ;
if ( ! Token : : Match ( parent - > previous ( ) , " %var% {|( " ) )
return false ;
if ( ! parent - > astOperand1 ( ) | | ! parent - > astOperand2 ( ) )
return false ;
do {
parent = parent - > astParent ( ) ;
} while ( Token : : simpleMatch ( parent , " , " ) ) ;
return Token : : simpleMatch ( parent , " : " ) & & ! Token : : simpleMatch ( parent - > astParent ( ) , " ? " ) ;
}
static std : : vector < ValueType > getParentValueTypes ( const Token * tok ,
const Settings * settings = nullptr ,
const Token * * parent = nullptr )
{
if ( ! tok )
return { } ;
if ( ! tok - > astParent ( ) )
return { } ;
if ( isInConstructorList ( tok ) ) {
if ( parent )
* parent = tok - > astParent ( ) - > astOperand1 ( ) ;
if ( tok - > astParent ( ) - > astOperand1 ( ) - > valueType ( ) )
return { * tok - > astParent ( ) - > astOperand1 ( ) - > valueType ( ) } ;
return { } ;
} else if ( Token : : Match ( tok - > astParent ( ) , " (| { | , " )) {
int argn = - 1 ;
const Token * ftok = getTokenArgumentFunction ( tok , argn ) ;
2022-05-13 06:51:07 +02:00
const Token * typeTok = nullptr ;
if ( ftok & & argn > = 0 ) {
if ( ftok - > function ( ) ) {
std : : vector < ValueType > result ;
std : : vector < const Variable * > argsVars = getArgumentVars ( ftok , argn ) ;
const Token * nameTok = nullptr ;
for ( const Variable * var : getArgumentVars ( ftok , argn ) ) {
if ( ! var )
continue ;
if ( ! var - > valueType ( ) )
continue ;
nameTok = var - > nameToken ( ) ;
result . push_back ( * var - > valueType ( ) ) ;
}
if ( result . size ( ) = = 1 & & nameTok & & parent ) {
* parent = nameTok ;
}
return result ;
} else if ( const Type * t = Token : : typeOf ( ftok , & typeTok ) ) {
if ( astIsPointer ( typeTok ) )
return { * typeTok - > valueType ( ) } ;
const Scope * scope = t - > classScope ;
// Check for aggregate constructors
if ( scope & & scope - > numConstructors = = 0 & & t - > derivedFrom . empty ( ) & &
( t - > isClassType ( ) | | t - > isStructType ( ) ) & & numberOfArguments ( ftok ) < scope - > varlist . size ( ) ) {
assert ( argn < scope - > varlist . size ( ) ) ;
auto it = std : : next ( scope - > varlist . begin ( ) , argn ) ;
if ( it - > valueType ( ) )
return { * it - > valueType ( ) } ;
}
2022-04-23 06:19:07 +02:00
}
}
}
if ( settings & & Token : : Match ( tok - > astParent ( ) - > tokAt ( - 2 ) , " . push_back|push_front|insert|push ( " ) & &
astIsContainer ( tok - > astParent ( ) - > tokAt ( - 2 ) - > astOperand1 ( ) ) ) {
const Token * contTok = tok - > astParent ( ) - > tokAt ( - 2 ) - > astOperand1 ( ) ;
const ValueType * vtCont = contTok - > valueType ( ) ;
if ( ! vtCont - > containerTypeToken )
return { } ;
ValueType vtParent = ValueType : : parseDecl ( vtCont - > containerTypeToken , settings ) ;
return { std : : move ( vtParent ) } ;
}
if ( Token : : Match ( tok - > astParent ( ) , " return|(|{|%assign% " ) & & parent ) {
* parent = tok - > astParent ( ) ;
}
if ( tok - > astParent ( ) - > valueType ( ) )
return { * tok - > astParent ( ) - > valueType ( ) } ;
return { } ;
}
2018-11-11 08:04:01 +01:00
static bool isEscapeScope ( const Token * tok , TokenList * tokenlist , bool unknown = false )
2018-11-07 06:49:07 +01:00
{
if ( ! Token : : simpleMatch ( tok , " { " ) )
return false ;
2019-08-26 06:56:29 +02:00
// TODO this search for termTok in all subscopes. It should check the end of the scope.
2018-11-07 06:49:07 +01:00
const Token * termTok = Token : : findmatch ( tok , " return|continue|break|throw|goto " , tok - > link ( ) ) ;
2018-11-12 06:32:02 +01:00
if ( termTok & & termTok - > scope ( ) = = tok - > scope ( ) )
2018-11-11 08:04:01 +01:00
return true ;
std : : string unknownFunction ;
2018-11-12 06:32:02 +01:00
if ( tokenlist & & tokenlist - > getSettings ( ) - > library . isScopeNoReturn ( tok - > link ( ) , & unknownFunction ) )
2018-11-11 08:04:01 +01:00
return unknownFunction . empty ( ) | | unknown ;
return false ;
2018-11-07 06:49:07 +01:00
}
2019-07-15 13:47:17 +02:00
static ValueFlow : : Value castValue ( ValueFlow : : Value value , const ValueType : : Sign sign , nonneg int bit )
2016-12-20 19:32:21 +01:00
{
if ( value . isFloatValue ( ) ) {
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : INT ;
2017-11-03 14:05:23 +01:00
if ( value . floatValue > = std : : numeric_limits < int > : : min ( ) & & value . floatValue < = std : : numeric_limits < int > : : max ( ) ) {
value . intvalue = value . floatValue ;
} else { // don't perform UB
value . intvalue = 0 ;
}
2016-12-20 19:32:21 +01:00
}
2017-10-20 17:31:58 +02:00
if ( bit < MathLib : : bigint_bits ) {
2017-10-21 22:08:34 +02:00
const MathLib : : biguint one = 1 ;
value . intvalue & = ( one < < bit ) - 1 ;
if ( sign = = ValueType : : Sign : : SIGNED & & value . intvalue & ( one < < ( bit - 1 ) ) ) {
value . intvalue | = ~ ( ( one < < bit ) - 1ULL ) ;
2016-12-20 19:32:21 +01:00
}
}
return value ;
}
2021-09-09 07:49:56 +02:00
static bool isNumeric ( const ValueFlow : : Value & value ) {
return value . isIntValue ( ) | | value . isFloatValue ( ) ;
}
2018-11-02 18:28:32 +01:00
static void combineValueProperties ( const ValueFlow : : Value & value1 , const ValueFlow : : Value & value2 , ValueFlow : : Value * result )
{
if ( value1 . isKnown ( ) & & value2 . isKnown ( ) )
result - > setKnown ( ) ;
2019-09-20 15:06:37 +02:00
else if ( value1 . isImpossible ( ) | | value2 . isImpossible ( ) )
result - > setImpossible ( ) ;
2018-11-02 18:28:32 +01:00
else if ( value1 . isInconclusive ( ) | | value2 . isInconclusive ( ) )
result - > setInconclusive ( ) ;
else
result - > setPossible ( ) ;
2021-09-09 07:49:56 +02:00
if ( value1 . isSymbolicValue ( ) ) {
result - > valueType = value1 . valueType ;
result - > tokvalue = value1 . tokvalue ;
}
if ( value2 . isSymbolicValue ( ) ) {
result - > valueType = value2 . valueType ;
result - > tokvalue = value2 . tokvalue ;
}
2020-08-17 23:36:45 +02:00
if ( value1 . isIteratorValue ( ) )
result - > valueType = value1 . valueType ;
if ( value2 . isIteratorValue ( ) )
result - > valueType = value2 . valueType ;
2018-11-02 18:28:32 +01:00
result - > condition = value1 . condition ? value1 . condition : value2 . condition ;
2021-01-27 19:49:13 +01:00
result - > varId = ( value1 . varId ! = 0 ) ? value1 . varId : value2 . varId ;
2018-11-02 18:28:32 +01:00
result - > varvalue = ( result - > varId = = value1 . varId ) ? value1 . varvalue : value2 . varvalue ;
result - > errorPath = ( value1 . errorPath . empty ( ) ? value2 : value1 ) . errorPath ;
2019-07-25 17:19:51 +02:00
result - > safe = value1 . safe | | value2 . safe ;
2019-12-26 15:47:53 +01:00
if ( value1 . bound = = ValueFlow : : Value : : Bound : : Point | | value2 . bound = = ValueFlow : : Value : : Bound : : Point ) {
if ( value1 . bound = = ValueFlow : : Value : : Bound : : Upper | | value2 . bound = = ValueFlow : : Value : : Bound : : Upper )
result - > bound = ValueFlow : : Value : : Bound : : Upper ;
if ( value1 . bound = = ValueFlow : : Value : : Bound : : Lower | | value2 . bound = = ValueFlow : : Value : : Bound : : Lower )
result - > bound = ValueFlow : : Value : : Bound : : Lower ;
}
2020-02-17 10:31:08 +01:00
if ( value1 . path ! = value2 . path )
result - > path = - 1 ;
else
result - > path = value1 . path ;
2018-11-02 18:28:32 +01:00
}
2019-01-06 12:21:47 +01:00
static const Token * getCastTypeStartToken ( const Token * parent )
{
// TODO: This might be a generic utility function?
2021-11-07 18:19:56 +01:00
if ( ! Token : : Match ( parent , " {|( " ) )
return nullptr ;
// Functional cast
if ( parent - > isBinaryOp ( ) & & Token : : Match ( parent - > astOperand1 ( ) , " %type% (|{ " ) & &
parent - > astOperand1 ( ) - > tokType ( ) = = Token : : eType & & astIsPrimitive ( parent ) )
return parent - > astOperand1 ( ) ;
if ( parent - > str ( ) ! = " ( " )
2019-01-06 12:21:47 +01:00
return nullptr ;
if ( ! parent - > astOperand2 ( ) & & Token : : Match ( parent , " ( %name% " ) )
return parent - > next ( ) ;
if ( parent - > astOperand2 ( ) & & Token : : Match ( parent - > astOperand1 ( ) , " const_cast|dynamic_cast|reinterpret_cast|static_cast < " ) )
return parent - > astOperand1 ( ) - > tokAt ( 2 ) ;
return nullptr ;
}
2021-12-14 07:30:57 +01:00
// does the operation cause a loss of information?
static bool isNonInvertibleOperation ( const Token * tok )
{
2022-04-14 18:13:04 +02:00
return ! Token : : Match ( tok , " +|- " ) ;
2021-12-14 07:30:57 +01:00
}
2020-08-17 23:36:45 +02:00
static bool isComputableValue ( const Token * parent , const ValueFlow : : Value & value )
{
2021-12-14 07:30:57 +01:00
const bool noninvertible = isNonInvertibleOperation ( parent ) ;
2020-08-17 23:36:45 +02:00
if ( noninvertible & & value . isImpossible ( ) )
return false ;
if ( ! value . isIntValue ( ) & & ! value . isFloatValue ( ) & & ! value . isTokValue ( ) & & ! value . isIteratorValue ( ) )
return false ;
if ( value . isIteratorValue ( ) & & ! Token : : Match ( parent , " +|- " ) )
return false ;
if ( value . isTokValue ( ) & & ( ! parent - > isComparisonOp ( ) | | value . tokvalue - > tokType ( ) ! = Token : : eString ) )
return false ;
return true ;
}
2018-12-27 21:33:01 +01:00
/** Set token value for cast */
static void setTokenValueCast ( Token * parent , const ValueType & valueType , const ValueFlow : : Value & value , const Settings * settings ) ;
2021-09-09 07:49:56 +02:00
static bool isCompatibleValueTypes ( ValueFlow : : Value : : ValueType x , ValueFlow : : Value : : ValueType y )
{
static const std : : unordered_map < ValueFlow : : Value : : ValueType ,
std : : unordered_set < ValueFlow : : Value : : ValueType , EnumClassHash > ,
EnumClassHash >
compatibleTypes = {
{ ValueFlow : : Value : : ValueType : : INT ,
{ ValueFlow : : Value : : ValueType : : FLOAT ,
ValueFlow : : Value : : ValueType : : SYMBOLIC ,
ValueFlow : : Value : : ValueType : : TOK } } ,
{ ValueFlow : : Value : : ValueType : : FLOAT , { ValueFlow : : Value : : ValueType : : INT } } ,
{ ValueFlow : : Value : : ValueType : : TOK , { ValueFlow : : Value : : ValueType : : INT } } ,
{ ValueFlow : : Value : : ValueType : : ITERATOR_START , { ValueFlow : : Value : : ValueType : : INT } } ,
{ ValueFlow : : Value : : ValueType : : ITERATOR_END , { ValueFlow : : Value : : ValueType : : INT } } ,
} ;
if ( x = = y )
return true ;
auto it = compatibleTypes . find ( x ) ;
if ( it = = compatibleTypes . end ( ) )
return false ;
return it - > second . count ( y ) > 0 ;
}
2021-05-24 08:28:21 +02:00
static bool isCompatibleValues ( const ValueFlow : : Value & value1 , const ValueFlow : : Value & value2 )
{
2021-09-09 07:49:56 +02:00
if ( value1 . isSymbolicValue ( ) & & value2 . isSymbolicValue ( ) & & value1 . tokvalue - > exprId ( ) ! = value2 . tokvalue - > exprId ( ) )
return false ;
if ( ! isCompatibleValueTypes ( value1 . valueType , value2 . valueType ) )
return false ;
2021-05-24 08:28:21 +02:00
if ( value1 . isKnown ( ) | | value2 . isKnown ( ) )
return true ;
if ( value1 . isImpossible ( ) | | value2 . isImpossible ( ) )
return false ;
if ( value1 . varId = = 0 | | value2 . varId = = 0 )
return true ;
if ( value1 . varId = = value2 . varId & & value1 . varvalue = = value2 . varvalue & & value1 . isIntValue ( ) & & value2 . isIntValue ( ) )
return true ;
return false ;
}
2021-06-04 17:17:41 +02:00
static ValueFlow : : Value truncateImplicitConversion ( Token * parent , const ValueFlow : : Value & value , const Settings * settings )
{
if ( ! value . isIntValue ( ) & & ! value . isFloatValue ( ) )
return value ;
if ( ! parent )
return value ;
if ( ! parent - > isBinaryOp ( ) )
return value ;
if ( ! parent - > isConstOp ( ) )
return value ;
if ( ! astIsIntegral ( parent - > astOperand1 ( ) , false ) )
return value ;
if ( ! astIsIntegral ( parent - > astOperand2 ( ) , false ) )
return value ;
const ValueType * vt1 = parent - > astOperand1 ( ) - > valueType ( ) ;
const ValueType * vt2 = parent - > astOperand2 ( ) - > valueType ( ) ;
// If the sign is the same there is no truncation
if ( vt1 - > sign = = vt2 - > sign )
return value ;
size_t n1 = ValueFlow : : getSizeOf ( * vt1 , settings ) ;
size_t n2 = ValueFlow : : getSizeOf ( * vt2 , settings ) ;
ValueType : : Sign sign = ValueType : : Sign : : UNSIGNED ;
if ( n1 < n2 )
sign = vt2 - > sign ;
else if ( n1 > n2 )
sign = vt1 - > sign ;
ValueFlow : : Value v = castValue ( value , sign , std : : max ( n1 , n2 ) * 8 ) ;
v . wideintvalue = value . intvalue ;
return v ;
}
2015-07-17 09:46:31 +02:00
/** set ValueFlow value and perform calculations if possible */
2021-06-04 17:17:41 +02:00
static void setTokenValue ( Token * tok , ValueFlow : : Value value , const Settings * settings )
2015-07-17 09:46:31 +02:00
{
2021-03-30 14:02:28 +02:00
// Skip setting values that are too big since its ambiguous
if ( ! value . isImpossible ( ) & & value . isIntValue ( ) & & value . intvalue < 0 & & astIsUnsigned ( tok ) & &
ValueFlow : : getSizeOf ( * tok - > valueType ( ) , settings ) > = sizeof ( MathLib : : bigint ) )
return ;
2021-06-04 17:17:41 +02:00
if ( ! value . isImpossible ( ) & & value . isIntValue ( ) )
value = truncateImplicitConversion ( tok - > astParent ( ) , value , settings ) ;
2017-03-27 18:48:34 +02:00
if ( ! tok - > addValue ( value ) )
2015-07-17 09:46:31 +02:00
return ;
2020-02-17 10:31:08 +01:00
if ( value . path < 0 )
return ;
2019-06-16 10:09:38 +02:00
Token * parent = tok - > astParent ( ) ;
2015-02-24 15:57:39 +01:00
if ( ! parent )
return ;
2014-01-20 22:26:55 +01:00
2021-01-13 12:36:26 +01:00
if ( Token : : simpleMatch ( parent , " = " ) & & astIsRHS ( tok ) & & ! value . isLifetimeValue ( ) ) {
setTokenValue ( parent , value , settings ) ;
return ;
}
2018-11-02 18:28:32 +01:00
if ( value . isContainerSizeValue ( ) ) {
// .empty, .size, +"abc", +'a'
2021-01-10 13:30:00 +01:00
if ( Token : : Match ( parent , " +|==|!= " ) & & parent - > astOperand1 ( ) & & parent - > astOperand2 ( ) ) {
2018-11-02 18:28:32 +01:00
for ( const ValueFlow : : Value & value1 : parent - > astOperand1 ( ) - > values ( ) ) {
2021-02-03 10:21:47 +01:00
if ( value1 . isImpossible ( ) )
continue ;
2018-11-02 18:28:32 +01:00
for ( const ValueFlow : : Value & value2 : parent - > astOperand2 ( ) - > values ( ) ) {
2021-02-03 10:21:47 +01:00
if ( value2 . isImpossible ( ) )
continue ;
2020-02-17 10:31:08 +01:00
if ( value1 . path ! = value2 . path )
continue ;
2018-11-02 18:28:32 +01:00
ValueFlow : : Value result ;
2021-01-10 13:30:00 +01:00
if ( Token : : Match ( parent , " %comp% " ) )
result . valueType = ValueFlow : : Value : : ValueType : : INT ;
else
result . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
2018-11-02 18:28:32 +01:00
if ( value1 . isContainerSizeValue ( ) & & value2 . isContainerSizeValue ( ) )
2021-01-10 13:30:00 +01:00
result . intvalue = calculate ( parent - > str ( ) , value1 . intvalue , value2 . intvalue ) ;
2018-11-02 18:28:32 +01:00
else if ( value1 . isContainerSizeValue ( ) & & value2 . isTokValue ( ) & & value2 . tokvalue - > tokType ( ) = = Token : : eString )
2021-01-10 15:27:42 +01:00
result . intvalue = calculate ( parent - > str ( ) , value1 . intvalue , MathLib : : bigint ( Token : : getStrLength ( value2 . tokvalue ) ) ) ;
2018-11-02 18:28:32 +01:00
else if ( value2 . isContainerSizeValue ( ) & & value1 . isTokValue ( ) & & value1 . tokvalue - > tokType ( ) = = Token : : eString )
2021-01-10 15:27:42 +01:00
result . intvalue = calculate ( parent - > str ( ) , MathLib : : bigint ( Token : : getStrLength ( value1 . tokvalue ) ) , value2 . intvalue ) ;
2018-11-02 18:28:32 +01:00
else
continue ;
combineValueProperties ( value1 , value2 , & result ) ;
2021-01-10 13:30:00 +01:00
if ( Token : : simpleMatch ( parent , " == " ) & & result . intvalue )
continue ;
if ( Token : : simpleMatch ( parent , " != " ) & & ! result . intvalue )
continue ;
2018-11-02 18:28:32 +01:00
setTokenValue ( parent , result , settings ) ;
}
}
}
2021-01-10 13:30:00 +01:00
else if ( Token : : Match ( parent , " . %name% ( " ) & & parent - > astParent ( ) = = parent - > tokAt ( 2 ) & &
parent - > astOperand1 ( ) & & parent - > astOperand1 ( ) - > valueType ( ) ) {
2022-01-26 06:42:41 +01:00
const Library : : Container * c = getLibraryContainer ( parent - > astOperand1 ( ) ) ;
2018-11-02 18:28:32 +01:00
const Library : : Container : : Yield yields = c ? c - > getYield ( parent - > strAt ( 1 ) ) : Library : : Container : : Yield : : NO_YIELD ;
if ( yields = = Library : : Container : : Yield : : SIZE ) {
ValueFlow : : Value v ( value ) ;
v . valueType = ValueFlow : : Value : : ValueType : : INT ;
2019-06-16 10:09:38 +02:00
setTokenValue ( parent - > astParent ( ) , v , settings ) ;
2018-11-02 18:28:32 +01:00
} else if ( yields = = Library : : Container : : Yield : : EMPTY ) {
ValueFlow : : Value v ( value ) ;
v . intvalue = ! v . intvalue ;
v . valueType = ValueFlow : : Value : : ValueType : : INT ;
2019-06-16 10:09:38 +02:00
setTokenValue ( parent - > astParent ( ) , v , settings ) ;
2018-11-02 18:28:32 +01:00
}
2021-08-23 09:03:48 +02:00
} else if ( Token : : Match ( parent - > previous ( ) , " %name% ( " ) ) {
if ( const Library : : Function * f = settings - > library . getFunction ( parent - > previous ( ) ) ) {
if ( f - > containerYield = = Library : : Container : : Yield : : SIZE ) {
ValueFlow : : Value v ( value ) ;
v . valueType = ValueFlow : : Value : : ValueType : : INT ;
setTokenValue ( parent , v , settings ) ;
} else if ( f - > containerYield = = Library : : Container : : Yield : : EMPTY ) {
ValueFlow : : Value v ( value ) ;
v . intvalue = ! v . intvalue ;
v . valueType = ValueFlow : : Value : : ValueType : : INT ;
setTokenValue ( parent , v , settings ) ;
}
}
2018-11-02 18:28:32 +01:00
}
return ;
}
2018-11-12 10:08:57 +01:00
if ( value . isLifetimeValue ( ) ) {
2019-10-05 16:32:20 +02:00
if ( ! isLifetimeBorrowed ( parent , settings ) )
return ;
2019-07-16 10:51:07 +02:00
if ( value . lifetimeKind = = ValueFlow : : Value : : LifetimeKind : : Iterator & & astIsIterator ( parent ) ) {
2018-11-12 10:08:17 +01:00
setTokenValue ( parent , value , settings ) ;
2021-08-14 21:37:17 +02:00
} else if ( astIsPointer ( tok ) & & astIsPointer ( parent ) & & ! parent - > isUnaryOp ( " * " ) & &
2021-06-09 09:21:03 +02:00
( parent - > isArithmeticalOp ( ) | | parent - > isCast ( ) ) ) {
2018-11-12 10:08:17 +01:00
setTokenValue ( parent , value , settings ) ;
}
return ;
}
2019-08-15 10:44:55 +02:00
if ( value . isUninitValue ( ) ) {
2021-10-21 14:44:48 +02:00
if ( Token : : Match ( tok , " . %var% " ) )
setTokenValue ( tok - > next ( ) , value , settings ) ;
2022-04-11 07:23:44 +02:00
if ( parent - > isCast ( ) ) {
setTokenValue ( parent , value , settings ) ;
return ;
}
2019-08-17 07:36:41 +02:00
ValueFlow : : Value pvalue = value ;
2021-11-06 19:06:07 +01:00
if ( ! value . subexpressions . empty ( ) & & Token : : Match ( parent , " . %var% " ) ) {
if ( contains ( value . subexpressions , parent - > next ( ) - > str ( ) ) )
2021-10-30 07:43:37 +02:00
pvalue . subexpressions . clear ( ) ;
2021-11-06 19:06:07 +01:00
else
return ;
2021-10-30 07:43:37 +02:00
}
2019-08-17 07:36:41 +02:00
if ( parent - > isUnaryOp ( " & " ) ) {
pvalue . indirect + + ;
setTokenValue ( parent , pvalue , settings ) ;
} else if ( Token : : Match ( parent , " . %var% " ) & & parent - > astOperand1 ( ) = = tok ) {
2019-08-23 06:23:21 +02:00
if ( parent - > originalName ( ) = = " -> " & & pvalue . indirect > 0 )
2019-08-17 07:36:41 +02:00
pvalue . indirect - - ;
setTokenValue ( parent - > astOperand2 ( ) , pvalue , settings ) ;
2019-08-23 06:23:21 +02:00
} else if ( Token : : Match ( parent - > astParent ( ) , " . %var% " ) & & parent - > astParent ( ) - > astOperand1 ( ) = = parent ) {
if ( parent - > astParent ( ) - > originalName ( ) = = " -> " & & pvalue . indirect > 0 )
pvalue . indirect - - ;
setTokenValue ( parent - > astParent ( ) - > astOperand2 ( ) , pvalue , settings ) ;
2019-08-17 07:36:41 +02:00
} else if ( parent - > isUnaryOp ( " * " ) & & pvalue . indirect > 0 ) {
pvalue . indirect - - ;
setTokenValue ( parent , pvalue , settings ) ;
}
2019-08-15 10:44:55 +02:00
return ;
}
2018-12-27 21:33:01 +01:00
// cast..
2019-01-06 12:21:47 +01:00
if ( const Token * castType = getCastTypeStartToken ( parent ) ) {
2021-05-23 10:20:29 +02:00
if ( ( ( tok - > valueType ( ) = = nullptr & & value . isImpossible ( ) ) | | astIsPointer ( tok ) ) & &
2021-12-04 08:54:24 +01:00
contains ( { ValueFlow : : Value : : ValueType : : INT , ValueFlow : : Value : : ValueType : : SYMBOLIC } , value . valueType ) & &
2019-09-20 15:06:37 +02:00
Token : : simpleMatch ( parent - > astOperand1 ( ) , " dynamic_cast " ) )
return ;
2019-01-06 12:21:47 +01:00
const ValueType & valueType = ValueType : : parseDecl ( castType , settings ) ;
2021-05-23 10:20:29 +02:00
if ( value . isImpossible ( ) & & value . isIntValue ( ) & & value . intvalue < 0 & & astIsUnsigned ( tok ) & &
valueType . sign = = ValueType : : SIGNED & & tok - > valueType ( ) & &
ValueFlow : : getSizeOf ( * tok - > valueType ( ) , settings ) > = ValueFlow : : getSizeOf ( valueType , settings ) )
return ;
2018-12-27 21:33:01 +01:00
setTokenValueCast ( parent , valueType , value , settings ) ;
2016-12-20 19:32:21 +01:00
}
2014-01-20 22:26:55 +01:00
2016-12-20 19:32:21 +01:00
else if ( parent - > str ( ) = = " : " ) {
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , value , settings ) ;
2015-07-02 20:11:27 +02:00
}
2016-08-21 11:06:48 +02:00
else if ( parent - > str ( ) = = " ? " & & tok - > str ( ) = = " : " & & tok = = parent - > astOperand2 ( ) & & parent - > astOperand1 ( ) ) {
2016-08-15 14:19:35 +02:00
// is condition always true/false?
2018-07-14 13:09:53 +02:00
if ( parent - > astOperand1 ( ) - > hasKnownValue ( ) ) {
2017-03-27 18:48:34 +02:00
const ValueFlow : : Value & condvalue = parent - > astOperand1 ( ) - > values ( ) . front ( ) ;
2016-11-13 22:33:39 +01:00
const bool cond ( condvalue . isTokValue ( ) | | ( condvalue . isIntValue ( ) & & condvalue . intvalue ! = 0 ) ) ;
2016-08-17 18:44:41 +02:00
if ( cond & & ! tok - > astOperand1 ( ) ) { // true condition, no second operator
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , condvalue , settings ) ;
2016-08-17 18:44:41 +02:00
} else {
const Token * op = cond ? tok - > astOperand1 ( ) : tok - > astOperand2 ( ) ;
2016-11-06 14:20:58 +01:00
if ( ! op ) // #7769 segmentation fault at setTokenValue()
return ;
2017-03-27 18:48:34 +02:00
const std : : list < ValueFlow : : Value > & values = op - > values ( ) ;
2016-08-17 18:44:41 +02:00
if ( std : : find ( values . begin ( ) , values . end ( ) , value ) ! = values . end ( ) )
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , value , settings ) ;
2016-08-17 18:44:41 +02:00
}
2021-09-04 19:55:36 +02:00
} else if ( ! value . isImpossible ( ) ) {
2016-08-15 14:19:35 +02:00
// is condition only depending on 1 variable?
2021-09-18 22:23:05 +02:00
// cppcheck-suppress[variableScope] #8541
2021-01-27 19:49:13 +01:00
nonneg int varId = 0 ;
2018-11-23 19:16:19 +01:00
bool ret = false ;
visitAstNodes ( parent - > astOperand1 ( ) ,
2021-08-07 20:51:18 +02:00
[ & ] ( const Token * t ) {
2016-08-15 14:19:35 +02:00
if ( t - > varId ( ) ) {
2021-01-27 19:49:13 +01:00
if ( varId > 0 | | value . varId ! = 0 )
2018-11-23 19:16:19 +01:00
ret = true ;
2016-08-15 14:19:35 +02:00
varId = t - > varId ( ) ;
} else if ( t - > str ( ) = = " ( " & & Token : : Match ( t - > previous ( ) , " %name% " ) )
2018-11-23 19:16:19 +01:00
ret = true ; // function call
return ret ? ChildrenToVisit : : done : ChildrenToVisit : : op1_and_op2 ;
} ) ;
if ( ret )
return ;
2015-07-02 20:11:27 +02:00
2016-08-15 14:19:35 +02:00
ValueFlow : : Value v ( value ) ;
v . conditional = true ;
v . changeKnownToPossible ( ) ;
2015-07-17 15:22:24 +02:00
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , v , settings ) ;
2016-08-15 14:19:35 +02:00
}
2015-07-02 20:11:27 +02:00
}
2020-06-19 22:19:03 +02:00
else if ( parent - > str ( ) = = " ? " & & value . isIntValue ( ) & & tok = = parent - > astOperand1 ( ) & & value . isKnown ( ) & &
parent - > astOperand2 ( ) & & parent - > astOperand2 ( ) - > astOperand1 ( ) & & parent - > astOperand2 ( ) - > astOperand2 ( ) ) {
const std : : list < ValueFlow : : Value > & values = ( value . intvalue = = 0
? parent - > astOperand2 ( ) - > astOperand2 ( ) - > values ( )
: parent - > astOperand2 ( ) - > astOperand1 ( ) - > values ( ) ) ;
for ( const ValueFlow : : Value & v : values )
setTokenValue ( parent , v , settings ) ;
}
2014-01-20 22:26:55 +01:00
// Calculations..
2016-10-17 13:05:19 +02:00
else if ( ( parent - > isArithmeticalOp ( ) | | parent - > isComparisonOp ( ) | | ( parent - > tokType ( ) = = Token : : eBitOp ) | | ( parent - > tokType ( ) = = Token : : eLogicalOp ) ) & &
2015-07-26 11:27:52 +02:00
parent - > astOperand1 ( ) & &
parent - > astOperand2 ( ) ) {
2016-01-26 10:07:42 +01:00
2021-12-14 07:30:57 +01:00
const bool noninvertible = isNonInvertibleOperation ( parent ) ;
2019-09-20 15:06:37 +02:00
2019-09-21 19:53:54 +02:00
// Skip operators with impossible values that are not invertible
2019-09-24 08:15:03 +02:00
if ( noninvertible & & value . isImpossible ( ) )
2019-09-21 19:53:54 +02:00
return ;
2016-01-26 10:07:42 +01:00
// known result when a operand is 0.
2022-04-30 09:35:51 +02:00
if ( Token : : Match ( parent , " [&*] " ) & & astIsIntegral ( parent , true ) & & value . isKnown ( ) & & value . isIntValue ( ) & &
value . intvalue = = 0 ) {
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , value , settings ) ;
2016-01-26 10:07:42 +01:00
return ;
}
2018-07-23 08:51:59 +02:00
// known result when a operand is true.
if ( Token : : simpleMatch ( parent , " && " ) & & value . isKnown ( ) & & value . isIntValue ( ) & & value . intvalue = = 0 ) {
setTokenValue ( parent , value , settings ) ;
return ;
}
// known result when a operand is false.
if ( Token : : simpleMatch ( parent , " || " ) & & value . isKnown ( ) & & value . isIntValue ( ) & & value . intvalue ! = 0 ) {
setTokenValue ( parent , value , settings ) ;
return ;
}
2016-01-26 10:07:42 +01:00
2018-04-24 18:02:36 +02:00
for ( const ValueFlow : : Value & value1 : parent - > astOperand1 ( ) - > values ( ) ) {
2020-08-17 23:36:45 +02:00
if ( ! isComputableValue ( parent , value1 ) )
2015-08-02 18:12:03 +02:00
continue ;
2018-04-24 18:02:36 +02:00
for ( const ValueFlow : : Value & value2 : parent - > astOperand2 ( ) - > values ( ) ) {
2020-02-17 10:31:08 +01:00
if ( value1 . path ! = value2 . path )
continue ;
2020-08-17 23:36:45 +02:00
if ( ! isComputableValue ( parent , value2 ) )
2019-09-24 08:15:03 +02:00
continue ;
2020-08-17 23:36:45 +02:00
if ( value1 . isIteratorValue ( ) & & value2 . isIteratorValue ( ) )
2015-08-02 18:12:03 +02:00
continue ;
2021-05-24 08:28:21 +02:00
if ( ! isCompatibleValues ( value1 , value2 ) )
continue ;
ValueFlow : : Value result ( 0 ) ;
combineValueProperties ( value1 , value2 , & result ) ;
2021-09-09 07:49:56 +02:00
if ( astIsFloat ( parent , false ) ) {
if ( ! result . isIntValue ( ) & & ! result . isFloatValue ( ) )
continue ;
result . valueType = ValueFlow : : Value : : ValueType : : FLOAT ;
}
const double floatValue1 = value1 . isFloatValue ( ) ? value1 . floatValue : value1 . intvalue ;
const double floatValue2 = value2 . isFloatValue ( ) ? value2 . floatValue : value2 . intvalue ;
const MathLib : : bigint intValue1 =
value1 . isFloatValue ( ) ? static_cast < MathLib : : bigint > ( value1 . floatValue ) : value1 . intvalue ;
const MathLib : : bigint intValue2 =
value2 . isFloatValue ( ) ? static_cast < MathLib : : bigint > ( value2 . floatValue ) : value2 . intvalue ;
if ( ( value1 . isFloatValue ( ) | | value2 . isFloatValue ( ) ) & & Token : : Match ( parent , " &|^|%|<<|>>|==|!=|%or% " ) )
2021-05-24 08:28:21 +02:00
continue ;
if ( Token : : Match ( parent , " ==|!= " ) ) {
if ( ( value1 . isIntValue ( ) & & value2 . isTokValue ( ) ) | | ( value1 . isTokValue ( ) & & value2 . isIntValue ( ) ) ) {
if ( parent - > str ( ) = = " == " )
result . intvalue = 0 ;
else if ( parent - > str ( ) = = " != " )
result . intvalue = 1 ;
} else if ( value1 . isIntValue ( ) & & value2 . isIntValue ( ) ) {
2021-09-09 07:49:56 +02:00
bool error = false ;
result . intvalue = calculate ( parent - > str ( ) , intValue1 , intValue2 , & error ) ;
if ( error )
continue ;
2021-05-24 08:28:21 +02:00
} else {
2021-01-10 13:30:00 +01:00
continue ;
2021-05-24 08:28:21 +02:00
}
setTokenValue ( parent , result , settings ) ;
} else if ( Token : : Match ( parent , " %op% " ) ) {
2021-09-09 07:49:56 +02:00
if ( Token : : Match ( parent , " %comp% " ) ) {
if ( ! result . isFloatValue ( ) & & ! value1 . isIntValue ( ) & & ! value2 . isIntValue ( ) )
continue ;
2021-05-24 08:28:21 +02:00
} else {
2021-09-09 07:49:56 +02:00
if ( value1 . isTokValue ( ) | | value2 . isTokValue ( ) )
break ;
2014-01-18 19:30:44 +01:00
}
2021-09-09 07:49:56 +02:00
bool error = false ;
if ( result . isFloatValue ( ) ) {
result . floatValue = calculate ( parent - > str ( ) , floatValue1 , floatValue2 , & error ) ;
} else {
result . intvalue = calculate ( parent - > str ( ) , intValue1 , intValue2 , & error ) ;
}
if ( error )
continue ;
2021-05-24 08:28:21 +02:00
// If the bound comes from the second value then invert the bound when subtracting
if ( Token : : simpleMatch ( parent , " - " ) & & value2 . bound = = result . bound & &
value2 . bound ! = ValueFlow : : Value : : Bound : : Point )
result . invertBound ( ) ;
setTokenValue ( parent , result , settings ) ;
2014-01-18 19:30:44 +01:00
}
}
}
}
2015-02-24 15:57:39 +01:00
2015-07-16 21:17:44 +02:00
// !
else if ( parent - > str ( ) = = " ! " ) {
2018-07-14 13:19:41 +02:00
for ( const ValueFlow : : Value & val : tok - > values ( ) ) {
if ( ! val . isIntValue ( ) )
2015-07-16 21:17:44 +02:00
continue ;
2021-06-03 07:26:36 +02:00
if ( val . isImpossible ( ) & & val . intvalue ! = 0 )
continue ;
2018-07-14 13:19:41 +02:00
ValueFlow : : Value v ( val ) ;
2015-07-16 21:17:44 +02:00
v . intvalue = ! v . intvalue ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , v , settings ) ;
}
}
// ~
else if ( parent - > str ( ) = = " ~ " ) {
2018-07-14 13:19:41 +02:00
for ( const ValueFlow : : Value & val : tok - > values ( ) ) {
if ( ! val . isIntValue ( ) )
2016-11-05 09:29:22 +01:00
continue ;
2018-07-14 13:19:41 +02:00
ValueFlow : : Value v ( val ) ;
2016-11-05 09:29:22 +01:00
v . intvalue = ~ v . intvalue ;
2019-07-15 13:47:17 +02:00
int bits = 0 ;
2016-11-05 09:59:48 +01:00
if ( settings & &
tok - > valueType ( ) & &
2016-11-05 09:29:22 +01:00
tok - > valueType ( ) - > sign = = ValueType : : Sign : : UNSIGNED & &
tok - > valueType ( ) - > pointer = = 0 ) {
if ( tok - > valueType ( ) - > type = = ValueType : : Type : : INT )
bits = settings - > int_bit ;
else if ( tok - > valueType ( ) - > type = = ValueType : : Type : : LONG )
bits = settings - > long_bit ;
}
2017-10-20 17:31:58 +02:00
if ( bits > 0 & & bits < MathLib : : bigint_bits )
v . intvalue & = ( ( ( MathLib : : biguint ) 1 ) < < bits ) - 1 ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , v , settings ) ;
2015-07-16 21:17:44 +02:00
}
}
2016-08-14 22:19:06 +02:00
// unary minus
2018-07-14 13:19:41 +02:00
else if ( parent - > isUnaryOp ( " - " ) ) {
for ( const ValueFlow : : Value & val : tok - > values ( ) ) {
if ( ! val . isIntValue ( ) & & ! val . isFloatValue ( ) )
2016-08-14 22:19:06 +02:00
continue ;
2018-07-14 13:19:41 +02:00
ValueFlow : : Value v ( val ) ;
2020-09-05 21:09:11 +02:00
if ( v . isIntValue ( ) ) {
if ( v . intvalue = = LLONG_MIN )
// Value can't be inverted
continue ;
2016-11-14 06:42:52 +01:00
v . intvalue = - v . intvalue ;
2020-09-06 07:47:17 +02:00
} else
2016-11-14 06:42:52 +01:00
v . floatValue = - v . floatValue ;
2019-12-26 15:47:53 +01:00
v . invertBound ( ) ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , v , settings ) ;
2016-08-14 22:19:06 +02:00
}
}
2020-08-24 13:10:36 +02:00
// increment
else if ( parent - > str ( ) = = " ++ " ) {
for ( const ValueFlow : : Value & val : tok - > values ( ) ) {
2021-10-11 19:10:37 +02:00
if ( ! val . isIntValue ( ) & & ! val . isFloatValue ( ) & & ! val . isSymbolicValue ( ) )
2020-08-24 13:10:36 +02:00
continue ;
ValueFlow : : Value v ( val ) ;
if ( parent = = tok - > previous ( ) ) {
2021-10-11 19:10:37 +02:00
if ( v . isIntValue ( ) | | v . isSymbolicValue ( ) )
2020-08-24 13:10:36 +02:00
v . intvalue = v . intvalue + 1 ;
else
v . floatValue = v . floatValue + 1.0 ;
}
setTokenValue ( parent , v , settings ) ;
}
}
// decrement
else if ( parent - > str ( ) = = " -- " ) {
for ( const ValueFlow : : Value & val : tok - > values ( ) ) {
2021-10-11 19:10:37 +02:00
if ( ! val . isIntValue ( ) & & ! val . isFloatValue ( ) & & ! val . isSymbolicValue ( ) )
2020-08-24 13:10:36 +02:00
continue ;
ValueFlow : : Value v ( val ) ;
if ( parent = = tok - > previous ( ) ) {
2021-10-11 19:10:37 +02:00
if ( v . isIntValue ( ) | | v . isSymbolicValue ( ) )
2020-08-24 13:10:36 +02:00
v . intvalue = v . intvalue - 1 ;
else
v . floatValue = v . floatValue - 1.0 ;
}
setTokenValue ( parent , v , settings ) ;
}
}
2015-02-24 15:57:39 +01:00
// Array element
2018-07-14 13:19:41 +02:00
else if ( parent - > str ( ) = = " [ " & & parent - > isBinaryOp ( ) ) {
for ( const ValueFlow : : Value & value1 : parent - > astOperand1 ( ) - > values ( ) ) {
if ( ! value1 . isTokValue ( ) )
2015-02-24 15:57:39 +01:00
continue ;
2018-07-14 13:19:41 +02:00
for ( const ValueFlow : : Value & value2 : parent - > astOperand2 ( ) - > values ( ) ) {
if ( ! value2 . isIntValue ( ) )
2015-02-24 15:57:39 +01:00
continue ;
2021-01-27 19:49:13 +01:00
if ( value1 . varId = = 0 | | value2 . varId = = 0 | |
2018-07-14 13:19:41 +02:00
( value1 . varId = = value2 . varId & & value1 . varvalue = = value2 . varvalue ) ) {
2015-02-24 15:57:39 +01:00
ValueFlow : : Value result ( 0 ) ;
2018-07-14 13:19:41 +02:00
result . condition = value1 . condition ? value1 . condition : value2 . condition ;
result . setInconclusive ( value1 . isInconclusive ( ) | value2 . isInconclusive ( ) ) ;
2021-01-27 19:49:13 +01:00
result . varId = ( value1 . varId ! = 0 ) ? value1 . varId : value2 . varId ;
2018-07-14 13:19:41 +02:00
result . varvalue = ( result . varId = = value1 . varId ) ? value1 . intvalue : value2 . intvalue ;
if ( value1 . valueKind = = value2 . valueKind )
result . valueKind = value1 . valueKind ;
if ( value1 . tokvalue - > tokType ( ) = = Token : : eString ) {
const std : : string s = value1 . tokvalue - > strValue ( ) ;
const MathLib : : bigint index = value2 . intvalue ;
2015-11-12 16:14:27 +01:00
if ( index = = s . size ( ) ) {
result . intvalue = 0 ;
2016-11-05 09:59:48 +01:00
setTokenValue ( parent , result , settings ) ;
2015-11-12 16:14:27 +01:00
} else if ( index > = 0 & & index < s . size ( ) ) {
2015-02-24 15:57:39 +01:00
result . intvalue = s [ index ] ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-02-24 15:57:39 +01:00
}
2018-07-14 13:19:41 +02:00
} else if ( value1 . tokvalue - > str ( ) = = " { " ) {
MathLib : : bigint index = value2 . intvalue ;
const Token * element = value1 . tokvalue - > next ( ) ;
2015-02-24 15:57:39 +01:00
while ( index > 0 & & element - > str ( ) ! = " } " ) {
if ( element - > str ( ) = = " , " )
- - index ;
if ( Token : : Match ( element , " [{}()[]] " ) )
break ;
element = element - > next ( ) ;
}
if ( Token : : Match ( element , " %num% [,}] " ) ) {
result . intvalue = MathLib : : toLongNumber ( element - > str ( ) ) ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-02-24 15:57:39 +01:00
}
}
}
}
}
}
2019-10-19 21:08:59 +02:00
else if ( Token : : Match ( parent , " :: %name% " ) & & parent - > astOperand2 ( ) = = tok ) {
setTokenValue ( parent , value , settings ) ;
}
2021-12-04 08:54:04 +01:00
// Calling std::size or std::empty on an array
else if ( value . isTokValue ( ) & & Token : : simpleMatch ( value . tokvalue , " { " ) && tok->variable() &&
tok - > variable ( ) - > isArray ( ) & & Token : : Match ( parent - > previous ( ) , " %name% ( " ) & & astIsRHS ( tok ) ) {
std : : vector < const Token * > args = getArguments ( value . tokvalue ) ;
if ( const Library : : Function * f = settings - > library . getFunction ( parent - > previous ( ) ) ) {
if ( f - > containerYield = = Library : : Container : : Yield : : SIZE ) {
ValueFlow : : Value v ( value ) ;
v . valueType = ValueFlow : : Value : : ValueType : : INT ;
v . intvalue = args . size ( ) ;
setTokenValue ( parent , v , settings ) ;
} else if ( f - > containerYield = = Library : : Container : : Yield : : EMPTY ) {
ValueFlow : : Value v ( value ) ;
v . intvalue = args . empty ( ) ;
v . valueType = ValueFlow : : Value : : ValueType : : INT ;
setTokenValue ( parent , v , settings ) ;
}
}
}
2014-01-18 19:30:44 +01:00
}
2018-12-27 21:33:01 +01:00
static void setTokenValueCast ( Token * parent , const ValueType & valueType , const ValueFlow : : Value & value , const Settings * settings )
{
2021-04-05 10:20:14 +02:00
if ( valueType . pointer | | value . isImpossible ( ) )
2018-12-27 21:33:01 +01:00
setTokenValue ( parent , value , settings ) ;
else if ( valueType . type = = ValueType : : Type : : CHAR )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > char_bit ) , settings ) ;
else if ( valueType . type = = ValueType : : Type : : SHORT )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > short_bit ) , settings ) ;
else if ( valueType . type = = ValueType : : Type : : INT )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > int_bit ) , settings ) ;
else if ( valueType . type = = ValueType : : Type : : LONG )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > long_bit ) , settings ) ;
else if ( valueType . type = = ValueType : : Type : : LONGLONG )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > long_long_bit ) , settings ) ;
2021-09-09 07:49:56 +02:00
else if ( valueType . isFloat ( ) & & isNumeric ( value ) ) {
2021-09-05 07:10:58 +02:00
ValueFlow : : Value floatValue = value ;
floatValue . valueType = ValueFlow : : Value : : ValueType : : FLOAT ;
if ( value . isIntValue ( ) )
floatValue . floatValue = value . intvalue ;
setTokenValue ( parent , floatValue , settings ) ;
2021-09-09 07:49:56 +02:00
} else if ( value . isIntValue ( ) ) {
2018-12-27 21:33:01 +01:00
const long long charMax = settings - > signedCharMax ( ) ;
const long long charMin = settings - > signedCharMin ( ) ;
if ( charMin < = value . intvalue & & value . intvalue < = charMax ) {
// unknown type, but value is small so there should be no truncation etc
setTokenValue ( parent , value , settings ) ;
}
}
}
2019-07-15 13:47:17 +02:00
static nonneg int getSizeOfType ( const Token * typeTok , const Settings * settings )
2017-10-18 23:20:04 +02:00
{
2020-05-28 21:24:48 +02:00
const ValueType & valueType = ValueType : : parseDecl ( typeTok , settings ) ;
if ( valueType . pointer > 0 )
return settings - > sizeof_pointer ;
if ( valueType . type = = ValueType : : Type : : BOOL | | valueType . type = = ValueType : : Type : : CHAR )
2017-10-18 23:20:04 +02:00
return 1 ;
2020-05-28 21:24:48 +02:00
if ( valueType . type = = ValueType : : Type : : SHORT )
2017-10-18 23:20:04 +02:00
return settings - > sizeof_short ;
2020-05-28 21:24:48 +02:00
if ( valueType . type = = ValueType : : Type : : INT )
2017-10-18 23:20:04 +02:00
return settings - > sizeof_int ;
2020-05-28 21:24:48 +02:00
if ( valueType . type = = ValueType : : Type : : LONG )
return settings - > sizeof_long ;
if ( valueType . type = = ValueType : : Type : : LONGLONG )
return settings - > sizeof_long_long ;
if ( valueType . type = = ValueType : : Type : : WCHAR_T )
2018-03-23 08:28:12 +01:00
return settings - > sizeof_wchar_t ;
2020-05-28 21:24:48 +02:00
return 0 ;
2017-10-18 23:20:04 +02:00
}
2016-05-07 20:18:07 +02:00
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
size_t ValueFlow : : getSizeOf ( const ValueType & vt , const Settings * settings )
2019-03-11 20:32:24 +01:00
{
if ( vt . pointer )
return settings - > sizeof_pointer ;
2020-05-28 21:24:48 +02:00
if ( vt . type = = ValueType : : Type : : CHAR )
2019-03-11 20:32:24 +01:00
return 1 ;
2020-05-28 21:24:48 +02:00
if ( vt . type = = ValueType : : Type : : SHORT )
2019-03-11 20:32:24 +01:00
return settings - > sizeof_short ;
2020-05-28 21:24:48 +02:00
if ( vt . type = = ValueType : : Type : : WCHAR_T )
2019-05-01 16:34:28 +02:00
return settings - > sizeof_wchar_t ;
2020-05-28 21:24:48 +02:00
if ( vt . type = = ValueType : : Type : : INT )
2019-03-11 20:32:24 +01:00
return settings - > sizeof_int ;
2020-05-28 21:24:48 +02:00
if ( vt . type = = ValueType : : Type : : LONG )
2019-03-11 20:32:24 +01:00
return settings - > sizeof_long ;
2020-05-28 21:24:48 +02:00
if ( vt . type = = ValueType : : Type : : LONGLONG )
2019-03-11 20:32:24 +01:00
return settings - > sizeof_long_long ;
2020-05-28 21:24:48 +02:00
if ( vt . type = = ValueType : : Type : : FLOAT )
2019-03-11 20:32:24 +01:00
return settings - > sizeof_float ;
2020-05-28 21:24:48 +02:00
if ( vt . type = = ValueType : : Type : : DOUBLE )
2019-03-11 20:32:24 +01:00
return settings - > sizeof_double ;
2020-11-11 22:51:17 +01:00
if ( vt . type = = ValueType : : Type : : LONGDOUBLE )
return settings - > sizeof_long_double ;
2019-03-11 20:32:24 +01:00
return 0 ;
}
2016-05-07 20:18:07 +02:00
// Handle various constants..
2019-06-16 10:09:38 +02:00
static Token * valueFlowSetConstantValue ( Token * tok , const Settings * settings , bool cpp )
2016-05-07 20:18:07 +02:00
{
if ( ( tok - > isNumber ( ) & & MathLib : : isInt ( tok - > str ( ) ) ) | | ( tok - > tokType ( ) = = Token : : eChar ) ) {
2021-05-08 11:20:29 +02:00
try {
ValueFlow : : Value value ( MathLib : : toLongNumber ( tok - > str ( ) ) ) ;
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
setTokenValue ( tok , value , settings ) ;
2021-08-07 20:51:18 +02:00
} catch ( const std : : exception & /*e*/ ) {
2021-05-08 11:20:29 +02:00
// Bad character literal
}
2016-11-13 22:59:56 +01:00
} else if ( tok - > isNumber ( ) & & MathLib : : isFloat ( tok - > str ( ) ) ) {
ValueFlow : : Value value ;
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : FLOAT ;
2016-11-13 22:59:56 +01:00
value . floatValue = MathLib : : toDoubleNumber ( tok - > str ( ) ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
2016-05-26 11:25:50 +02:00
} else if ( tok - > enumerator ( ) & & tok - > enumerator ( ) - > value_known ) {
2016-05-07 20:18:07 +02:00
ValueFlow : : Value value ( tok - > enumerator ( ) - > value ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
2016-10-18 21:44:02 +02:00
} else if ( tok - > str ( ) = = " NULL " | | ( cpp & & tok - > str ( ) = = " nullptr " ) ) {
ValueFlow : : Value value ( 0 ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
2016-12-18 14:03:48 +01:00
} else if ( Token : : simpleMatch ( tok , " sizeof ( " ) ) {
2021-07-08 18:18:32 +02:00
if ( tok - > next ( ) - > astOperand2 ( ) & & ! tok - > next ( ) - > astOperand2 ( ) - > isLiteral ( ) & & tok - > next ( ) - > astOperand2 ( ) - > valueType ( ) & &
tok - > next ( ) - > astOperand2 ( ) - > valueType ( ) - > pointer = = 0 & & // <- TODO this is a bailout, abort when there are array->pointer conversions
! tok - > next ( ) - > astOperand2 ( ) - > valueType ( ) - > isEnum ( ) ) { // <- TODO this is a bailout, handle enum with non-int types
const size_t sz = ValueFlow : : getSizeOf ( * tok - > next ( ) - > astOperand2 ( ) - > valueType ( ) , settings ) ;
if ( sz ) {
ValueFlow : : Value value ( sz ) ;
value . setKnown ( ) ;
setTokenValue ( tok - > next ( ) , value , settings ) ;
return tok - > linkAt ( 1 ) ;
}
}
2016-07-08 20:39:34 +02:00
const Token * tok2 = tok - > tokAt ( 2 ) ;
2018-03-23 08:28:12 +01:00
// skip over tokens to find variable or type
while ( Token : : Match ( tok2 , " %name% ::|.|[ " ) ) {
if ( tok2 - > next ( ) - > str ( ) = = " [ " )
tok2 = tok2 - > linkAt ( 1 ) - > next ( ) ;
else
tok2 = tok2 - > tokAt ( 2 ) ;
}
2019-04-06 06:55:46 +02:00
if ( Token : : simpleMatch ( tok , " sizeof ( * " ) ) {
2019-03-11 20:32:24 +01:00
const ValueType * vt = tok - > tokAt ( 2 ) - > valueType ( ) ;
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
const size_t sz = vt ? ValueFlow : : getSizeOf ( * vt , settings ) : 0 ;
2019-03-11 20:32:24 +01:00
if ( sz > 0 ) {
ValueFlow : : Value value ( sz ) ;
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok - > next ( ) , value , settings ) ;
2019-03-11 20:32:24 +01:00
}
} else if ( tok2 - > enumerator ( ) & & tok2 - > enumerator ( ) - > scope ) {
2016-07-08 20:39:34 +02:00
long long size = settings - > sizeof_int ;
const Token * type = tok2 - > enumerator ( ) - > scope - > enumType ;
if ( type ) {
2017-10-18 23:20:04 +02:00
size = getSizeOfType ( type , settings ) ;
2020-05-28 21:24:48 +02:00
if ( size = = 0 )
tok - > linkAt ( 1 ) ;
2016-07-08 20:39:34 +02:00
}
ValueFlow : : Value value ( size ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
setTokenValue ( tok - > next ( ) , value , settings ) ;
2016-07-08 20:39:34 +02:00
} else if ( tok2 - > type ( ) & & tok2 - > type ( ) - > isEnumType ( ) ) {
long long size = settings - > sizeof_int ;
2016-10-26 10:36:02 +02:00
if ( tok2 - > type ( ) - > classScope ) {
const Token * type = tok2 - > type ( ) - > classScope - > enumType ;
if ( type ) {
2017-10-18 23:20:04 +02:00
size = getSizeOfType ( type , settings ) ;
2016-10-26 10:36:02 +02:00
}
2016-07-08 20:39:34 +02:00
}
ValueFlow : : Value value ( size ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
setTokenValue ( tok - > next ( ) , value , settings ) ;
2016-12-18 14:03:48 +01:00
} else if ( Token : : Match ( tok , " sizeof ( %var% ) / sizeof ( " ) && tok->next()->astParent() == tok->tokAt(4)) {
// Get number of elements in array
const Token * sz1 = tok - > tokAt ( 2 ) ;
2022-04-14 18:13:29 +02:00
const Token * sz2 = tok - > tokAt ( 6 ) ; // left parenthesis
2021-01-27 19:49:13 +01:00
const nonneg int varid1 = sz1 - > varId ( ) ;
2016-12-18 14:03:48 +01:00
if ( varid1 & &
sz1 - > variable ( ) & &
sz1 - > variable ( ) - > isArray ( ) & &
! sz1 - > variable ( ) - > dimensions ( ) . empty ( ) & &
sz1 - > variable ( ) - > dimensionKnown ( 0 ) & &
2022-04-15 08:48:49 +02:00
Token : : Match ( sz2 - > astOperand2 ( ) , " *|[ " ) & & Token : : Match ( sz2 - > astOperand2 ( ) - > astOperand1 ( ) , " %varid% " , varid1 ) ) {
2016-12-18 14:03:48 +01:00
ValueFlow : : Value value ( sz1 - > variable ( ) - > dimension ( 0 ) ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok - > tokAt ( 4 ) , value , settings ) ;
2016-12-18 14:03:48 +01:00
}
2018-03-23 08:28:12 +01:00
} else if ( Token : : Match ( tok2 , " %var% ) " )) {
const Variable * var = tok2 - > variable ( ) ;
// only look for single token types (no pointers or references yet)
if ( var & & var - > typeStartToken ( ) = = var - > typeEndToken ( ) ) {
// find the size of the type
size_t size = 0 ;
if ( var - > isEnumType ( ) ) {
size = settings - > sizeof_int ;
if ( var - > type ( ) - > classScope & & var - > type ( ) - > classScope - > enumType )
size = getSizeOfType ( var - > type ( ) - > classScope - > enumType , settings ) ;
2019-03-13 18:31:41 +01:00
} else if ( var - > valueType ( ) ) {
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
size = ValueFlow : : getSizeOf ( * var - > valueType ( ) , settings ) ;
2018-03-23 08:28:12 +01:00
} else if ( ! var - > type ( ) ) {
size = getSizeOfType ( var - > typeStartToken ( ) , settings ) ;
}
// find the number of elements
size_t count = 1 ;
for ( size_t i = 0 ; i < var - > dimensions ( ) . size ( ) ; + + i ) {
if ( var - > dimensionKnown ( i ) )
count * = var - > dimension ( i ) ;
else
count = 0 ;
}
if ( size & & count > 0 ) {
ValueFlow : : Value value ( count * size ) ;
if ( settings - > platformType ! = cppcheck : : Platform : : Unspecified )
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
setTokenValue ( tok - > next ( ) , value , settings ) ;
2018-03-23 08:28:12 +01:00
}
}
2019-10-20 21:02:28 +02:00
} else if ( tok2 - > tokType ( ) = = Token : : eString ) {
size_t sz = Token : : getStrSize ( tok2 , settings ) ;
if ( sz > 0 ) {
ValueFlow : : Value value ( sz ) ;
value . setKnown ( ) ;
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
}
} else if ( tok2 - > tokType ( ) = = Token : : eChar ) {
2019-10-31 14:06:34 +01:00
nonneg int sz = 0 ;
if ( cpp & & settings - > standards . cpp > = Standards : : CPP20 & & tok2 - > isUtf8 ( ) )
sz = 1 ;
else if ( tok2 - > isUtf16 ( ) )
sz = 2 ;
else if ( tok2 - > isUtf32 ( ) )
sz = 4 ;
else if ( tok2 - > isLong ( ) )
sz = settings - > sizeof_wchar_t ;
else if ( ( tok2 - > isCChar ( ) & & ! cpp ) | | ( tok2 - > isCMultiChar ( ) ) )
sz = settings - > sizeof_int ;
else
sz = 1 ;
2019-10-20 21:02:28 +02:00
if ( sz > 0 ) {
ValueFlow : : Value value ( sz ) ;
value . setKnown ( ) ;
setTokenValue ( tok - > next ( ) , value , settings ) ;
}
2016-12-18 14:03:48 +01:00
} else if ( ! tok2 - > type ( ) ) {
const ValueType & vt = ValueType : : parseDecl ( tok2 , settings ) ;
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
const size_t sz = ValueFlow : : getSizeOf ( vt , settings ) ;
2019-03-11 20:32:24 +01:00
if ( sz > 0 ) {
ValueFlow : : Value value ( sz ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok - > next ( ) , value , settings ) ;
2016-12-18 14:03:48 +01:00
}
2016-07-08 20:39:34 +02:00
}
// skip over enum
tok = tok - > linkAt ( 1 ) ;
2016-05-07 20:18:07 +02:00
}
2016-07-08 20:39:34 +02:00
return tok - > next ( ) ;
2016-05-07 20:18:07 +02:00
}
2014-01-18 19:30:44 +01:00
static void valueFlowNumber ( TokenList * tokenlist )
{
2016-07-08 20:39:34 +02:00
for ( Token * tok = tokenlist - > front ( ) ; tok ; ) {
2016-10-18 21:44:02 +02:00
tok = valueFlowSetConstantValue ( tok , tokenlist - > getSettings ( ) , tokenlist - > isCPP ( ) ) ;
2014-01-18 19:30:44 +01:00
}
2015-07-14 18:02:26 +02:00
if ( tokenlist - > isCPP ( ) ) {
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2015-07-16 17:33:16 +02:00
if ( tok - > isName ( ) & & ! tok - > varId ( ) & & Token : : Match ( tok , " false|true " ) ) {
ValueFlow : : Value value ( tok - > str ( ) = = " true " ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2016-01-26 16:53:01 +01:00
} else if ( Token : : Match ( tok , " [(,] NULL [,) ] " )) {
// NULL function parameters are not simplified in the
// normal tokenlist
ValueFlow : : Value value ( 0 ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok - > next ( ) , value , tokenlist - > getSettings ( ) ) ;
2015-07-16 17:33:16 +02:00
}
2015-07-14 18:02:26 +02:00
}
}
2014-01-18 19:30:44 +01:00
}
2014-08-03 20:11:22 +02:00
static void valueFlowString ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2015-08-14 20:46:13 +02:00
if ( tok - > tokType ( ) = = Token : : eString ) {
2014-08-03 20:11:22 +02:00
ValueFlow : : Value strvalue ;
2021-01-02 09:30:00 +01:00
strvalue . valueType = ValueFlow : : Value : : ValueType : : TOK ;
2014-08-03 20:11:22 +02:00
strvalue . tokvalue = tok ;
2015-07-25 19:36:29 +02:00
strvalue . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , strvalue , tokenlist - > getSettings ( ) ) ;
2014-08-03 20:11:22 +02:00
}
2015-02-24 15:57:39 +01:00
}
}
static void valueFlowArray ( TokenList * tokenlist )
{
2021-01-27 19:49:13 +01:00
std : : map < nonneg int , const Token * > constantArrays ;
2015-02-24 15:57:39 +01:00
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2021-01-27 19:49:13 +01:00
if ( tok - > varId ( ) > 0 ) {
2018-12-17 06:07:34 +01:00
// array
2021-01-27 19:49:13 +01:00
const std : : map < nonneg int , const Token * > : : const_iterator it = constantArrays . find ( tok - > varId ( ) ) ;
2015-08-28 14:27:56 +02:00
if ( it ! = constantArrays . end ( ) ) {
ValueFlow : : Value value ;
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : TOK ;
2015-08-28 14:27:56 +02:00
value . tokvalue = it - > second ;
2015-11-07 15:07:26 +01:00
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2015-11-07 15:07:26 +01:00
}
2021-12-17 21:48:29 +01:00
// const array decl
else if ( tok - > variable ( ) & & tok - > variable ( ) - > isArray ( ) & & tok - > variable ( ) - > isConst ( ) & &
tok - > variable ( ) - > nameToken ( ) = = tok & & Token : : Match ( tok , " %var% [ %num%| ] = { " ) ) {
const Token * rhstok = tok - > next ( ) - > link ( ) - > tokAt ( 2 ) ;
constantArrays [ tok - > varId ( ) ] = rhstok ;
tok = rhstok - > link ( ) ;
}
2015-11-07 15:07:26 +01:00
// pointer = array
2021-12-17 21:48:29 +01:00
else if ( tok - > variable ( ) & & tok - > variable ( ) - > isArray ( ) & & Token : : simpleMatch ( tok - > astParent ( ) , " = " ) & &
astIsRHS ( tok ) & & tok - > astParent ( ) - > astOperand1 ( ) & &
2015-11-07 15:07:26 +01:00
tok - > astParent ( ) - > astOperand1 ( ) - > variable ( ) & &
tok - > astParent ( ) - > astOperand1 ( ) - > variable ( ) - > isPointer ( ) ) {
ValueFlow : : Value value ;
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : TOK ;
2015-11-07 15:07:26 +01:00
value . tokvalue = tok ;
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2015-08-28 14:27:56 +02:00
}
continue ;
}
2015-02-24 15:57:39 +01:00
if ( Token : : Match ( tok , " const %type% %var% [ %num%| ] = { " ) ) {
const Token * vartok = tok - > tokAt ( 2 ) ;
const Token * rhstok = vartok - > next ( ) - > link ( ) - > tokAt ( 2 ) ;
constantArrays [ vartok - > varId ( ) ] = rhstok ;
tok = rhstok - > link ( ) ;
2015-08-28 14:27:56 +02:00
continue ;
2015-02-24 15:57:39 +01:00
}
2015-02-07 18:14:22 +01:00
2015-11-07 15:07:26 +01:00
else if ( Token : : Match ( tok , " const char %var% [ %num%| ] = %str% ; " ) ) {
2015-02-07 18:14:22 +01:00
const Token * vartok = tok - > tokAt ( 2 ) ;
2015-02-09 08:23:36 +01:00
const Token * strtok = vartok - > next ( ) - > link ( ) - > tokAt ( 2 ) ;
2015-02-24 15:57:39 +01:00
constantArrays [ vartok - > varId ( ) ] = strtok ;
tok = strtok - > next ( ) ;
2015-08-28 14:27:56 +02:00
continue ;
2015-02-07 18:14:22 +01:00
}
2014-08-03 20:11:22 +02:00
}
}
2019-01-21 20:05:35 +01:00
static bool isNonZero ( const Token * tok )
{
return tok & & ( ! tok - > hasKnownIntValue ( ) | | tok - > values ( ) . front ( ) . intvalue ! = 0 ) ;
}
static const Token * getOtherOperand ( const Token * tok )
{
if ( ! tok )
return nullptr ;
if ( ! tok - > astParent ( ) )
return nullptr ;
if ( tok - > astParent ( ) - > astOperand1 ( ) ! = tok )
return tok - > astParent ( ) - > astOperand1 ( ) ;
if ( tok - > astParent ( ) - > astOperand2 ( ) ! = tok )
return tok - > astParent ( ) - > astOperand2 ( ) ;
return nullptr ;
}
static void valueFlowArrayBool ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > hasKnownIntValue ( ) )
continue ;
const Variable * var = nullptr ;
bool known = false ;
std : : list < ValueFlow : : Value > : : const_iterator val =
std : : find_if ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , std : : mem_fn ( & ValueFlow : : Value : : isTokValue ) ) ;
if ( val = = tok - > values ( ) . end ( ) ) {
var = tok - > variable ( ) ;
known = true ;
} else {
var = val - > tokvalue - > variable ( ) ;
known = val - > isKnown ( ) ;
}
if ( ! var )
continue ;
if ( ! var - > isArray ( ) | | var - > isArgument ( ) | | var - > isStlType ( ) )
continue ;
if ( isNonZero ( getOtherOperand ( tok ) ) & & Token : : Match ( tok - > astParent ( ) , " %comp% " ) )
continue ;
// TODO: Check for function argument
if ( ( astIsBool ( tok - > astParent ( ) ) & & ! Token : : Match ( tok - > astParent ( ) , " (|%name% " ) ) | |
( tok - > astParent ( ) & & Token : : Match ( tok - > astParent ( ) - > previous ( ) , " if|while|for ( " ) ) ) {
ValueFlow : : Value value { 1 } ;
if ( known )
value . setKnown ( ) ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
}
}
}
2014-08-03 20:11:22 +02:00
static void valueFlowPointerAlias ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
// not address of
2018-07-14 22:36:08 +02:00
if ( ! tok - > isUnaryOp ( " & " ) )
2014-08-03 20:11:22 +02:00
continue ;
// parent should be a '='
if ( ! Token : : simpleMatch ( tok - > astParent ( ) , " = " ) )
continue ;
// child should be some buffer or variable
2015-11-15 23:38:23 +01:00
const Token * vartok = tok - > astOperand1 ( ) ;
while ( vartok ) {
if ( vartok - > str ( ) = = " [ " )
vartok = vartok - > astOperand1 ( ) ;
else if ( vartok - > str ( ) = = " . " | | vartok - > str ( ) = = " :: " )
vartok = vartok - > astOperand2 ( ) ;
else
break ;
}
if ( ! ( vartok & & vartok - > variable ( ) & & ! vartok - > variable ( ) - > isPointer ( ) ) )
2014-08-03 20:11:22 +02:00
continue ;
ValueFlow : : Value value ;
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : TOK ;
2014-08-03 20:11:22 +02:00
value . tokvalue = tok ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2014-08-03 20:11:22 +02:00
}
}
2014-04-14 06:45:39 +02:00
static void valueFlowBitAnd ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) ! = " & " )
continue ;
2018-07-14 13:09:53 +02:00
if ( tok - > hasKnownValue ( ) )
2016-08-28 19:11:05 +02:00
continue ;
2014-04-14 06:45:39 +02:00
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
MathLib : : bigint number ;
if ( MathLib : : isInt ( tok - > astOperand1 ( ) - > str ( ) ) )
number = MathLib : : toLongNumber ( tok - > astOperand1 ( ) - > str ( ) ) ;
else if ( MathLib : : isInt ( tok - > astOperand2 ( ) - > str ( ) ) )
number = MathLib : : toLongNumber ( tok - > astOperand2 ( ) - > str ( ) ) ;
else
continue ;
int bit = 0 ;
2017-10-21 22:08:34 +02:00
while ( bit < = ( MathLib : : bigint_bits - 2 ) & & ( ( ( ( MathLib : : bigint ) 1 ) < < bit ) < number ) )
2014-04-14 06:45:39 +02:00
+ + bit ;
2017-10-21 22:17:07 +02:00
if ( ( ( ( MathLib : : bigint ) 1 ) < < bit ) = = number ) {
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , ValueFlow : : Value ( 0 ) , tokenlist - > getSettings ( ) ) ;
setTokenValue ( tok , ValueFlow : : Value ( number ) , tokenlist - > getSettings ( ) ) ;
2014-04-14 06:45:39 +02:00
}
}
}
2018-12-16 07:35:27 +01:00
static void valueFlowSameExpressions ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2021-07-30 21:29:35 +02:00
if ( tok - > hasKnownIntValue ( ) )
2018-12-16 07:35:27 +01:00
continue ;
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
if ( tok - > astOperand1 ( ) - > isLiteral ( ) | | tok - > astOperand2 ( ) - > isLiteral ( ) )
continue ;
2019-01-11 08:39:23 +01:00
if ( ! astIsIntegral ( tok - > astOperand1 ( ) , false ) & & ! astIsIntegral ( tok - > astOperand2 ( ) , false ) )
2018-12-16 07:35:27 +01:00
continue ;
ValueFlow : : Value val ;
if ( Token : : Match ( tok , " ==|>=|<=|/ " ) ) {
val = ValueFlow : : Value ( 1 ) ;
val . setKnown ( ) ;
}
if ( Token : : Match ( tok , " !=|>|<|%|- " ) ) {
val = ValueFlow : : Value ( 0 ) ;
val . setKnown ( ) ;
}
if ( ! val . isKnown ( ) )
continue ;
2018-12-28 12:59:05 +01:00
if ( isSameExpression ( tokenlist - > isCPP ( ) , false , tok - > astOperand1 ( ) , tok - > astOperand2 ( ) , tokenlist - > getSettings ( ) - > library , true , true , & val . errorPath ) ) {
2018-12-16 07:35:27 +01:00
setTokenValue ( tok , val , tokenlist - > getSettings ( ) ) ;
}
}
}
2018-11-03 23:25:46 +01:00
static bool getExpressionRange ( const Token * expr , MathLib : : bigint * minvalue , MathLib : : bigint * maxvalue )
{
if ( expr - > hasKnownIntValue ( ) ) {
if ( minvalue )
* minvalue = expr - > values ( ) . front ( ) . intvalue ;
if ( maxvalue )
* maxvalue = expr - > values ( ) . front ( ) . intvalue ;
return true ;
}
if ( expr - > str ( ) = = " & " & & expr - > astOperand1 ( ) & & expr - > astOperand2 ( ) ) {
MathLib : : bigint vals [ 4 ] ;
bool lhsHasKnownRange = getExpressionRange ( expr - > astOperand1 ( ) , & vals [ 0 ] , & vals [ 1 ] ) ;
bool rhsHasKnownRange = getExpressionRange ( expr - > astOperand2 ( ) , & vals [ 2 ] , & vals [ 3 ] ) ;
if ( ! lhsHasKnownRange & & ! rhsHasKnownRange )
return false ;
if ( ! lhsHasKnownRange | | ! rhsHasKnownRange ) {
if ( minvalue )
* minvalue = lhsHasKnownRange ? vals [ 0 ] : vals [ 2 ] ;
if ( maxvalue )
* maxvalue = lhsHasKnownRange ? vals [ 1 ] : vals [ 3 ] ;
} else {
if ( minvalue )
* minvalue = vals [ 0 ] & vals [ 2 ] ;
if ( maxvalue )
* maxvalue = vals [ 1 ] & vals [ 3 ] ;
}
return true ;
}
if ( expr - > str ( ) = = " % " & & expr - > astOperand1 ( ) & & expr - > astOperand2 ( ) ) {
MathLib : : bigint vals [ 4 ] ;
if ( ! getExpressionRange ( expr - > astOperand2 ( ) , & vals [ 2 ] , & vals [ 3 ] ) )
return false ;
if ( vals [ 2 ] < = 0 )
return false ;
bool lhsHasKnownRange = getExpressionRange ( expr - > astOperand1 ( ) , & vals [ 0 ] , & vals [ 1 ] ) ;
if ( lhsHasKnownRange & & vals [ 0 ] < 0 )
return false ;
// If lhs has unknown value, it must be unsigned
if ( ! lhsHasKnownRange & & ( ! expr - > astOperand1 ( ) - > valueType ( ) | | expr - > astOperand1 ( ) - > valueType ( ) - > sign ! = ValueType : : Sign : : UNSIGNED ) )
return false ;
if ( minvalue )
* minvalue = 0 ;
if ( maxvalue )
* maxvalue = vals [ 3 ] - 1 ;
return true ;
}
return false ;
}
2019-01-01 14:15:50 +01:00
static void valueFlowRightShift ( TokenList * tokenList , const Settings * settings )
2018-11-03 23:25:46 +01:00
{
for ( Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) ! = " >> " )
continue ;
if ( tok - > hasKnownValue ( ) )
continue ;
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
if ( ! tok - > astOperand2 ( ) - > hasKnownValue ( ) )
continue ;
2018-11-04 17:13:23 +01:00
const MathLib : : bigint rhsvalue = tok - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ;
if ( rhsvalue < 0 )
continue ;
2018-11-03 23:25:46 +01:00
if ( ! tok - > astOperand1 ( ) - > valueType ( ) | | ! tok - > astOperand1 ( ) - > valueType ( ) - > isIntegral ( ) )
continue ;
if ( ! tok - > astOperand2 ( ) - > valueType ( ) | | ! tok - > astOperand2 ( ) - > valueType ( ) - > isIntegral ( ) )
continue ;
MathLib : : bigint lhsmax = 0 ;
if ( ! getExpressionRange ( tok - > astOperand1 ( ) , nullptr , & lhsmax ) )
continue ;
if ( lhsmax < 0 )
continue ;
2019-01-01 14:15:50 +01:00
int lhsbits ;
if ( ( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : CHAR ) | |
( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : SHORT ) | |
2019-05-01 16:34:28 +02:00
( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : WCHAR_T ) | |
2019-01-01 14:15:50 +01:00
( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : BOOL ) | |
( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : INT ) )
lhsbits = settings - > int_bit ;
else if ( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : LONG )
lhsbits = settings - > long_bit ;
else if ( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : LONGLONG )
lhsbits = settings - > long_long_bit ;
else
continue ;
if ( rhsvalue > = lhsbits | | rhsvalue > = MathLib : : bigint_bits | | ( 1ULL < < rhsvalue ) < = lhsmax )
2018-11-03 23:25:46 +01:00
continue ;
ValueFlow : : Value val ( 0 ) ;
val . setKnown ( ) ;
setTokenValue ( tok , val , tokenList - > getSettings ( ) ) ;
}
}
2021-03-30 14:02:28 +02:00
static std : : vector < MathLib : : bigint > minUnsignedValue ( const Token * tok , int depth = 8 )
{
std : : vector < MathLib : : bigint > result = { } ;
if ( ! tok )
return result ;
if ( depth < 0 )
return result ;
if ( tok - > hasKnownIntValue ( ) ) {
result = { tok - > values ( ) . front ( ) . intvalue } ;
2021-07-26 22:22:50 +02:00
} else if ( ! Token : : Match ( tok , " -|%|&|^ " ) & & tok - > isConstOp ( ) & & tok - > astOperand1 ( ) & & tok - > astOperand2 ( ) ) {
2021-03-30 14:02:28 +02:00
std : : vector < MathLib : : bigint > op1 = minUnsignedValue ( tok - > astOperand1 ( ) , depth - 1 ) ;
std : : vector < MathLib : : bigint > op2 = minUnsignedValue ( tok - > astOperand2 ( ) , depth - 1 ) ;
if ( ! op1 . empty ( ) & & ! op2 . empty ( ) ) {
result = calculate < std : : vector < MathLib : : bigint > > ( tok - > str ( ) , op1 . front ( ) , op2 . front ( ) ) ;
}
}
if ( result . empty ( ) & & astIsUnsigned ( tok ) )
result = { 0 } ;
return result ;
}
2022-04-23 06:19:07 +02:00
static bool isConvertedToIntegral ( const Token * tok , const Settings * settings )
{
if ( ! tok )
return false ;
std : : vector < ValueType > parentTypes = getParentValueTypes ( tok , settings ) ;
if ( parentTypes . empty ( ) )
return false ;
const ValueType & vt = parentTypes . front ( ) ;
return vt . type ! = ValueType : : UNKNOWN_INT & & vt . isIntegral ( ) ;
}
2021-03-30 14:02:28 +02:00
static void valueFlowImpossibleValues ( TokenList * tokenList , const Settings * settings )
{
for ( Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > hasKnownIntValue ( ) )
continue ;
2022-04-03 20:04:05 +02:00
if ( Token : : Match ( tok , " true|false " ) )
continue ;
if ( astIsBool ( tok ) | | Token : : Match ( tok , " %comp% " ) ) {
ValueFlow : : Value lower { - 1 } ;
lower . bound = ValueFlow : : Value : : Bound : : Upper ;
lower . setImpossible ( ) ;
setTokenValue ( tok , lower , settings ) ;
ValueFlow : : Value upper { 2 } ;
upper . bound = ValueFlow : : Value : : Bound : : Lower ;
upper . setImpossible ( ) ;
setTokenValue ( tok , upper , settings ) ;
} else if ( astIsUnsigned ( tok ) & & ! astIsPointer ( tok ) ) {
2021-03-30 14:02:28 +02:00
std : : vector < MathLib : : bigint > minvalue = minUnsignedValue ( tok ) ;
if ( minvalue . empty ( ) )
continue ;
ValueFlow : : Value value { std : : max < MathLib : : bigint > ( 0 , minvalue . front ( ) ) - 1 } ;
value . bound = ValueFlow : : Value : : Bound : : Upper ;
value . setImpossible ( ) ;
setTokenValue ( tok , value , settings ) ;
}
if ( Token : : simpleMatch ( tok , " % " ) & & tok - > astOperand2 ( ) & & tok - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
ValueFlow : : Value value { tok - > astOperand2 ( ) - > values ( ) . front ( ) } ;
value . bound = ValueFlow : : Value : : Bound : : Lower ;
value . setImpossible ( ) ;
setTokenValue ( tok , value , settings ) ;
2021-08-03 06:31:28 +02:00
} else if ( Token : : Match ( tok , " abs|labs|llabs|fabs|fabsf|fabsl ( " ) ) {
ValueFlow : : Value value { - 1 } ;
value . bound = ValueFlow : : Value : : Bound : : Upper ;
value . setImpossible ( ) ;
setTokenValue ( tok - > next ( ) , value , settings ) ;
2021-10-11 19:16:12 +02:00
} else if ( Token : : Match ( tok , " . data|c_str ( " ) & & astIsContainerOwned ( tok - > astOperand1 ( ) ) ) {
2022-01-26 06:42:41 +01:00
const Library : : Container * container = getLibraryContainer ( tok - > astOperand1 ( ) ) ;
2021-10-11 19:16:12 +02:00
if ( ! container )
continue ;
if ( ! container - > stdStringLike )
continue ;
if ( container - > view )
continue ;
ValueFlow : : Value value { 0 } ;
value . setImpossible ( ) ;
setTokenValue ( tok - > tokAt ( 2 ) , value , settings ) ;
} else if ( Token : : Match ( tok , " make_shared|make_unique < " ) & & Token : : simpleMatch ( tok - > linkAt ( 1 ) , " > ( " ) ) {
ValueFlow : : Value value { 0 } ;
value . setImpossible ( ) ;
setTokenValue ( tok - > linkAt ( 1 ) - > next ( ) , value , settings ) ;
} else if ( tokenList - > isCPP ( ) & & Token : : simpleMatch ( tok , " this " ) ) {
ValueFlow : : Value value { 0 } ;
value . setImpossible ( ) ;
setTokenValue ( tok , value , settings ) ;
2022-04-23 06:19:07 +02:00
} else if ( tok - > isIncompleteVar ( ) & & tok - > astParent ( ) & & tok - > astParent ( ) - > isUnaryOp ( " - " ) & &
isConvertedToIntegral ( tok - > astParent ( ) , settings ) ) {
ValueFlow : : Value value { 0 } ;
value . setImpossible ( ) ;
setTokenValue ( tok , value , settings ) ;
2021-03-30 14:02:28 +02:00
}
}
}
Fix #9647: Set correct enum value (#2856)
* Tokenize: Set varId for variables in enum
Set varIds in enum values. It was previously disabled in 5119ae84b879fad
to avoid issues with enums named the same as global variables. Take care
to only set varids to variables used to set the value of an enumerator,
not the enumerator itself. This is somewhat complicated by the fact that
at the time this happens, astOperand1(), astOperand2(), astParent() etc
are not set. The current implementation is not perfect, for example in
the code below, y will not have a varid set, but x and z will. This is
deemed sufficient for now.
int x, y, z;
enum E { a = f(x, y, z); };
* Fix #9647: Value of enums with variables as init values
C++ allows enum values to be set using constexprs, which cppcheck did
not handle before. To solve this, add a new pass to valueflow to update
enum values after global consts have been processed. In order to do so,
I moved all settings of enum values to valueflow. After setting the enum
values, we need another call to valueFlowNumber() to actually set users
of the enums.
There is still room for improvements, since each pass of
valueFlowGlobalConstVar() and valueFlowEnumValue() only sets variables
that are possible to set directly, and not if setting the value of a
variable allows us to set the value of another. For example
constexpr int a = 5;
constexpr int b = a + 5;
enum E { X = a };
constexpr E e = X;
Here both b and e will not have their values set, even though cppcheck
should be possible to figure out their values. That's for another PR
though.
This was tested by running test-my-pr.py with 500 packages. The only
difference was one error message in fairy-stockfish_11.1, where cppcheck
now printed the correct size of an array instead of 2147483648 which I
assume is some kind of default value. In that package, using a constexpr
when setting enum values is common, but as mentioned, there was no
change in the number of warnings.
2020-10-22 07:45:04 +02:00
static void valueFlowEnumValue ( SymbolDatabase * symboldatabase , const Settings * settings )
{
for ( Scope & scope : symboldatabase - > scopeList ) {
if ( scope . type ! = Scope : : eEnum )
continue ;
MathLib : : bigint value = 0 ;
bool prev_enum_is_known = true ;
for ( Enumerator & enumerator : scope . enumeratorList ) {
if ( enumerator . start ) {
Token * rhs = enumerator . start - > previous ( ) - > astOperand2 ( ) ;
ValueFlow : : valueFlowConstantFoldAST ( rhs , settings ) ;
if ( rhs & & rhs - > hasKnownIntValue ( ) ) {
enumerator . value = rhs - > values ( ) . front ( ) . intvalue ;
enumerator . value_known = true ;
value = enumerator . value + 1 ;
prev_enum_is_known = true ;
} else
prev_enum_is_known = false ;
} else if ( prev_enum_is_known ) {
enumerator . value = value + + ;
enumerator . value_known = true ;
}
}
}
}
2019-04-21 06:54:32 +02:00
static void valueFlowGlobalConstVar ( TokenList * tokenList , const Settings * settings )
{
// Get variable values...
std : : map < const Variable * , ValueFlow : : Value > vars ;
for ( const Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > variable ( ) )
continue ;
// Initialization...
if ( tok = = tok - > variable ( ) - > nameToken ( ) & &
2019-05-14 08:58:27 +02:00
! tok - > variable ( ) - > isVolatile ( ) & &
! tok - > variable ( ) - > isArgument ( ) & &
2019-04-21 06:54:32 +02:00
tok - > variable ( ) - > isConst ( ) & &
tok - > valueType ( ) & &
tok - > valueType ( ) - > isIntegral ( ) & &
tok - > valueType ( ) - > pointer = = 0 & &
tok - > valueType ( ) - > constness = = 1 & &
Token : : Match ( tok , " %name% = " ) & &
tok - > next ( ) - > astOperand2 ( ) & &
tok - > next ( ) - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
vars [ tok - > variable ( ) ] = tok - > next ( ) - > astOperand2 ( ) - > values ( ) . front ( ) ;
}
}
// Set values..
for ( Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > variable ( ) )
continue ;
std : : map < const Variable * , ValueFlow : : Value > : : const_iterator var = vars . find ( tok - > variable ( ) ) ;
if ( var = = vars . end ( ) )
continue ;
setTokenValue ( tok , var - > second , settings ) ;
}
}
2017-09-15 15:58:19 +02:00
static void valueFlowGlobalStaticVar ( TokenList * tokenList , const Settings * settings )
{
// Get variable values...
std : : map < const Variable * , ValueFlow : : Value > vars ;
for ( const Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > variable ( ) )
continue ;
// Initialization...
if ( tok = = tok - > variable ( ) - > nameToken ( ) & &
tok - > variable ( ) - > isStatic ( ) & &
! tok - > variable ( ) - > isConst ( ) & &
tok - > valueType ( ) & &
tok - > valueType ( ) - > isIntegral ( ) & &
tok - > valueType ( ) - > pointer = = 0 & &
tok - > valueType ( ) - > constness = = 0 & &
Token : : Match ( tok , " %name% = " ) & &
tok - > next ( ) - > astOperand2 ( ) & &
tok - > next ( ) - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
vars [ tok - > variable ( ) ] = tok - > next ( ) - > astOperand2 ( ) - > values ( ) . front ( ) ;
} else {
// If variable is written anywhere in TU then remove it from vars
if ( ! tok - > astParent ( ) )
continue ;
if ( Token : : Match ( tok - > astParent ( ) , " ++|--|& " ) & & ! tok - > astParent ( ) - > astOperand2 ( ) )
vars . erase ( tok - > variable ( ) ) ;
2018-03-18 19:53:33 +01:00
else if ( tok - > astParent ( ) - > isAssignmentOp ( ) ) {
2017-09-15 15:58:19 +02:00
if ( tok = = tok - > astParent ( ) - > astOperand1 ( ) )
vars . erase ( tok - > variable ( ) ) ;
else if ( tokenList - > isCPP ( ) & & Token : : Match ( tok - > astParent ( ) - > tokAt ( - 2 ) , " & %name% = " ) )
vars . erase ( tok - > variable ( ) ) ;
2018-04-17 19:51:27 +02:00
} else if ( isLikelyStreamRead ( tokenList - > isCPP ( ) , tok - > astParent ( ) ) ) {
vars . erase ( tok - > variable ( ) ) ;
2017-09-15 15:58:19 +02:00
} else if ( Token : : Match ( tok - > astParent ( ) , " [(,] " ) )
vars . erase ( tok - > variable ( ) ) ;
}
}
// Set values..
for ( Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > variable ( ) )
continue ;
std : : map < const Variable * , ValueFlow : : Value > : : const_iterator var = vars . find ( tok - > variable ( ) ) ;
if ( var = = vars . end ( ) )
continue ;
setTokenValue ( tok , var - > second , settings ) ;
}
}
2021-07-18 07:46:31 +02:00
static Analyzer : : Result valueFlowForward ( Token * startToken ,
2021-08-07 20:51:18 +02:00
const Token * endToken ,
const Token * exprTok ,
2022-05-08 20:42:06 +02:00
const std : : list < ValueFlow : : Value > & values ,
2021-08-07 20:51:18 +02:00
TokenList * const tokenlist ,
const Settings * settings ) ;
2020-08-28 19:29:09 +02:00
2020-11-10 16:00:55 +01:00
static void valueFlowReverse ( TokenList * tokenlist ,
Token * tok ,
const Token * const varToken ,
2015-07-20 19:45:38 +02:00
ValueFlow : : Value val ,
2022-05-08 20:42:06 +02:00
const ValueFlow : : Value & val2 ,
2020-11-10 16:00:55 +01:00
ErrorLogger * errorLogger ,
const Settings * settings ) ;
2015-07-20 19:45:38 +02:00
2019-10-03 09:58:57 +02:00
static bool isConditionKnown ( const Token * tok , bool then )
{
const char * op = " || " ;
if ( then )
op = " && " ;
const Token * parent = tok - > astParent ( ) ;
2020-08-22 09:16:26 +02:00
while ( parent & & ( parent - > str ( ) = = op | | parent - > str ( ) = = " ! " ) )
2019-10-03 09:58:57 +02:00
parent = parent - > astParent ( ) ;
2021-11-14 18:30:36 +01:00
return Token : : Match ( parent , " (|; " ) ;
2019-10-03 09:58:57 +02:00
}
2020-11-10 16:00:55 +01:00
static const std : : string & invertAssign ( const std : : string & assign )
{
2021-08-09 07:45:41 +02:00
static std : : unordered_map < std : : string , std : : string > lookup = { { " = " , " = " } ,
{ " += " , " -= " } ,
{ " -= " , " += " } ,
{ " *= " , " /= " } ,
{ " /= " , " *= " } ,
{ " <<= " , " >>= " } ,
{ " >>= " , " <<= " } ,
{ " ^= " , " ^= " } } ;
2020-11-10 16:00:55 +01:00
auto it = lookup . find ( assign ) ;
2022-03-21 16:05:38 +01:00
if ( it = = lookup . end ( ) ) {
static std : : string empty ;
2020-11-10 16:00:55 +01:00
return empty ;
2022-03-21 16:05:38 +01:00
}
2020-11-10 16:00:55 +01:00
else
return it - > second ;
}
2021-08-09 07:45:41 +02:00
static std : : string removeAssign ( const std : : string & assign ) {
return std : : string { assign . begin ( ) , assign . end ( ) - 1 } ;
}
template < class T , class U >
static T calculateAssign ( const std : : string & assign , const T & x , const U & y , bool * error = nullptr )
2017-09-20 11:45:28 +02:00
{
2021-08-09 07:45:41 +02:00
if ( assign . empty ( ) | | assign . back ( ) ! = ' = ' ) {
if ( error )
* error = true ;
return T { } ;
}
if ( assign = = " = " )
return y ;
return calculate < T , T > ( removeAssign ( assign ) , x , y , error ) ;
}
template < class T , class U >
static void assignValueIfMutable ( T & x , const U & y )
{
x = y ;
}
template < class T , class U >
static void assignValueIfMutable ( const T & , const U & )
{ }
template < class Value , REQUIRES ( " Value must ValueFlow::Value " , std : : is_convertible < Value & , const ValueFlow : : Value & > ) >
static bool evalAssignment ( Value & lhsValue , const std : : string & assign , const ValueFlow : : Value & rhsValue )
{
bool error = false ;
if ( lhsValue . isSymbolicValue ( ) & & rhsValue . isIntValue ( ) ) {
if ( assign ! = " += " & & assign ! = " -= " )
2017-09-20 11:45:28 +02:00
return false ;
2021-08-09 07:45:41 +02:00
assignValueIfMutable ( lhsValue . intvalue , calculateAssign ( assign , lhsValue . intvalue , rhsValue . intvalue , & error ) ) ;
} else if ( lhsValue . isIntValue ( ) & & rhsValue . isIntValue ( ) ) {
assignValueIfMutable ( lhsValue . intvalue , calculateAssign ( assign , lhsValue . intvalue , rhsValue . intvalue , & error ) ) ;
} else if ( lhsValue . isFloatValue ( ) & & rhsValue . isIntValue ( ) ) {
assignValueIfMutable ( lhsValue . floatValue ,
calculateAssign ( assign , lhsValue . floatValue , rhsValue . intvalue , & error ) ) ;
2017-09-20 11:45:28 +02:00
} else {
return false ;
}
2021-08-09 07:45:41 +02:00
return ! error ;
2017-09-20 11:45:28 +02:00
}
2021-02-10 08:18:21 +01:00
template < class T >
struct SingleRange {
T * x ;
T * begin ( ) const {
return x ;
}
T * end ( ) const {
return x + 1 ;
}
} ;
template < class T >
SingleRange < T > MakeSingleRange ( T & x )
{
return { & x } ;
}
class SelectValueFromVarIdMapRange {
using M = std : : unordered_map < nonneg int , ValueFlow : : Value > ;
struct Iterator {
using iterator_category = std : : forward_iterator_tag ;
using value_type = const ValueFlow : : Value ;
using pointer = value_type * ;
using reference = value_type & ;
explicit Iterator ( const M : : const_iterator & it )
2021-08-07 20:51:18 +02:00
: mIt ( it ) { }
2021-02-10 08:18:21 +01:00
reference operator * ( ) const {
return mIt - > second ;
}
2022-03-02 07:46:47 +01:00
pointer operator - > ( ) const {
2021-02-10 08:18:21 +01:00
return & mIt - > second ;
}
Iterator & operator + + ( ) {
// cppcheck-suppress postfixOperator - forward iterator needs to perform post-increment
mIt + + ;
return * this ;
}
friend bool operator = = ( const Iterator & a , const Iterator & b ) {
return a . mIt = = b . mIt ;
}
friend bool operator ! = ( const Iterator & a , const Iterator & b ) {
return a . mIt ! = b . mIt ;
}
private :
M : : const_iterator mIt ;
} ;
public :
explicit SelectValueFromVarIdMapRange ( const M * m )
2021-08-07 20:51:18 +02:00
: mMap ( m ) { }
2021-02-10 08:18:21 +01:00
Iterator begin ( ) const {
return Iterator ( mMap - > begin ( ) ) ;
}
Iterator end ( ) const {
return Iterator ( mMap - > end ( ) ) ;
}
private :
const M * mMap ;
} ;
2020-05-19 21:03:28 +02:00
// Check if its an alias of the variable or is being aliased to this variable
2021-02-10 08:18:21 +01:00
template < typename V >
static bool isAliasOf ( const Variable * var , const Token * tok , nonneg int varid , const V & values , bool * inconclusive = nullptr )
2020-05-19 21:03:28 +02:00
{
if ( tok - > varId ( ) = = varid )
return false ;
if ( tok - > varId ( ) = = 0 )
return false ;
2020-09-20 14:27:09 +02:00
if ( isAliasOf ( tok , varid , inconclusive ) )
2019-08-29 08:38:50 +02:00
return true ;
2020-02-16 16:02:22 +01:00
if ( var & & ! var - > isPointer ( ) )
2019-08-29 08:38:50 +02:00
return false ;
// Search through non value aliases
for ( const ValueFlow : : Value & val : values ) {
if ( ! val . isNonValue ( ) )
continue ;
2019-09-11 19:25:09 +02:00
if ( val . isInconclusive ( ) )
continue ;
2019-08-29 08:38:50 +02:00
if ( val . isLifetimeValue ( ) & & ! val . isLocalLifetimeValue ( ) )
continue ;
if ( val . isLifetimeValue ( ) & & val . lifetimeKind ! = ValueFlow : : Value : : LifetimeKind : : Address )
continue ;
if ( ! Token : : Match ( val . tokvalue , " .|&|*|%var% " ) )
continue ;
if ( astHasVar ( val . tokvalue , tok - > varId ( ) ) )
return true ;
}
return false ;
}
2021-01-18 10:12:07 +01:00
static bool bifurcate ( const Token * tok , const std : : set < nonneg int > & varids , const Settings * settings , int depth = 20 ) ;
static bool bifurcateVariableChanged ( const Variable * var ,
const std : : set < nonneg int > & varids ,
const Token * start ,
const Token * end ,
const Settings * settings ,
int depth = 20 )
{
bool result = false ;
const Token * tok = start ;
while ( ( tok = findVariableChanged (
2021-08-07 20:51:18 +02:00
tok - > next ( ) , end , var - > isPointer ( ) , var - > declarationId ( ) , var - > isGlobal ( ) , settings , true ) ) ) {
2021-01-18 10:12:07 +01:00
if ( Token : : Match ( tok - > astParent ( ) , " %assign% " ) ) {
if ( ! bifurcate ( tok - > astParent ( ) - > astOperand2 ( ) , varids , settings , depth - 1 ) )
return true ;
} else {
result = true ;
}
}
return result ;
}
static bool bifurcate ( const Token * tok , const std : : set < nonneg int > & varids , const Settings * settings , int depth )
2020-01-05 16:25:33 +01:00
{
if ( depth < 0 )
return false ;
if ( ! tok )
return true ;
if ( tok - > hasKnownIntValue ( ) )
return true ;
if ( Token : : Match ( tok , " %cop% " ) )
2020-01-17 03:17:26 +01:00
return bifurcate ( tok - > astOperand1 ( ) , varids , settings , depth ) & & bifurcate ( tok - > astOperand2 ( ) , varids , settings , depth ) ;
2020-01-05 16:25:33 +01:00
if ( Token : : Match ( tok , " %var% " ) ) {
if ( varids . count ( tok - > varId ( ) ) > 0 )
return true ;
const Variable * var = tok - > variable ( ) ;
if ( ! var )
return false ;
const Token * start = var - > declEndToken ( ) ;
if ( ! start )
return false ;
2020-01-17 03:17:26 +01:00
if ( start - > strAt ( - 1 ) = = " ) " | | start - > strAt ( - 1 ) = = " } " )
return false ;
2020-01-05 16:25:33 +01:00
if ( Token : : Match ( start , " ; %varid% = " , var - > declarationId ( ) ) )
start = start - > tokAt ( 2 ) ;
2021-01-18 10:12:07 +01:00
if ( var - > isConst ( ) | | ! bifurcateVariableChanged ( var , varids , start , tok , settings , depth ) )
2020-01-05 16:25:33 +01:00
return var - > isArgument ( ) | | bifurcate ( start - > astOperand2 ( ) , varids , settings , depth - 1 ) ;
return false ;
}
return false ;
}
2020-11-10 16:00:55 +01:00
struct ValueFlowAnalyzer : Analyzer {
2020-02-16 16:02:22 +01:00
const TokenList * tokenlist ;
2020-02-19 07:55:04 +01:00
ProgramMemoryState pms ;
2020-02-13 16:27:06 +01:00
2021-08-23 09:03:48 +02:00
ValueFlowAnalyzer ( ) : tokenlist ( nullptr ) , pms ( nullptr ) { }
2020-02-16 16:02:22 +01:00
2021-08-23 09:03:48 +02:00
explicit ValueFlowAnalyzer ( const TokenList * t ) : tokenlist ( t ) , pms ( tokenlist - > getSettings ( ) ) { }
2020-02-16 16:02:22 +01:00
2020-03-01 16:46:20 +01:00
virtual const ValueFlow : : Value * getValue ( const Token * tok ) const = 0 ;
virtual ValueFlow : : Value * getValue ( const Token * tok ) = 0 ;
virtual void makeConditional ( ) = 0 ;
virtual void addErrorPath ( const Token * tok , const std : : string & s ) = 0 ;
2020-02-16 16:02:22 +01:00
virtual bool match ( const Token * tok ) const = 0 ;
2021-10-30 07:43:37 +02:00
virtual bool internalMatch ( const Token * ) const {
return false ;
}
2020-09-20 14:27:09 +02:00
virtual bool isAlias ( const Token * tok , bool & inconclusive ) const = 0 ;
2020-02-16 16:02:22 +01:00
2022-02-06 20:15:45 +01:00
using ProgramState = ProgramMemory : : Map ;
2020-02-16 16:02:22 +01:00
virtual ProgramState getProgramState ( ) const = 0 ;
2020-03-01 16:46:20 +01:00
virtual int getIndirect ( const Token * tok ) const {
const ValueFlow : : Value * value = getValue ( tok ) ;
if ( value )
return value - > indirect ;
return 0 ;
}
2020-02-16 16:02:22 +01:00
virtual bool isGlobal ( ) const {
return false ;
}
2021-07-18 10:01:22 +02:00
virtual bool dependsOnThis ( ) const {
return false ;
}
2021-10-23 14:47:10 +02:00
virtual bool isVariable ( ) const {
return false ;
}
2020-02-16 16:02:22 +01:00
virtual bool invalid ( ) const {
return false ;
}
bool isCPP ( ) const {
return tokenlist - > isCPP ( ) ;
}
const Settings * getSettings ( ) const {
return tokenlist - > getSettings ( ) ;
}
2021-08-21 18:52:11 +02:00
struct ConditionState {
bool dependent = true ;
bool unknown = true ;
bool isUnknownDependent ( ) const {
return unknown & & dependent ;
}
} ;
std : : unordered_map < nonneg int , const Token * > getSymbols ( const Token * tok ) const
{
std : : unordered_map < nonneg int , const Token * > result ;
if ( ! tok )
return result ;
for ( const ValueFlow : : Value & v : tok - > values ( ) ) {
if ( ! v . isSymbolicValue ( ) )
continue ;
if ( v . isImpossible ( ) )
continue ;
if ( ! v . tokvalue )
continue ;
if ( v . tokvalue - > exprId ( ) = = 0 )
continue ;
if ( match ( v . tokvalue ) )
continue ;
result [ v . tokvalue - > exprId ( ) ] = v . tokvalue ;
}
return result ;
}
ConditionState analyzeCondition ( const Token * tok , int depth = 20 ) const
{
ConditionState result ;
if ( ! tok )
return result ;
if ( depth < 0 )
return result ;
depth - - ;
if ( analyze ( tok , Direction : : Forward ) . isRead ( ) ) {
result . dependent = true ;
result . unknown = false ;
return result ;
} else if ( tok - > hasKnownIntValue ( ) | | tok - > isLiteral ( ) ) {
result . dependent = false ;
result . unknown = false ;
return result ;
} else if ( Token : : Match ( tok , " %cop% " ) ) {
if ( isLikelyStream ( isCPP ( ) , tok - > astOperand1 ( ) ) ) {
result . dependent = false ;
return result ;
}
ConditionState lhs = analyzeCondition ( tok - > astOperand1 ( ) , depth - 1 ) ;
if ( lhs . isUnknownDependent ( ) )
return lhs ;
ConditionState rhs = analyzeCondition ( tok - > astOperand2 ( ) , depth - 1 ) ;
if ( rhs . isUnknownDependent ( ) )
return rhs ;
if ( Token : : Match ( tok , " %comp% " ) )
result . dependent = lhs . dependent & & rhs . dependent ;
else
result . dependent = lhs . dependent | | rhs . dependent ;
result . unknown = lhs . unknown | | rhs . unknown ;
return result ;
} else if ( Token : : Match ( tok - > previous ( ) , " %name% ( " ) ) {
std : : vector < const Token * > args = getArguments ( tok - > previous ( ) ) ;
if ( Token : : Match ( tok - > tokAt ( - 2 ) , " . %name% ( " ) ) {
args . push_back ( tok - > tokAt ( - 2 ) - > astOperand1 ( ) ) ;
}
result . dependent = std : : any_of ( args . begin ( ) , args . end ( ) , [ & ] ( const Token * arg ) {
ConditionState cs = analyzeCondition ( arg , depth - 1 ) ;
return cs . dependent ;
} ) ;
if ( result . dependent ) {
// Check if we can evaluate the function
if ( ! evaluate ( Evaluate : : Integral , tok ) . empty ( ) )
result . unknown = false ;
}
return result ;
} else {
std : : unordered_map < nonneg int , const Token * > symbols = getSymbols ( tok ) ;
result . dependent = false ;
for ( auto & & p : symbols ) {
const Token * arg = p . second ;
ConditionState cs = analyzeCondition ( arg , depth - 1 ) ;
result . dependent = cs . dependent ;
if ( result . dependent )
break ;
}
if ( result . dependent ) {
// Check if we can evaluate the token
if ( ! evaluate ( Evaluate : : Integral , tok ) . empty ( ) )
result . unknown = false ;
}
return result ;
}
}
2020-02-16 16:02:22 +01:00
virtual Action isModified ( const Token * tok ) const {
Action read = Action : : Read ;
bool inconclusive = false ;
2020-03-01 16:46:20 +01:00
if ( isVariableChangedByFunctionCall ( tok , getIndirect ( tok ) , getSettings ( ) , & inconclusive ) )
2020-02-16 16:02:22 +01:00
return read | Action : : Invalid ;
if ( inconclusive )
return read | Action : : Inconclusive ;
2020-03-01 16:46:20 +01:00
if ( isVariableChanged ( tok , getIndirect ( tok ) , getSettings ( ) , isCPP ( ) ) ) {
2020-02-16 16:02:22 +01:00
if ( Token : : Match ( tok - > astParent ( ) , " *|[|.|++|-- " ) )
return read | Action : : Invalid ;
2020-11-10 16:00:55 +01:00
const ValueFlow : : Value * value = getValue ( tok ) ;
// Check if its assigned to the same value
if ( value & & ! value - > isImpossible ( ) & & Token : : simpleMatch ( tok - > astParent ( ) , " = " ) & & astIsLHS ( tok ) & &
astIsIntegral ( tok - > astParent ( ) - > astOperand2 ( ) , false ) ) {
2022-03-12 06:15:35 +01:00
std : : vector < MathLib : : bigint > result =
evaluateInt ( tok - > astParent ( ) - > astOperand2 ( ) , [ & ] {
return ProgramMemory { getProgramState ( ) } ;
} ) ;
2020-11-10 16:00:55 +01:00
if ( ! result . empty ( ) & & value - > equalTo ( result . front ( ) ) )
return Action : : Idempotent ;
}
2020-02-16 16:02:22 +01:00
return Action : : Invalid ;
}
return read ;
}
virtual Action isAliasModified ( const Token * tok ) const {
2021-09-04 19:05:41 +02:00
// Lambda function call
if ( Token : : Match ( tok , " %var% ( " ) )
// TODO: Check if modified in the lambda function
return Action : : Invalid ;
2020-02-16 16:02:22 +01:00
int indirect = 0 ;
if ( tok - > valueType ( ) )
2021-07-18 07:46:31 +02:00
indirect = tok - > valueType ( ) - > pointer ;
2020-02-16 16:02:22 +01:00
if ( isVariableChanged ( tok , indirect , getSettings ( ) , isCPP ( ) ) )
return Action : : Invalid ;
return Action : : None ;
2020-02-13 17:04:05 +01:00
}
2020-02-13 16:27:06 +01:00
2021-07-18 10:01:22 +02:00
virtual Action isThisModified ( const Token * tok ) const {
2021-07-18 07:46:31 +02:00
if ( isThisChanged ( tok , 0 , getSettings ( ) , isCPP ( ) ) )
return Action : : Invalid ;
return Action : : None ;
}
2021-11-25 22:34:00 +01:00
Action isGlobalModified ( const Token * tok ) const
{
if ( tok - > function ( ) ) {
if ( ! tok - > function ( ) - > isConstexpr ( ) & & ! isConstFunctionCall ( tok , getSettings ( ) - > library ) )
return Action : : Invalid ;
} else if ( getSettings ( ) - > library . getFunction ( tok ) ) {
// Assume library function doesn't modify user-global variables
return Action : : None ;
2022-04-27 14:08:44 +02:00
} else if ( Token : : simpleMatch ( tok - > astParent ( ) , " . " ) & & astIsContainer ( tok - > astParent ( ) - > astOperand1 ( ) ) ) {
// Assume container member function doesn't modify user-global variables
return Action : : None ;
2021-11-25 22:34:00 +01:00
} else if ( tok - > tokType ( ) = = Token : : eType & & astIsPrimitive ( tok - > next ( ) ) ) {
2022-04-27 14:08:44 +02:00
// Function cast does not modify global variables
2021-11-25 22:34:00 +01:00
return Action : : None ;
2022-04-27 14:08:44 +02:00
} else if ( ! tok - > isKeyword ( ) & & Token : : Match ( tok , " %name% ( " ) ) {
2021-11-25 22:34:00 +01:00
return Action : : Invalid ;
}
return Action : : None ;
}
2021-08-09 07:45:41 +02:00
static const std : : string & getAssign ( const Token * tok , Direction d )
{
if ( d = = Direction : : Forward )
return tok - > str ( ) ;
else
return invertAssign ( tok - > str ( ) ) ;
}
2020-11-11 09:15:36 +01:00
virtual Action isWritable ( const Token * tok , Direction d ) const {
2020-08-08 07:10:03 +02:00
const ValueFlow : : Value * value = getValue ( tok ) ;
if ( ! value )
return Action : : None ;
2022-02-24 06:50:34 +01:00
if ( ! ( value - > isIntValue ( ) | | value - > isFloatValue ( ) | | value - > isSymbolicValue ( ) | | value - > isLifetimeValue ( ) ) )
2020-08-08 07:10:03 +02:00
return Action : : None ;
const Token * parent = tok - > astParent ( ) ;
2021-08-28 09:28:29 +02:00
// Only if its invertible
if ( value - > isImpossible ( ) & & ! Token : : Match ( parent , " +=|-=|*=|++|-- " ) )
return Action : : None ;
2022-02-24 06:50:34 +01:00
if ( value - > isLifetimeValue ( ) ) {
if ( value - > lifetimeKind ! = ValueFlow : : Value : : LifetimeKind : : Iterator )
return Action : : None ;
if ( ! Token : : Match ( parent , " ++|--|+= " ) )
return Action : : None ;
return Action : : Read | Action : : Write ;
}
2020-08-08 07:10:03 +02:00
if ( parent & & parent - > isAssignmentOp ( ) & & astIsLHS ( tok ) & &
parent - > astOperand2 ( ) - > hasKnownValue ( ) ) {
const Token * rhs = parent - > astOperand2 ( ) ;
2021-08-09 07:45:41 +02:00
const ValueFlow : : Value * rhsValue = rhs - > getKnownValue ( ValueFlow : : Value : : ValueType : : INT ) ;
2020-08-08 07:10:03 +02:00
Action a ;
2021-08-09 07:45:41 +02:00
if ( ! rhsValue | | ! evalAssignment ( * value , getAssign ( parent , d ) , * rhsValue ) )
2020-08-08 07:10:03 +02:00
a = Action : : Invalid ;
else
a = Action : : Write ;
2020-11-10 16:00:55 +01:00
if ( parent - > str ( ) ! = " = " ) {
2020-08-08 07:10:03 +02:00
a | = Action : : Read ;
2020-11-10 16:00:55 +01:00
} else {
if ( rhsValue & & ! value - > isImpossible ( ) & & value - > equalValue ( * rhsValue ) )
a = Action : : Idempotent ;
2021-01-21 19:50:57 +01:00
a | = Action : : Incremental ;
2020-11-10 16:00:55 +01:00
}
2020-08-08 07:10:03 +02:00
return a ;
}
// increment/decrement
2021-06-04 21:40:57 +02:00
if ( Token : : Match ( tok - > astParent ( ) , " ++|-- " ) ) {
2021-01-21 19:50:57 +01:00
return Action : : Read | Action : : Write | Action : : Incremental ;
2020-08-08 07:10:03 +02:00
}
return Action : : None ;
}
2020-11-11 09:15:36 +01:00
virtual void writeValue ( ValueFlow : : Value * value , const Token * tok , Direction d ) const {
2020-08-08 07:10:03 +02:00
if ( ! value )
return ;
if ( ! tok - > astParent ( ) )
return ;
2022-02-24 06:50:34 +01:00
// Lifetime value doesn't change
if ( value - > isLifetimeValue ( ) )
return ;
2020-08-08 07:10:03 +02:00
if ( tok - > astParent ( ) - > isAssignmentOp ( ) ) {
2021-08-09 07:45:41 +02:00
const ValueFlow : : Value * rhsValue =
tok - > astParent ( ) - > astOperand2 ( ) - > getKnownValue ( ValueFlow : : Value : : ValueType : : INT ) ;
assert ( rhsValue ) ;
if ( evalAssignment ( * value , getAssign ( tok - > astParent ( ) , d ) , * rhsValue ) ) {
2020-08-08 07:10:03 +02:00
const std : : string info ( " Compound assignment ' " + tok - > astParent ( ) - > str ( ) + " ', assigned value is " +
value - > infoString ( ) ) ;
if ( tok - > astParent ( ) - > str ( ) = = " = " )
value - > errorPath . clear ( ) ;
value - > errorPath . emplace_back ( tok , info ) ;
} else {
2021-08-09 07:45:41 +02:00
assert ( false & & " Writable value cannot be evaluated " ) ;
2020-08-08 07:10:03 +02:00
// TODO: Don't set to zero
value - > intvalue = 0 ;
}
} else if ( tok - > astParent ( ) - > tokType ( ) = = Token : : eIncDecOp ) {
2020-11-10 16:00:55 +01:00
bool inc = tok - > astParent ( ) - > str ( ) = = " ++ " ;
std : : string opName ( inc ? " incremented " : " decremented " ) ;
if ( d = = Direction : : Reverse )
inc = ! inc ;
2020-08-08 07:10:03 +02:00
value - > intvalue + = ( inc ? 1 : - 1 ) ;
2020-11-10 16:00:55 +01:00
const std : : string info ( tok - > str ( ) + " is " + opName + " ', new value is " + value - > infoString ( ) ) ;
2020-08-08 07:10:03 +02:00
value - > errorPath . emplace_back ( tok , info ) ;
}
}
2021-08-07 20:51:18 +02:00
virtual bool useSymbolicValues ( ) const {
return true ;
}
2021-08-04 21:07:31 +02:00
2021-10-11 19:10:37 +02:00
const Token * findMatch ( const Token * tok ) const
{
return findAstNode ( tok , [ & ] ( const Token * child ) {
return match ( child ) ;
} ) ;
}
bool isSameSymbolicValue ( const Token * tok , ValueFlow : : Value * value = nullptr ) const
2021-08-04 21:07:31 +02:00
{
if ( ! useSymbolicValues ( ) )
return false ;
2021-10-06 08:39:58 +02:00
if ( Token : : Match ( tok , " %assign% " ) )
return false ;
2021-10-11 19:10:37 +02:00
const ValueFlow : : Value * currValue = getValue ( tok ) ;
if ( ! currValue )
return false ;
2022-02-11 06:52:28 +01:00
// If the same symbolic value is already there then skip
if ( currValue - > isSymbolicValue ( ) & &
std : : any_of ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , [ & ] ( const ValueFlow : : Value & v ) {
return v . isSymbolicValue ( ) & & currValue - > equalValue ( v ) ;
} ) )
return false ;
2022-04-14 07:35:07 +02:00
const bool isPoint = currValue - > bound = = ValueFlow : : Value : : Bound : : Point & & currValue - > isIntValue ( ) ;
2021-10-11 19:10:37 +02:00
const bool exact = ! currValue - > isIntValue ( ) | | currValue - > isImpossible ( ) ;
2021-08-04 21:07:31 +02:00
for ( const ValueFlow : : Value & v : tok - > values ( ) ) {
if ( ! v . isSymbolicValue ( ) )
continue ;
2022-02-11 06:52:28 +01:00
if ( currValue - > equalValue ( v ) )
continue ;
2021-10-11 19:10:37 +02:00
const bool toImpossible = v . isImpossible ( ) & & currValue - > isKnown ( ) ;
if ( ! v . isKnown ( ) & & ! toImpossible )
2021-08-04 21:07:31 +02:00
continue ;
2022-04-14 07:35:07 +02:00
if ( exact & & v . intvalue ! = 0 & & ! isPoint )
2021-08-04 21:07:31 +02:00
continue ;
2021-10-11 19:10:37 +02:00
std : : vector < MathLib : : bigint > r ;
ValueFlow : : Value : : Bound bound = currValue - > bound ;
2021-08-04 21:07:31 +02:00
if ( match ( v . tokvalue ) ) {
2021-10-11 19:10:37 +02:00
r = { currValue - > intvalue } ;
} else if ( ! exact & & findMatch ( v . tokvalue ) ) {
r = evaluate ( Evaluate : : Integral , v . tokvalue , tok ) ;
if ( bound = = ValueFlow : : Value : : Bound : : Point )
bound = v . bound ;
}
if ( ! r . empty ( ) ) {
if ( value ) {
value - > errorPath . insert ( value - > errorPath . end ( ) , v . errorPath . begin ( ) , v . errorPath . end ( ) ) ;
value - > intvalue = r . front ( ) + v . intvalue ;
if ( toImpossible )
value - > setImpossible ( ) ;
value - > bound = bound ;
}
2021-08-04 21:07:31 +02:00
return true ;
}
}
return false ;
}
2021-02-09 15:27:46 +01:00
Action analyzeMatch ( const Token * tok , Direction d ) const {
const Token * parent = tok - > astParent ( ) ;
2021-11-25 22:34:00 +01:00
if ( d = = Direction : : Reverse & & isGlobal ( ) & & ! dependsOnThis ( ) & & Token : : Match ( parent , " . %name% ( " ) ) {
Action a = isGlobalModified ( parent - > next ( ) ) ;
if ( a ! = Action : : None )
return a ;
}
2021-10-06 08:39:58 +02:00
if ( ( astIsPointer ( tok ) | | astIsSmartPointer ( tok ) ) & &
( Token : : Match ( parent , " *|[ " ) | | ( parent & & parent - > originalName ( ) = = " -> " ) ) & & getIndirect ( tok ) < = 0 )
2021-02-09 15:27:46 +01:00
return Action : : Read ;
Action w = isWritable ( tok , d ) ;
if ( w ! = Action : : None )
return w ;
// Check for modifications by function calls
return isModified ( tok ) ;
}
2021-02-10 11:42:00 +01:00
Action analyzeToken ( const Token * ref , const Token * tok , Direction d , bool inconclusiveRef ) const {
2021-02-09 15:27:46 +01:00
if ( ! ref )
return Action : : None ;
// If its an inconclusiveRef then ref != tok
assert ( ! inconclusiveRef | | ref ! = tok ) ;
2020-09-20 14:27:09 +02:00
bool inconclusive = false ;
2021-02-09 15:27:46 +01:00
if ( match ( ref ) ) {
if ( inconclusiveRef ) {
Action a = isModified ( tok ) ;
if ( a . isModified ( ) | | a . isInconclusive ( ) )
return Action : : Inconclusive ;
} else {
return analyzeMatch ( tok , d ) | Action : : Match ;
}
2022-03-24 06:35:44 +01:00
} else if ( ref - > isUnaryOp ( " * " ) & & ! match ( ref - > astOperand1 ( ) ) ) {
2020-07-20 06:25:35 +02:00
const Token * lifeTok = nullptr ;
2021-02-09 15:27:46 +01:00
for ( const ValueFlow : : Value & v : ref - > astOperand1 ( ) - > values ( ) ) {
2020-07-20 06:25:35 +02:00
if ( ! v . isLocalLifetimeValue ( ) )
continue ;
if ( lifeTok )
return Action : : None ;
lifeTok = v . tokvalue ;
}
if ( lifeTok & & match ( lifeTok ) ) {
Action a = Action : : Read ;
if ( isModified ( tok ) . isModified ( ) )
a = Action : : Invalid ;
if ( Token : : Match ( tok - > astParent ( ) , " %assign% " ) & & astIsLHS ( tok ) )
2021-01-18 10:12:07 +01:00
a | = Action : : Invalid ;
2021-02-09 15:27:46 +01:00
if ( inconclusiveRef & & a . isModified ( ) )
return Action : : Inconclusive ;
2020-07-20 06:25:35 +02:00
return a ;
}
return Action : : None ;
2021-02-09 15:27:46 +01:00
} else if ( isAlias ( ref , inconclusive ) ) {
inconclusive | = inconclusiveRef ;
2020-09-20 14:27:09 +02:00
Action a = isAliasModified ( tok ) ;
if ( inconclusive & & a . isModified ( ) )
return Action : : Inconclusive ;
else
return a ;
2021-08-04 21:07:31 +02:00
} else if ( isSameSymbolicValue ( ref ) ) {
return Action : : Read | Action : : SymbolicMatch ;
2021-02-09 15:27:46 +01:00
}
return Action : : None ;
}
2022-02-10 23:02:24 +01:00
virtual Action analyze ( const Token * tok , Direction d ) const override {
2021-02-09 15:27:46 +01:00
if ( invalid ( ) )
return Action : : Invalid ;
// Follow references
std : : vector < ReferenceToken > refs = followAllReferences ( tok ) ;
const bool inconclusiveRefs = refs . size ( ) ! = 1 ;
2021-11-01 19:23:15 +01:00
if ( std : : none_of ( refs . begin ( ) , refs . end ( ) , [ & ] ( const ReferenceToken & ref ) {
return tok = = ref . token ;
} ) )
refs . push_back ( ReferenceToken { tok , { } } ) ;
2021-02-10 11:42:00 +01:00
for ( const ReferenceToken & ref : refs ) {
2021-11-01 19:23:15 +01:00
Action a = analyzeToken ( ref . token , tok , d , inconclusiveRefs & & ref . token ! = tok ) ;
2021-10-30 07:43:37 +02:00
if ( internalMatch ( ref . token ) )
a | = Action : : Internal ;
2021-02-09 15:27:46 +01:00
if ( a ! = Action : : None )
return a ;
}
2021-10-23 14:47:10 +02:00
if ( dependsOnThis ( ) & & exprDependsOnThis ( tok , ! isVariable ( ) ) )
2021-07-18 07:46:31 +02:00
return isThisModified ( tok ) ;
// bailout: global non-const variables
2021-10-15 10:57:40 +02:00
if ( isGlobal ( ) & & ! dependsOnThis ( ) & & Token : : Match ( tok , " %name% ( " ) & &
! Token : : simpleMatch ( tok - > linkAt ( 1 ) , " ) { " ) ) {
2021-11-25 22:34:00 +01:00
return isGlobalModified ( tok ) ;
2014-06-25 16:00:56 +02:00
}
2020-02-13 16:27:06 +01:00
return Action : : None ;
}
2020-02-16 16:02:22 +01:00
2022-03-12 06:15:35 +01:00
template < class F >
std : : vector < MathLib : : bigint > evaluateInt ( const Token * tok , F getProgramMemory ) const
{
if ( tok - > hasKnownIntValue ( ) )
return { static_cast < int > ( tok - > values ( ) . front ( ) . intvalue ) } ;
std : : vector < MathLib : : bigint > result ;
ProgramMemory pm = getProgramMemory ( ) ;
if ( Token : : Match ( tok , " &&|%oror% " ) ) {
if ( conditionIsTrue ( tok , pm , getSettings ( ) ) )
result . push_back ( 1 ) ;
if ( conditionIsFalse ( tok , pm , getSettings ( ) ) )
result . push_back ( 0 ) ;
} else {
MathLib : : bigint out = 0 ;
bool error = false ;
execute ( tok , & pm , & out , & error , getSettings ( ) ) ;
if ( ! error )
result . push_back ( out ) ;
}
return result ;
}
2022-02-10 23:02:24 +01:00
virtual std : : vector < MathLib : : bigint > evaluate ( Evaluate e , const Token * tok , const Token * ctx = nullptr ) const override
2021-10-11 19:10:37 +02:00
{
2021-07-16 18:49:07 +02:00
if ( e = = Evaluate : : Integral ) {
2022-03-12 06:15:35 +01:00
return evaluateInt ( tok , [ & ] {
return pms . get ( tok , ctx , getProgramState ( ) ) ;
} ) ;
2021-07-16 18:49:07 +02:00
} else if ( e = = Evaluate : : ContainerEmpty ) {
const ValueFlow : : Value * value = ValueFlow : : findValue ( tok - > values ( ) , nullptr , [ ] ( const ValueFlow : : Value & v ) {
return v . isKnown ( ) & & v . isContainerSizeValue ( ) ;
} ) ;
if ( value )
return { value - > intvalue = = 0 } ;
ProgramMemory pm = pms . get ( tok , ctx , getProgramState ( ) ) ;
2020-11-10 16:00:55 +01:00
MathLib : : bigint out = 0 ;
2021-07-16 18:49:07 +02:00
if ( pm . getContainerEmptyValue ( tok - > exprId ( ) , & out ) )
return { static_cast < int > ( out ) } ;
return { } ;
} else {
return { } ;
2020-11-10 16:00:55 +01:00
}
2020-02-13 16:27:06 +01:00
}
2020-03-01 16:46:20 +01:00
2022-02-10 23:02:24 +01:00
virtual void assume ( const Token * tok , bool state , unsigned int flags ) override {
2020-03-01 16:46:20 +01:00
// Update program state
pms . removeModifiedVars ( tok ) ;
pms . addState ( tok , getProgramState ( ) ) ;
2021-07-16 18:49:07 +02:00
pms . assume ( tok , state , flags & Assume : : ContainerEmpty ) ;
2020-03-01 16:46:20 +01:00
2021-06-09 09:20:43 +02:00
bool isCondBlock = false ;
const Token * parent = tok - > astParent ( ) ;
if ( parent ) {
isCondBlock = Token : : Match ( parent - > previous ( ) , " if|while ( " ) ;
}
if ( isCondBlock ) {
const Token * startBlock = parent - > link ( ) - > next ( ) ;
2021-08-27 05:46:57 +02:00
if ( Token : : simpleMatch ( startBlock , " ; " ) & & Token : : simpleMatch ( parent - > tokAt ( - 2 ) , " } while ( " ) )
startBlock = parent - > linkAt ( - 2 ) ;
2021-06-09 09:20:43 +02:00
const Token * endBlock = startBlock - > link ( ) ;
pms . removeModifiedVars ( endBlock ) ;
if ( state )
pms . addState ( endBlock - > previous ( ) , getProgramState ( ) ) ;
else if ( Token : : simpleMatch ( endBlock , " } else { " ) )
pms . addState ( endBlock - > linkAt ( 2 ) - > previous ( ) , getProgramState ( ) ) ;
}
2020-03-01 16:46:20 +01:00
2021-06-09 09:20:43 +02:00
if ( ! ( flags & Assume : : Quiet ) ) {
2021-07-16 18:49:07 +02:00
if ( flags & Assume : : ContainerEmpty ) {
std : : string s = state ? " empty " : " not empty " ;
addErrorPath ( tok , " Assuming container is " + s ) ;
} else {
std : : string s = state ? " true " : " false " ;
addErrorPath ( tok , " Assuming condition is " + s ) ;
}
2020-03-01 16:46:20 +01:00
}
2021-06-09 09:20:43 +02:00
if ( ! ( flags & Assume : : Absolute ) )
2020-03-01 16:46:20 +01:00
makeConditional ( ) ;
}
2021-10-30 07:43:37 +02:00
virtual void internalUpdate ( Token * , const ValueFlow : : Value & , Direction )
{
assert ( false & & " Internal update unimplemented. " ) ;
}
2022-02-10 23:02:24 +01:00
virtual void update ( Token * tok , Action a , Direction d ) override {
2020-03-01 16:46:20 +01:00
ValueFlow : : Value * value = getValue ( tok ) ;
if ( ! value )
return ;
2021-08-04 21:07:31 +02:00
ValueFlow : : Value localValue ;
if ( a . isSymbolicMatch ( ) ) {
// Make a copy of the value to modify it
localValue = * value ;
value = & localValue ;
2021-10-11 19:10:37 +02:00
isSameSymbolicValue ( tok , & localValue ) ;
2021-08-04 21:07:31 +02:00
}
2021-10-30 07:43:37 +02:00
if ( a . isInternal ( ) )
internalUpdate ( tok , * value , d ) ;
2020-11-10 16:00:55 +01:00
// Read first when moving forward
if ( d = = Direction : : Forward & & a . isRead ( ) )
2020-03-01 16:46:20 +01:00
setTokenValue ( tok , * value , getSettings ( ) ) ;
if ( a . isInconclusive ( ) )
lowerToInconclusive ( ) ;
2020-05-19 21:03:28 +02:00
if ( a . isWrite ( ) & & tok - > astParent ( ) ) {
2020-11-10 16:00:55 +01:00
writeValue ( value , tok , d ) ;
2020-03-01 16:46:20 +01:00
}
2020-11-10 16:00:55 +01:00
// Read last when moving in reverse
if ( d = = Direction : : Reverse & & a . isRead ( ) )
setTokenValue ( tok , * value , getSettings ( ) ) ;
2020-03-01 16:46:20 +01:00
}
2020-11-10 16:00:55 +01:00
2022-02-10 23:02:24 +01:00
virtual ValuePtr < Analyzer > reanalyze ( Token * , const std : : string & ) const override {
2020-11-11 09:15:36 +01:00
return { } ;
}
2020-02-16 16:02:22 +01:00
} ;
2021-08-09 20:41:10 +02:00
ValuePtr < Analyzer > makeAnalyzer ( const Token * exprTok , ValueFlow : : Value value , const TokenList * tokenlist ) ;
2020-11-10 16:00:55 +01:00
struct SingleValueFlowAnalyzer : ValueFlowAnalyzer {
2020-05-19 21:03:28 +02:00
std : : unordered_map < nonneg int , const Variable * > varids ;
2020-07-20 06:25:35 +02:00
std : : unordered_map < nonneg int , const Variable * > aliases ;
2020-02-16 16:02:22 +01:00
ValueFlow : : Value value ;
2020-11-10 16:00:55 +01:00
SingleValueFlowAnalyzer ( ) : ValueFlowAnalyzer ( ) { }
2020-02-16 16:02:22 +01:00
2020-11-10 16:00:55 +01:00
SingleValueFlowAnalyzer ( const ValueFlow : : Value & v , const TokenList * t ) : ValueFlowAnalyzer ( t ) , value ( v ) { }
2020-02-16 16:02:22 +01:00
2020-05-19 21:03:28 +02:00
const std : : unordered_map < nonneg int , const Variable * > & getVars ( ) const {
return varids ;
}
2020-02-16 16:02:22 +01:00
2020-07-20 06:25:35 +02:00
const std : : unordered_map < nonneg int , const Variable * > & getAliasedVars ( ) const {
return aliases ;
}
2022-02-10 23:02:24 +01:00
virtual const ValueFlow : : Value * getValue ( const Token * ) const override {
2020-03-01 16:46:20 +01:00
return & value ;
}
2022-02-10 23:02:24 +01:00
virtual ValueFlow : : Value * getValue ( const Token * ) override {
2020-03-01 16:46:20 +01:00
return & value ;
}
2022-02-10 23:02:24 +01:00
virtual void makeConditional ( ) override {
2020-03-01 16:46:20 +01:00
value . conditional = true ;
}
2022-02-10 23:02:24 +01:00
virtual bool useSymbolicValues ( ) const override
2021-08-04 21:07:31 +02:00
{
if ( value . isUninitValue ( ) )
return false ;
2021-11-06 19:08:19 +01:00
if ( value . isLifetimeValue ( ) )
return false ;
2021-08-04 21:07:31 +02:00
return true ;
}
2022-02-10 23:02:24 +01:00
virtual void addErrorPath ( const Token * tok , const std : : string & s ) override {
2020-03-01 16:46:20 +01:00
value . errorPath . emplace_back ( tok , s ) ;
2020-02-16 16:02:22 +01:00
}
2022-02-10 23:02:24 +01:00
virtual bool isAlias ( const Token * tok , bool & inconclusive ) const override {
2020-02-16 16:02:22 +01:00
if ( value . isLifetimeValue ( ) )
return false ;
2020-07-23 10:09:06 +02:00
for ( const auto & m : {
2021-08-07 20:51:18 +02:00
std : : ref ( getVars ( ) ) , std : : ref ( getAliasedVars ( ) )
2020-07-23 10:09:06 +02:00
} ) {
2020-07-21 20:18:45 +02:00
for ( const auto & p : m . get ( ) ) {
2020-07-20 06:25:35 +02:00
nonneg int varid = p . first ;
const Variable * var = p . second ;
if ( tok - > varId ( ) = = varid )
return true ;
2021-02-10 08:18:21 +01:00
if ( isAliasOf ( var , tok , varid , MakeSingleRange ( value ) , & inconclusive ) )
2020-07-20 06:25:35 +02:00
return true ;
}
2020-02-16 16:02:22 +01:00
}
return false ;
}
2022-02-10 23:02:24 +01:00
virtual bool isGlobal ( ) const override {
2020-02-16 19:58:09 +01:00
for ( const auto & p : getVars ( ) ) {
2020-02-16 16:02:22 +01:00
const Variable * var = p . second ;
2020-08-28 19:26:09 +02:00
if ( ! var - > isLocal ( ) & & ! var - > isArgument ( ) & & ! var - > isConst ( ) )
2020-02-16 16:02:22 +01:00
return true ;
}
return false ;
}
2022-02-10 23:02:24 +01:00
virtual bool lowerToPossible ( ) override {
2020-02-13 16:27:06 +01:00
if ( value . isImpossible ( ) )
2014-06-18 05:51:23 +02:00
return false ;
2020-02-13 16:27:06 +01:00
value . changeKnownToPossible ( ) ;
return true ;
}
2022-02-10 23:02:24 +01:00
virtual bool lowerToInconclusive ( ) override {
2020-02-13 16:27:06 +01:00
if ( value . isImpossible ( ) )
2014-06-18 05:51:23 +02:00
return false ;
2020-02-13 16:27:06 +01:00
value . setInconclusive ( ) ;
return true ;
}
2014-06-15 16:47:01 +02:00
2022-02-10 23:02:24 +01:00
virtual bool isConditional ( ) const override {
2020-02-13 16:27:06 +01:00
if ( value . conditional )
return true ;
if ( value . condition )
2021-01-05 16:49:08 +01:00
return ! value . isKnown ( ) & & ! value . isImpossible ( ) ;
2020-02-13 16:27:06 +01:00
return false ;
}
2019-09-20 15:06:37 +02:00
2022-02-10 23:02:24 +01:00
virtual bool stopOnCondition ( const Token * condTok ) const override
2021-08-21 18:52:11 +02:00
{
if ( value . isNonValue ( ) )
return false ;
if ( value . isImpossible ( ) )
return false ;
if ( isConditional ( ) & & ! value . isKnown ( ) & & ! value . isImpossible ( ) )
return true ;
2021-10-06 08:39:58 +02:00
if ( value . isSymbolicValue ( ) )
return false ;
2021-08-21 18:52:11 +02:00
ConditionState cs = analyzeCondition ( condTok ) ;
return cs . isUnknownDependent ( ) ;
}
2022-02-10 23:02:24 +01:00
virtual bool updateScope ( const Token * endBlock , bool ) const override {
2020-02-13 16:27:06 +01:00
const Scope * scope = endBlock - > scope ( ) ;
if ( ! scope )
return false ;
if ( scope - > type = = Scope : : eLambda ) {
return value . isLifetimeValue ( ) ;
} else if ( scope - > type = = Scope : : eIf | | scope - > type = = Scope : : eElse | | scope - > type = = Scope : : eWhile | |
scope - > type = = Scope : : eFor ) {
if ( value . isKnown ( ) | | value . isImpossible ( ) )
return true ;
if ( value . isLifetimeValue ( ) )
return true ;
if ( isConditional ( ) )
2019-08-29 08:38:50 +02:00
return false ;
2020-02-13 16:27:06 +01:00
const Token * condTok = getCondTokFromEnd ( endBlock ) ;
2020-05-19 21:55:28 +02:00
std : : set < nonneg int > varids2 ;
std : : transform ( getVars ( ) . begin ( ) , getVars ( ) . end ( ) , std : : inserter ( varids2 , varids2 . begin ( ) ) , SelectMapKeys { } ) ;
return bifurcate ( condTok , varids2 , getSettings ( ) ) ;
2014-06-15 16:47:01 +02:00
}
2015-07-26 19:28:42 +02:00
2020-02-13 16:27:06 +01:00
return false ;
}
2020-11-10 16:00:55 +01:00
2022-02-10 23:02:24 +01:00
virtual ValuePtr < Analyzer > reanalyze ( Token * tok , const std : : string & msg ) const override {
2020-11-10 16:00:55 +01:00
ValueFlow : : Value newValue = value ;
newValue . errorPath . emplace_back ( tok , msg ) ;
return makeAnalyzer ( tok , newValue , tokenlist ) ;
}
2020-02-13 16:27:06 +01:00
} ;
2019-08-24 11:27:47 +02:00
2020-11-10 16:00:55 +01:00
struct ExpressionAnalyzer : SingleValueFlowAnalyzer {
2020-02-16 16:02:22 +01:00
const Token * expr ;
bool local ;
bool unknown ;
2021-07-18 07:46:31 +02:00
bool dependOnThis ;
2020-02-16 16:02:22 +01:00
2021-07-18 07:46:31 +02:00
ExpressionAnalyzer ( ) : SingleValueFlowAnalyzer ( ) , expr ( nullptr ) , local ( true ) , unknown ( false ) , dependOnThis ( false ) { }
2020-02-16 16:02:22 +01:00
2020-11-10 16:00:55 +01:00
ExpressionAnalyzer ( const Token * e , const ValueFlow : : Value & val , const TokenList * t )
2021-07-18 10:01:22 +02:00
: SingleValueFlowAnalyzer ( val , t ) , expr ( e ) , local ( true ) , unknown ( false ) , dependOnThis ( false ) {
2020-02-16 16:02:22 +01:00
2021-08-03 06:31:52 +02:00
assert ( e & & e - > exprId ( ) ! = 0 & & " Not a valid expression " ) ;
2021-07-18 07:46:31 +02:00
dependOnThis = exprDependsOnThis ( expr ) ;
setupExprVarIds ( expr ) ;
2021-07-30 21:29:35 +02:00
if ( val . isSymbolicValue ( ) )
setupExprVarIds ( val . tokvalue ) ;
2020-02-16 16:02:22 +01:00
}
2020-02-16 19:58:09 +01:00
static bool nonLocal ( const Variable * var , bool deref ) {
2021-07-18 07:46:31 +02:00
return ! var | | ( ! var - > isLocal ( ) & & ! var - > isArgument ( ) ) | | ( deref & & var - > isArgument ( ) & & var - > isPointer ( ) ) | |
var - > isStatic ( ) | | var - > isReference ( ) | | var - > isExtern ( ) ;
2020-02-16 16:02:22 +01:00
}
2021-07-18 10:01:22 +02:00
void setupExprVarIds ( const Token * start , int depth = 0 ) {
2021-07-18 07:46:31 +02:00
const int maxDepth = 4 ;
if ( depth > maxDepth )
return ;
visitAstNodes ( start , [ & ] ( const Token * tok ) {
2021-10-06 08:39:58 +02:00
const bool top = depth = = 0 & & tok = = start ;
2021-12-05 15:46:52 +01:00
const bool ispointer = astIsPointer ( tok ) | | astIsSmartPointer ( tok ) | | astIsIterator ( tok ) ;
2021-10-06 08:39:58 +02:00
if ( ! top | | ! ispointer | | value . indirect ! = 0 ) {
for ( const ValueFlow : : Value & v : tok - > values ( ) ) {
if ( ! ( v . isLocalLifetimeValue ( ) | | ( ispointer & & v . isSymbolicValue ( ) & & v . isKnown ( ) ) ) )
continue ;
if ( ! v . tokvalue )
continue ;
if ( v . tokvalue = = tok )
continue ;
setupExprVarIds ( v . tokvalue , depth + 1 ) ;
}
2021-07-18 07:46:31 +02:00
}
2022-04-28 10:48:37 +02:00
if ( depth = = 0 & & tok - > isIncompleteVar ( ) ) {
// TODO: Treat incomplete var as global, but we need to update
// the alias variables to just expr ids instead of requiring
// Variable
2020-02-16 16:02:22 +01:00
unknown = true ;
return ChildrenToVisit : : none ;
}
if ( tok - > varId ( ) > 0 ) {
varids [ tok - > varId ( ) ] = tok - > variable ( ) ;
if ( ! Token : : simpleMatch ( tok - > previous ( ) , " . " ) ) {
2021-07-18 07:46:31 +02:00
const Variable * var = tok - > variable ( ) ;
if ( var & & var - > isReference ( ) & & var - > isLocal ( ) & & Token : : Match ( var - > nameToken ( ) , " %var% [=(] " ) & &
! isGlobalData ( var - > nameToken ( ) - > next ( ) - > astOperand2 ( ) , isCPP ( ) ) )
2020-02-16 16:02:22 +01:00
return ChildrenToVisit : : none ;
2021-07-18 07:46:31 +02:00
const bool deref = tok - > astParent ( ) & &
( tok - > astParent ( ) - > isUnaryOp ( " * " ) | |
( tok - > astParent ( ) - > str ( ) = = " [ " & & tok = = tok - > astParent ( ) - > astOperand1 ( ) ) ) ;
2020-02-16 16:02:22 +01:00
local & = ! nonLocal ( tok - > variable ( ) , deref ) ;
}
}
return ChildrenToVisit : : op1_and_op2 ;
} ) ;
}
2022-02-10 23:02:24 +01:00
virtual bool invalid ( ) const override {
2020-02-16 16:02:22 +01:00
return unknown ;
}
2022-02-10 23:02:24 +01:00
virtual ProgramState getProgramState ( ) const override {
2021-01-21 19:49:37 +01:00
ProgramState ps ;
2022-02-06 20:15:45 +01:00
ps [ expr ] = value ;
2021-01-21 19:49:37 +01:00
return ps ;
2020-02-16 16:02:22 +01:00
}
2022-02-10 23:02:24 +01:00
virtual bool match ( const Token * tok ) const override {
2021-07-18 10:01:22 +02:00
return tok - > exprId ( ) = = expr - > exprId ( ) ;
}
2021-07-18 07:46:31 +02:00
2022-02-10 23:02:24 +01:00
virtual bool dependsOnThis ( ) const override {
2021-07-18 10:01:22 +02:00
return dependOnThis ;
}
2020-02-16 16:02:22 +01:00
2022-02-10 23:02:24 +01:00
virtual bool isGlobal ( ) const override {
2020-02-16 16:02:22 +01:00
return ! local ;
}
2021-10-23 14:47:10 +02:00
2022-02-10 23:02:24 +01:00
virtual bool isVariable ( ) const override {
2021-10-23 14:47:10 +02:00
return expr - > varId ( ) > 0 ;
}
2020-02-16 16:02:22 +01:00
} ;
2021-01-11 07:56:16 +01:00
struct OppositeExpressionAnalyzer : ExpressionAnalyzer {
bool isNot ;
OppositeExpressionAnalyzer ( ) : ExpressionAnalyzer ( ) , isNot ( false ) { }
OppositeExpressionAnalyzer ( bool pIsNot , const Token * e , const ValueFlow : : Value & val , const TokenList * t )
2021-01-12 21:28:56 +01:00
: ExpressionAnalyzer ( e , val , t ) , isNot ( pIsNot )
{ }
2021-01-11 07:56:16 +01:00
2022-02-10 23:02:24 +01:00
virtual bool match ( const Token * tok ) const override {
2021-01-11 07:56:16 +01:00
return isOppositeCond ( isNot , isCPP ( ) , expr , tok , getSettings ( ) - > library , true , true ) ;
}
} ;
2021-10-21 14:44:48 +02:00
struct SubExpressionAnalyzer : ExpressionAnalyzer {
2021-10-30 07:43:37 +02:00
using PartialReadContainer = std : : vector < std : : pair < Token * , ValueFlow : : Value > > ;
// A shared_ptr is used so partial reads can be captured even after forking
std : : shared_ptr < PartialReadContainer > partialReads ;
SubExpressionAnalyzer ( ) : ExpressionAnalyzer ( ) , partialReads ( nullptr ) { }
2021-10-21 14:44:48 +02:00
SubExpressionAnalyzer ( const Token * e , const ValueFlow : : Value & val , const TokenList * t )
2021-10-30 07:43:37 +02:00
: ExpressionAnalyzer ( e , val , t ) , partialReads ( std : : make_shared < PartialReadContainer > ( ) )
2021-10-21 14:44:48 +02:00
{ }
virtual bool submatch ( const Token * tok , bool exact = true ) const = 0 ;
2022-02-10 23:02:24 +01:00
virtual bool isAlias ( const Token * tok , bool & inconclusive ) const override
2021-10-21 14:44:48 +02:00
{
if ( tok - > exprId ( ) = = expr - > exprId ( ) & & tok - > astParent ( ) & & submatch ( tok - > astParent ( ) , false ) )
return false ;
return ExpressionAnalyzer : : isAlias ( tok , inconclusive ) ;
}
2022-02-10 23:02:24 +01:00
virtual bool match ( const Token * tok ) const override
2021-10-21 14:44:48 +02:00
{
return tok - > astOperand1 ( ) & & tok - > astOperand1 ( ) - > exprId ( ) = = expr - > exprId ( ) & & submatch ( tok ) ;
}
2022-02-10 23:02:24 +01:00
virtual bool internalMatch ( const Token * tok ) const override
2021-10-30 07:43:37 +02:00
{
return tok - > exprId ( ) = = expr - > exprId ( ) & & ! ( astIsLHS ( tok ) & & submatch ( tok - > astParent ( ) , false ) ) ;
}
2022-02-10 23:02:24 +01:00
virtual void internalUpdate ( Token * tok , const ValueFlow : : Value & v , Direction ) override
2021-10-30 07:43:37 +02:00
{
partialReads - > push_back ( std : : make_pair ( tok , v ) ) ;
}
2021-10-21 14:44:48 +02:00
// No reanalysis for subexression
2022-02-10 23:02:24 +01:00
virtual ValuePtr < Analyzer > reanalyze ( Token * , const std : : string & ) const override {
2021-10-21 14:44:48 +02:00
return { } ;
}
} ;
struct MemberExpressionAnalyzer : SubExpressionAnalyzer {
std : : string varname ;
MemberExpressionAnalyzer ( ) : SubExpressionAnalyzer ( ) , varname ( ) { }
MemberExpressionAnalyzer ( std : : string varname , const Token * e , const ValueFlow : : Value & val , const TokenList * t )
: SubExpressionAnalyzer ( e , val , t ) , varname ( std : : move ( varname ) )
{ }
2022-02-10 23:02:24 +01:00
virtual bool submatch ( const Token * tok , bool exact ) const override
2021-10-21 14:44:48 +02:00
{
if ( ! Token : : Match ( tok , " . %var% " ) )
return false ;
if ( ! exact )
return true ;
return tok - > next ( ) - > str ( ) = = varname ;
}
} ;
2021-06-19 13:59:48 +02:00
static Analyzer : : Result valueFlowForwardExpression ( Token * startToken ,
2021-08-07 20:51:18 +02:00
const Token * endToken ,
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
const TokenList * const tokenlist ,
const Settings * settings )
2020-02-16 16:02:22 +01:00
{
2021-07-18 07:46:31 +02:00
Analyzer : : Result result { } ;
2020-02-16 16:02:22 +01:00
for ( const ValueFlow : : Value & v : values ) {
2020-11-10 16:00:55 +01:00
ExpressionAnalyzer a ( exprTok , v , tokenlist ) ;
2021-07-18 07:46:31 +02:00
result . update ( valueFlowGenericForward ( startToken , endToken , a , settings ) ) ;
2020-02-16 16:02:22 +01:00
}
2021-07-18 07:46:31 +02:00
return result ;
2020-02-16 16:02:22 +01:00
}
2022-05-04 05:59:01 +02:00
static const Token * parseBinaryIntOp ( const Token * expr ,
const std : : function < std : : vector < MathLib : : bigint > ( const Token * ) > & eval ,
MathLib : : bigint & known )
2019-09-26 10:32:25 +02:00
{
if ( ! expr )
return nullptr ;
if ( ! expr - > astOperand1 ( ) | | ! expr - > astOperand2 ( ) )
return nullptr ;
2022-05-04 05:59:01 +02:00
if ( expr - > astOperand1 ( ) - > exprId ( ) = = 0 & & expr - > astOperand2 ( ) - > exprId ( ) = = 0 )
2021-08-03 06:31:52 +02:00
return nullptr ;
2022-05-04 05:59:01 +02:00
std : : vector < MathLib : : bigint > x1 = eval ( expr - > astOperand1 ( ) ) ;
std : : vector < MathLib : : bigint > x2 = eval ( expr - > astOperand2 ( ) ) ;
if ( expr - > astOperand1 ( ) - > exprId ( ) = = 0 & & x1 . empty ( ) )
return nullptr ;
if ( expr - > astOperand2 ( ) - > exprId ( ) = = 0 & & x2 . empty ( ) )
2021-08-03 06:31:52 +02:00
return nullptr ;
2019-09-26 10:32:25 +02:00
const Token * varTok = nullptr ;
2022-05-04 05:59:01 +02:00
if ( ! x1 . empty ( ) & & x2 . empty ( ) ) {
2019-09-26 10:32:25 +02:00
varTok = expr - > astOperand2 ( ) ;
2022-05-04 05:59:01 +02:00
known = x1 . front ( ) ;
} else if ( x1 . empty ( ) & & ! x2 . empty ( ) ) {
2019-09-26 10:32:25 +02:00
varTok = expr - > astOperand1 ( ) ;
2022-05-04 05:59:01 +02:00
known = x2 . front ( ) ;
2019-09-26 10:32:25 +02:00
}
return varTok ;
}
2022-05-04 05:59:01 +02:00
const Token * solveExprValue ( const Token * expr ,
const std : : function < std : : vector < MathLib : : bigint > ( const Token * ) > & eval ,
ValueFlow : : Value & value )
2019-09-26 10:32:25 +02:00
{
2021-08-09 20:41:10 +02:00
if ( ! value . isIntValue ( ) & & ! value . isIteratorValue ( ) & & ! value . isSymbolicValue ( ) )
return expr ;
if ( value . isSymbolicValue ( ) & & ! Token : : Match ( expr , " +|- " ) )
return expr ;
2019-09-26 10:32:25 +02:00
MathLib : : bigint intval ;
2022-05-04 05:59:01 +02:00
const Token * binaryTok = parseBinaryIntOp ( expr , eval , intval ) ;
2022-02-06 20:14:13 +01:00
bool rhs = astIsRHS ( binaryTok ) ;
2022-03-16 15:28:44 +01:00
// If its on the rhs, then -1 multiplication is needed, which is not possible with simple delta analysis used currently for symbolic values
2022-02-06 20:14:13 +01:00
if ( value . isSymbolicValue ( ) & & rhs & & Token : : simpleMatch ( expr , " - " ) )
return expr ;
2019-09-26 10:32:25 +02:00
if ( binaryTok & & expr - > str ( ) . size ( ) = = 1 ) {
switch ( expr - > str ( ) [ 0 ] ) {
case ' + ' : {
2021-08-09 20:41:10 +02:00
value . intvalue - = intval ;
2022-05-04 05:59:01 +02:00
return solveExprValue ( binaryTok , eval , value ) ;
2021-08-09 20:41:10 +02:00
}
case ' - ' : {
2022-02-06 20:14:13 +01:00
if ( rhs )
value . intvalue = intval - value . intvalue ;
else
value . intvalue + = intval ;
2022-05-04 05:59:01 +02:00
return solveExprValue ( binaryTok , eval , value ) ;
2019-09-26 10:32:25 +02:00
}
case ' * ' : {
2019-12-06 08:08:40 +01:00
if ( intval = = 0 )
break ;
2021-08-09 20:41:10 +02:00
value . intvalue / = intval ;
2022-05-04 05:59:01 +02:00
return solveExprValue ( binaryTok , eval , value ) ;
2019-09-26 10:32:25 +02:00
}
case ' ^ ' : {
2021-08-09 20:41:10 +02:00
value . intvalue ^ = intval ;
2022-05-04 05:59:01 +02:00
return solveExprValue ( binaryTok , eval , value ) ;
2019-09-26 10:32:25 +02:00
}
}
}
return expr ;
}
2022-05-04 05:59:01 +02:00
static const Token * solveExprValue ( const Token * expr , ValueFlow : : Value & value )
{
return solveExprValue (
expr ,
[ ] ( const Token * tok ) - > std : : vector < MathLib : : bigint > {
if ( tok - > hasKnownIntValue ( ) )
return { tok - > values ( ) . front ( ) . intvalue } ;
return { } ;
} ,
value ) ;
}
2021-08-09 20:41:10 +02:00
ValuePtr < Analyzer > makeAnalyzer ( const Token * exprTok , ValueFlow : : Value value , const TokenList * tokenlist )
2020-11-10 16:00:55 +01:00
{
2021-08-09 20:41:10 +02:00
const Token * expr = solveExprValue ( exprTok , value ) ;
return ExpressionAnalyzer ( expr , value , tokenlist ) ;
2020-11-10 16:00:55 +01:00
}
2021-06-19 13:59:48 +02:00
static Analyzer : : Result valueFlowForward ( Token * startToken ,
2021-08-07 20:51:18 +02:00
const Token * endToken ,
const Token * exprTok ,
2022-05-08 20:42:06 +02:00
const std : : list < ValueFlow : : Value > & values ,
2021-08-07 20:51:18 +02:00
TokenList * const tokenlist ,
const Settings * settings )
2019-09-26 10:32:25 +02:00
{
2021-08-09 20:41:10 +02:00
Analyzer : : Result result { } ;
for ( const ValueFlow : : Value & v : values ) {
result . update ( valueFlowGenericForward ( startToken , endToken , makeAnalyzer ( exprTok , v , tokenlist ) , settings ) ) ;
}
return result ;
2021-07-18 07:46:31 +02:00
}
2021-08-01 10:13:14 +02:00
static Analyzer : : Result valueFlowForward ( Token * top ,
2021-08-02 10:51:34 +02:00
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * const tokenlist ,
const Settings * settings )
2021-08-01 10:13:14 +02:00
{
2021-08-03 17:53:30 +02:00
Analyzer : : Result result { } ;
for ( const ValueFlow : : Value & v : values ) {
result . update ( valueFlowGenericForward ( top , makeAnalyzer ( exprTok , v , tokenlist ) , settings ) ) ;
}
return result ;
2021-08-01 10:13:14 +02:00
}
2021-07-18 07:46:31 +02:00
static void valueFlowReverse ( Token * tok ,
const Token * const endToken ,
const Token * const varToken ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
const Settings * settings )
{
for ( const ValueFlow : : Value & v : values ) {
ExpressionAnalyzer a ( varToken , v , tokenlist ) ;
valueFlowGenericReverse ( tok , endToken , a , settings ) ;
2019-09-26 10:32:25 +02:00
}
}
2020-11-10 16:00:55 +01:00
static void valueFlowReverse ( TokenList * tokenlist ,
Token * tok ,
const Token * const varToken ,
ValueFlow : : Value val ,
2022-05-08 20:42:06 +02:00
const ValueFlow : : Value & val2 ,
2020-11-18 20:27:51 +01:00
ErrorLogger * /*errorLogger*/ ,
2020-11-10 16:00:55 +01:00
const Settings * settings )
{
std : : list < ValueFlow : : Value > values = { val } ;
if ( val2 . varId ! = 0 )
values . push_back ( val2 ) ;
2021-07-18 07:46:31 +02:00
valueFlowReverse ( tok , nullptr , varToken , values , tokenlist , settings ) ;
2021-01-08 22:55:04 +01:00
}
2022-02-28 18:54:55 +01:00
enum class LifetimeCapture { Undefined , ByValue , ByReference } ;
2019-01-29 09:47:52 +01:00
std : : string lifetimeType ( const Token * tok , const ValueFlow : : Value * val )
{
std : : string result ;
if ( ! val )
return " object " ;
switch ( val - > lifetimeKind ) {
2019-07-16 10:51:07 +02:00
case ValueFlow : : Value : : LifetimeKind : : Lambda :
2019-01-29 09:47:52 +01:00
result = " lambda " ;
break ;
2019-07-16 10:51:07 +02:00
case ValueFlow : : Value : : LifetimeKind : : Iterator :
2019-01-29 09:47:52 +01:00
result = " iterator " ;
break ;
2019-07-16 10:51:07 +02:00
case ValueFlow : : Value : : LifetimeKind : : Object :
2020-08-07 04:08:30 +02:00
case ValueFlow : : Value : : LifetimeKind : : SubObject :
2019-07-16 10:51:07 +02:00
case ValueFlow : : Value : : LifetimeKind : : Address :
2019-01-29 09:47:52 +01:00
if ( astIsPointer ( tok ) )
result = " pointer " ;
else
result = " object " ;
break ;
}
return result ;
}
2019-07-18 10:56:44 +02:00
std : : string lifetimeMessage ( const Token * tok , const ValueFlow : : Value * val , ErrorPath & errorPath )
{
const Token * tokvalue = val ? val - > tokvalue : nullptr ;
const Variable * tokvar = tokvalue ? tokvalue - > variable ( ) : nullptr ;
const Token * vartok = tokvar ? tokvar - > nameToken ( ) : nullptr ;
2021-11-01 19:23:15 +01:00
const bool classVar = tokvar ? ( ! tokvar - > isLocal ( ) & & ! tokvar - > isArgument ( ) & & ! tokvar - > isGlobal ( ) ) : false ;
2019-07-18 10:56:44 +02:00
std : : string type = lifetimeType ( tok , val ) ;
std : : string msg = type ;
if ( vartok ) {
2021-11-01 19:23:15 +01:00
if ( ! classVar )
errorPath . emplace_back ( vartok , " Variable created here. " ) ;
2019-07-18 10:56:44 +02:00
const Variable * var = vartok - > variable ( ) ;
if ( var ) {
2022-03-21 16:05:38 +01:00
std : : string submessage ;
2019-07-18 10:56:44 +02:00
switch ( val - > lifetimeKind ) {
2020-08-07 04:08:30 +02:00
case ValueFlow : : Value : : LifetimeKind : : SubObject :
2019-07-18 10:56:44 +02:00
case ValueFlow : : Value : : LifetimeKind : : Object :
case ValueFlow : : Value : : LifetimeKind : : Address :
if ( type = = " pointer " )
2021-11-01 19:23:15 +01:00
submessage = " to local variable " ;
2019-07-18 10:56:44 +02:00
else
2021-11-01 19:23:15 +01:00
submessage = " that points to local variable " ;
2019-07-18 10:56:44 +02:00
break ;
case ValueFlow : : Value : : LifetimeKind : : Lambda :
2021-11-01 19:23:15 +01:00
submessage = " that captures local variable " ;
2019-07-18 10:56:44 +02:00
break ;
case ValueFlow : : Value : : LifetimeKind : : Iterator :
2021-11-01 19:23:15 +01:00
submessage = " to local container " ;
2019-07-18 10:56:44 +02:00
break ;
}
2021-11-01 19:23:15 +01:00
if ( classVar )
submessage . replace ( submessage . find ( " local " ) , 5 , " member " ) ;
msg + = submessage + " ' " + var - > name ( ) + " ' " ;
2019-07-18 10:56:44 +02:00
}
}
return msg ;
}
2022-01-18 14:48:02 +01:00
std : : vector < ValueFlow : : Value > getLifetimeObjValues ( const Token * tok , bool inconclusive , MathLib : : bigint path )
2019-05-31 11:16:04 +02:00
{
2021-01-05 16:56:38 +01:00
std : : vector < ValueFlow : : Value > result ;
2022-01-18 14:48:02 +01:00
auto pred = [ & ] ( const ValueFlow : : Value & v ) {
if ( ! v . isLocalLifetimeValue ( ) & & ! ( path ! = 0 & & v . isSubFunctionLifetimeValue ( ) ) )
2019-05-31 11:16:04 +02:00
return false ;
2020-08-26 21:05:17 +02:00
if ( ! inconclusive & & v . isInconclusive ( ) )
2019-09-11 19:25:09 +02:00
return false ;
2021-11-13 07:45:29 +01:00
if ( ! v . tokvalue )
2019-05-31 11:16:04 +02:00
return false ;
2022-01-18 14:48:02 +01:00
if ( path > = 0 & & v . path ! = 0 & & v . path ! = path )
return false ;
2019-05-31 11:16:04 +02:00
return true ;
} ;
2021-01-05 16:56:38 +01:00
std : : copy_if ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , std : : back_inserter ( result ) , pred ) ;
return result ;
}
ValueFlow : : Value getLifetimeObjValue ( const Token * tok , bool inconclusive )
{
2022-01-18 14:48:02 +01:00
std : : vector < ValueFlow : : Value > values = getLifetimeObjValues ( tok , inconclusive ) ;
2019-05-31 11:16:04 +02:00
// There should only be one lifetime
2021-01-05 16:56:38 +01:00
if ( values . size ( ) ! = 1 )
2019-12-31 08:09:04 +01:00
return ValueFlow : : Value { } ;
2021-01-05 16:56:38 +01:00
return values . front ( ) ;
2019-05-31 11:16:04 +02:00
}
2021-08-07 20:51:18 +02:00
template < class Predicate >
2021-04-30 17:47:08 +02:00
static std : : vector < LifetimeToken > getLifetimeTokens ( const Token * tok ,
2021-08-07 20:51:18 +02:00
bool escape ,
ValueFlow : : Value : : ErrorPath errorPath ,
Predicate pred ,
int depth = 20 )
2018-11-16 06:12:28 +01:00
{
2019-01-23 07:29:16 +01:00
if ( ! tok )
2019-09-11 19:25:09 +02:00
return std : : vector < LifetimeToken > { } ;
2022-02-08 09:03:51 +01:00
if ( Token : : simpleMatch ( tok , " ... " ) )
return std : : vector < LifetimeToken > { } ;
2018-11-16 06:12:28 +01:00
const Variable * var = tok - > variable ( ) ;
2021-04-30 17:47:08 +02:00
if ( pred ( tok ) )
return { { tok , std : : move ( errorPath ) } } ;
2019-01-31 10:34:41 +01:00
if ( depth < 0 )
2019-09-11 19:25:09 +02:00
return { { tok , std : : move ( errorPath ) } } ;
2019-01-26 11:03:57 +01:00
if ( var & & var - > declarationId ( ) = = tok - > varId ( ) ) {
if ( var - > isReference ( ) | | var - > isRValueReference ( ) ) {
if ( ! var - > declEndToken ( ) )
2019-09-11 19:25:09 +02:00
return { { tok , true , std : : move ( errorPath ) } } ;
2019-01-26 11:03:57 +01:00
if ( var - > isArgument ( ) ) {
errorPath . emplace_back ( var - > declEndToken ( ) , " Passed to reference. " ) ;
2019-09-11 19:25:09 +02:00
return { { tok , true , std : : move ( errorPath ) } } ;
2019-01-26 11:03:57 +01:00
} else if ( Token : : simpleMatch ( var - > declEndToken ( ) , " = " ) ) {
errorPath . emplace_back ( var - > declEndToken ( ) , " Assigned to reference. " ) ;
const Token * vartok = var - > declEndToken ( ) - > astOperand2 ( ) ;
2021-04-19 14:20:29 +02:00
const bool temporary = isTemporary ( true , vartok , nullptr , true ) ;
2020-10-06 09:16:54 +02:00
const bool nonlocal = var - > isStatic ( ) | | var - > isGlobal ( ) ;
2021-04-19 14:20:29 +02:00
if ( vartok = = tok | | ( nonlocal & & temporary ) | |
( ! escape & & ( var - > isConst ( ) | | var - > isRValueReference ( ) ) & & temporary ) )
2019-09-11 19:25:09 +02:00
return { { tok , true , std : : move ( errorPath ) } } ;
2019-01-26 11:03:57 +01:00
if ( vartok )
2021-04-30 17:47:08 +02:00
return getLifetimeTokens ( vartok , escape , std : : move ( errorPath ) , pred , depth - 1 ) ;
2020-05-31 10:10:10 +02:00
} else if ( Token : : simpleMatch ( var - > nameToken ( ) - > astParent ( ) , " : " ) & &
var - > nameToken ( ) - > astParent ( ) - > astParent ( ) & &
Token : : simpleMatch ( var - > nameToken ( ) - > astParent ( ) - > astParent ( ) - > previous ( ) , " for ( " ) ) {
errorPath . emplace_back ( var - > nameToken ( ) , " Assigned to reference. " ) ;
const Token * vartok = var - > nameToken ( ) ;
if ( vartok = = tok )
return { { tok , true , std : : move ( errorPath ) } } ;
const Token * contok = var - > nameToken ( ) - > astParent ( ) - > astOperand2 ( ) ;
2021-09-04 19:53:14 +02:00
if ( astIsContainer ( contok ) )
2021-04-30 17:47:08 +02:00
return getLifetimeTokens ( contok , escape , std : : move ( errorPath ) , pred , depth - 1 ) ;
2021-09-04 19:53:14 +02:00
else
return std : : vector < LifetimeToken > { } ;
2019-01-26 11:03:57 +01:00
} else {
2019-09-11 19:25:09 +02:00
return std : : vector < LifetimeToken > { } ;
2019-01-26 11:03:57 +01:00
}
}
} else if ( Token : : Match ( tok - > previous ( ) , " %name% ( " ) ) {
const Function * f = tok - > previous ( ) - > function ( ) ;
2019-09-02 06:58:09 +02:00
if ( f ) {
if ( ! Function : : returnsReference ( f ) )
2019-09-11 19:25:09 +02:00
return { { tok , std : : move ( errorPath ) } } ;
std : : vector < LifetimeToken > result ;
2020-05-31 10:10:10 +02:00
std : : vector < const Token * > returns = Function : : findReturns ( f ) ;
2019-09-11 19:25:09 +02:00
for ( const Token * returnTok : returns ) {
if ( returnTok = = tok )
continue ;
2021-04-30 17:47:08 +02:00
for ( LifetimeToken & lt : getLifetimeTokens ( returnTok , escape , errorPath , pred , depth - returns . size ( ) ) ) {
2019-09-11 19:25:09 +02:00
const Token * argvarTok = lt . token ;
const Variable * argvar = argvarTok - > variable ( ) ;
if ( ! argvar )
continue ;
if ( argvar - > isArgument ( ) & & ( argvar - > isReference ( ) | | argvar - > isRValueReference ( ) ) ) {
int n = getArgumentPos ( argvar , f ) ;
if ( n < 0 )
return std : : vector < LifetimeToken > { } ;
2019-12-29 08:23:58 +01:00
std : : vector < const Token * > args = getArguments ( tok - > previous ( ) ) ;
// TODO: Track lifetimes of default parameters
if ( n > = args . size ( ) )
return std : : vector < LifetimeToken > { } ;
const Token * argTok = args [ n ] ;
2019-09-11 19:25:09 +02:00
lt . errorPath . emplace_back ( returnTok , " Return reference. " ) ;
2020-09-09 01:30:45 +02:00
lt . errorPath . emplace_back ( tok - > previous ( ) , " Called function passing ' " + argTok - > expressionString ( ) + " '. " ) ;
2019-09-11 19:25:09 +02:00
std : : vector < LifetimeToken > arglts = LifetimeToken : : setInconclusive (
2021-08-07 20:51:18 +02:00
getLifetimeTokens ( argTok , escape , std : : move ( lt . errorPath ) , pred , depth - returns . size ( ) ) ,
returns . size ( ) > 1 ) ;
2019-09-11 19:25:09 +02:00
result . insert ( result . end ( ) , arglts . begin ( ) , arglts . end ( ) ) ;
}
}
2019-09-02 06:58:09 +02:00
}
2019-09-11 19:25:09 +02:00
return result ;
2020-09-02 20:01:08 +02:00
} else if ( Token : : Match ( tok - > tokAt ( - 2 ) , " . %name% ( " ) & & tok - > tokAt ( - 2 ) - > originalName ( ) ! = " -> " & & astIsContainer ( tok - > tokAt ( - 2 ) - > astOperand1 ( ) ) ) {
2019-09-02 06:58:09 +02:00
const Library : : Container * library = getLibraryContainer ( tok - > tokAt ( - 2 ) - > astOperand1 ( ) ) ;
Library : : Container : : Yield y = library - > getYield ( tok - > previous ( ) - > str ( ) ) ;
if ( y = = Library : : Container : : Yield : : AT_INDEX | | y = = Library : : Container : : Yield : : ITEM ) {
errorPath . emplace_back ( tok - > previous ( ) , " Accessing container. " ) ;
2019-09-11 19:25:09 +02:00
return LifetimeToken : : setAddressOf (
2021-08-07 20:51:18 +02:00
getLifetimeTokens ( tok - > tokAt ( - 2 ) - > astOperand1 ( ) , escape , std : : move ( errorPath ) , pred , depth - 1 ) ,
false ) ;
2019-09-02 06:58:09 +02:00
}
2019-02-22 06:38:56 +01:00
}
2021-07-20 21:30:27 +02:00
} else if ( Token : : Match ( tok , " .|::|[ " ) | | tok - > isUnaryOp ( " * " ) ) {
2019-02-22 06:38:56 +01:00
const Token * vartok = tok ;
2021-07-20 21:30:27 +02:00
if ( tok - > isUnaryOp ( " * " ) )
vartok = tok - > astOperand1 ( ) ;
2019-02-22 06:38:56 +01:00
while ( vartok ) {
if ( vartok - > str ( ) = = " [ " | | vartok - > originalName ( ) = = " -> " )
vartok = vartok - > astOperand1 ( ) ;
else if ( vartok - > str ( ) = = " . " | | vartok - > str ( ) = = " :: " )
vartok = vartok - > astOperand2 ( ) ;
else
break ;
}
if ( ! vartok )
2019-09-11 19:25:09 +02:00
return { { tok , std : : move ( errorPath ) } } ;
2019-02-22 06:38:56 +01:00
const Variable * tokvar = vartok - > variable ( ) ;
2021-08-04 08:16:31 +02:00
const bool isContainer = astIsContainer ( vartok ) & & ! astIsPointer ( vartok ) ;
2021-11-11 08:00:05 +01:00
if ( ! astIsUniqueSmartPointer ( vartok ) & & ! isContainer & & ! ( tokvar & & tokvar - > isArray ( ) & & ! tokvar - > isArgument ( ) ) & &
2019-02-23 08:32:08 +01:00
( Token : : Match ( vartok - > astParent ( ) , " [|* " ) | | vartok - > astParent ( ) - > originalName ( ) = = " -> " ) ) {
2019-02-22 06:38:56 +01:00
for ( const ValueFlow : : Value & v : vartok - > values ( ) ) {
if ( ! v . isLocalLifetimeValue ( ) )
continue ;
2020-09-08 18:33:29 +02:00
if ( v . tokvalue = = tok )
continue ;
2019-02-22 06:38:56 +01:00
errorPath . insert ( errorPath . end ( ) , v . errorPath . begin ( ) , v . errorPath . end ( ) ) ;
2021-04-30 17:47:08 +02:00
return getLifetimeTokens ( v . tokvalue , escape , std : : move ( errorPath ) , pred , depth - 1 ) ;
2019-02-22 06:38:56 +01:00
}
} else {
2021-04-30 17:47:08 +02:00
return LifetimeToken : : setAddressOf ( getLifetimeTokens ( vartok , escape , std : : move ( errorPath ) , pred , depth - 1 ) ,
2019-09-11 19:25:09 +02:00
! ( astIsContainer ( vartok ) & & Token : : simpleMatch ( vartok - > astParent ( ) , " [ " ) ) ) ;
2019-01-26 11:03:57 +01:00
}
2022-05-13 06:51:07 +02:00
} else if ( Token : : simpleMatch ( tok , " { " ) & & getArgumentStart ( tok ) & &
! Token : : simpleMatch ( getArgumentStart ( tok ) , " , " ) & & getArgumentStart ( tok ) - > valueType ( ) ) {
const Token * vartok = getArgumentStart ( tok ) ;
auto vts = getParentValueTypes ( tok ) ;
for ( const ValueType & vt : vts ) {
if ( vt . isTypeEqual ( vartok - > valueType ( ) ) )
return getLifetimeTokens ( vartok , escape , std : : move ( errorPath ) , pred , depth - 1 ) ;
}
2018-11-16 06:12:28 +01:00
}
2019-09-11 19:25:09 +02:00
return { { tok , std : : move ( errorPath ) } } ;
}
2021-04-30 17:47:08 +02:00
std : : vector < LifetimeToken > getLifetimeTokens ( const Token * tok , bool escape , ValueFlow : : Value : : ErrorPath errorPath )
{
2021-05-01 07:35:03 +02:00
return getLifetimeTokens ( tok , escape , std : : move ( errorPath ) , [ ] ( const Token * ) {
return false ;
} ) ;
2021-04-30 17:47:08 +02:00
}
bool hasLifetimeToken ( const Token * tok , const Token * lifetime )
{
bool result = false ;
getLifetimeTokens ( tok , false , ValueFlow : : Value : : ErrorPath { } , [ & ] ( const Token * tok2 ) {
result = tok2 - > exprId ( ) = = lifetime - > exprId ( ) ;
return result ;
} ) ;
return result ;
}
2019-09-11 19:25:09 +02:00
static const Token * getLifetimeToken ( const Token * tok , ValueFlow : : Value : : ErrorPath & errorPath , bool * addressOf = nullptr )
{
std : : vector < LifetimeToken > lts = getLifetimeTokens ( tok ) ;
if ( lts . size ( ) ! = 1 )
return nullptr ;
if ( lts . front ( ) . inconclusive )
return nullptr ;
if ( addressOf )
* addressOf = lts . front ( ) . addressOf ;
errorPath . insert ( errorPath . end ( ) , lts . front ( ) . errorPath . begin ( ) , lts . front ( ) . errorPath . end ( ) ) ;
return lts . front ( ) . token ;
2019-02-22 06:38:56 +01:00
}
2019-09-02 06:58:09 +02:00
const Variable * getLifetimeVariable ( const Token * tok , ValueFlow : : Value : : ErrorPath & errorPath , bool * addressOf )
2019-02-22 06:38:56 +01:00
{
2019-09-02 06:58:09 +02:00
const Token * tok2 = getLifetimeToken ( tok , errorPath , addressOf ) ;
2019-02-22 06:38:56 +01:00
if ( tok2 & & tok2 - > variable ( ) )
return tok2 - > variable ( ) ;
return nullptr ;
2018-11-16 06:12:28 +01:00
}
2020-05-31 10:10:10 +02:00
const Variable * getLifetimeVariable ( const Token * tok )
{
ValueFlow : : Value : : ErrorPath errorPath ;
return getLifetimeVariable ( tok , errorPath , nullptr ) ;
}
2018-11-14 06:59:25 +01:00
static bool isNotLifetimeValue ( const ValueFlow : : Value & val )
{
return ! val . isLifetimeValue ( ) ;
}
2021-10-05 08:28:19 +02:00
static bool isLifetimeOwned ( const ValueType * vtParent )
{
if ( vtParent - > container )
return ! vtParent - > container - > view ;
return vtParent - > type = = ValueType : : CONTAINER ;
}
2019-05-05 11:40:59 +02:00
static bool isLifetimeOwned ( const ValueType * vt , const ValueType * vtParent )
{
if ( ! vtParent )
return false ;
2022-02-28 18:54:55 +01:00
if ( isLifetimeOwned ( vtParent ) )
return true ;
if ( ! vt )
2019-05-05 11:40:59 +02:00
return false ;
2021-10-05 08:28:19 +02:00
// If converted from iterator to pointer then the iterator is most likely a pointer
if ( vtParent - > pointer = = 1 & & vt - > pointer = = 0 & & vt - > type = = ValueType : : ITERATOR )
return false ;
2019-05-05 11:40:59 +02:00
if ( vt - > type ! = ValueType : : UNKNOWN_TYPE & & vtParent - > type ! = ValueType : : UNKNOWN_TYPE ) {
if ( vt - > pointer ! = vtParent - > pointer )
return true ;
if ( vt - > type ! = vtParent - > type ) {
if ( vtParent - > type = = ValueType : : RECORD )
return true ;
2021-10-05 08:28:19 +02:00
if ( isLifetimeOwned ( vtParent ) )
2019-05-05 11:40:59 +02:00
return true ;
}
}
return false ;
}
static bool isLifetimeBorrowed ( const ValueType * vt , const ValueType * vtParent )
{
if ( ! vtParent )
return false ;
if ( ! vt )
return false ;
2021-06-04 17:15:39 +02:00
if ( vt - > pointer > 0 & & vt - > pointer = = vtParent - > pointer )
return true ;
2021-10-05 08:28:19 +02:00
if ( vtParent - > container & & vtParent - > container - > view )
return true ;
2020-08-06 06:17:35 +02:00
if ( vt - > type ! = ValueType : : UNKNOWN_TYPE & & vtParent - > type ! = ValueType : : UNKNOWN_TYPE & & vtParent - > container = = vt - > container ) {
2019-05-05 11:40:59 +02:00
if ( vtParent - > pointer > vt - > pointer )
return true ;
if ( vtParent - > pointer < vt - > pointer & & vtParent - > isIntegral ( ) )
return true ;
2020-05-23 23:12:00 +02:00
if ( vtParent - > str ( ) = = vt - > str ( ) )
return true ;
2019-05-05 11:40:59 +02:00
}
return false ;
}
2020-05-23 23:12:00 +02:00
static const Token * skipCVRefs ( const Token * tok , const Token * endTok )
{
2020-05-24 10:52:58 +02:00
while ( tok ! = endTok & & Token : : Match ( tok , " const|volatile|auto|&|&& " ) )
2020-05-23 23:12:00 +02:00
tok = tok - > next ( ) ;
return tok ;
}
static bool isNotEqual ( std : : pair < const Token * , const Token * > x , std : : pair < const Token * , const Token * > y )
{
const Token * start1 = x . first ;
const Token * start2 = y . first ;
if ( start1 = = nullptr | | start2 = = nullptr )
return false ;
2020-05-24 10:52:58 +02:00
while ( start1 ! = x . second & & start2 ! = y . second ) {
2020-05-23 23:12:00 +02:00
const Token * tok1 = skipCVRefs ( start1 , x . second ) ;
if ( tok1 ! = start1 ) {
start1 = tok1 ;
continue ;
}
const Token * tok2 = skipCVRefs ( start2 , y . second ) ;
if ( tok2 ! = start2 ) {
start2 = tok2 ;
continue ;
}
if ( start1 - > str ( ) ! = start2 - > str ( ) )
return true ;
start1 = start1 - > next ( ) ;
start2 = start2 - > next ( ) ;
}
start1 = skipCVRefs ( start1 , x . second ) ;
start2 = skipCVRefs ( start2 , y . second ) ;
return ! ( start1 = = x . second & & start2 = = y . second ) ;
}
2020-09-02 07:13:15 +02:00
static bool isNotEqual ( std : : pair < const Token * , const Token * > x , const std : : string & y )
{
TokenList tokenList ( nullptr ) ;
std : : istringstream istr ( y ) ;
tokenList . createTokens ( istr ) ;
return isNotEqual ( x , std : : make_pair ( tokenList . front ( ) , tokenList . back ( ) ) ) ;
}
static bool isNotEqual ( std : : pair < const Token * , const Token * > x , const ValueType * y )
{
if ( y = = nullptr )
return false ;
if ( y - > originalTypeName . empty ( ) )
return false ;
return isNotEqual ( x , y - > originalTypeName ) ;
}
2020-05-23 23:12:00 +02:00
2021-08-04 21:07:31 +02:00
static bool isDifferentType ( const Token * src , const Token * dst )
{
const Type * t = Token : : typeOf ( src ) ;
const Type * parentT = Token : : typeOf ( dst ) ;
if ( t & & parentT ) {
if ( t - > classDef & & parentT - > classDef & & t - > classDef ! = parentT - > classDef )
return true ;
} else {
std : : pair < const Token * , const Token * > decl = Token : : typeDecl ( src ) ;
std : : pair < const Token * , const Token * > parentdecl = Token : : typeDecl ( dst ) ;
if ( isNotEqual ( decl , parentdecl ) )
return true ;
if ( isNotEqual ( decl , dst - > valueType ( ) ) )
return true ;
if ( isNotEqual ( parentdecl , src - > valueType ( ) ) )
return true ;
}
return false ;
}
2019-05-05 11:40:59 +02:00
bool isLifetimeBorrowed ( const Token * tok , const Settings * settings )
{
if ( ! tok )
return true ;
2020-05-19 21:03:28 +02:00
if ( tok - > str ( ) = = " , " )
2019-05-05 11:40:59 +02:00
return true ;
if ( ! tok - > astParent ( ) )
return true ;
2022-03-16 17:11:49 +01:00
const Token * parent = nullptr ;
const ValueType * vt = tok - > valueType ( ) ;
std : : vector < ValueType > vtParents = getParentValueTypes ( tok , settings , & parent ) ;
2022-04-12 08:09:13 +02:00
for ( const ValueType & vtParent : vtParents ) {
if ( isLifetimeBorrowed ( vt , & vtParent ) )
return true ;
if ( isLifetimeOwned ( vt , & vtParent ) )
return false ;
2022-03-16 17:11:49 +01:00
}
if ( parent ) {
if ( isDifferentType ( tok , parent ) )
2019-05-05 11:40:59 +02:00
return false ;
}
return true ;
}
2018-11-16 06:12:28 +01:00
static void valueFlowLifetimeFunction ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) ;
2019-03-19 06:25:10 +01:00
static void valueFlowLifetimeConstructor ( Token * tok ,
2021-08-07 20:51:18 +02:00
TokenList * tokenlist ,
ErrorLogger * errorLogger ,
const Settings * settings ) ;
2019-03-19 06:25:10 +01:00
2022-02-02 13:01:44 +01:00
static const Token * getEndOfVarScope ( const Variable * var )
{
if ( ! var )
return nullptr ;
const Scope * innerScope = var - > scope ( ) ;
const Scope * outerScope = innerScope ;
if ( var - > typeStartToken ( ) & & var - > typeStartToken ( ) - > scope ( ) )
outerScope = var - > typeStartToken ( ) - > scope ( ) ;
if ( ! innerScope & & outerScope )
innerScope = outerScope ;
if ( ! innerScope | | ! outerScope )
return nullptr ;
if ( ! innerScope - > isExecutable ( ) )
return nullptr ;
// If the variable is defined in a for/while initializer then we want to
// pick one token after the end so forward analysis can analyze the exit
// conditions
if ( innerScope ! = outerScope & & outerScope - > isExecutable ( ) & & innerScope - > isLocal ( ) )
return innerScope - > bodyEnd - > next ( ) ;
return innerScope - > bodyEnd ;
}
2022-02-22 09:51:44 +01:00
const Token * getEndOfExprScope ( const Token * tok , const Scope * defaultScope , bool smallest )
2022-02-02 13:01:44 +01:00
{
const Token * end = nullptr ;
bool local = false ;
visitAstNodes ( tok , [ & ] ( const Token * child ) {
if ( const Variable * var = child - > variable ( ) ) {
local | = var - > isLocal ( ) ;
if ( var - > isLocal ( ) | | var - > isArgument ( ) ) {
const Token * varEnd = getEndOfVarScope ( var ) ;
2022-02-22 09:51:44 +01:00
if ( ! end | | ( smallest ? precedes ( varEnd , end ) : succeeds ( varEnd , end ) ) )
2022-02-02 13:01:44 +01:00
end = varEnd ;
}
}
return ChildrenToVisit : : op1_and_op2 ;
} ) ;
if ( ! end & & defaultScope )
end = defaultScope - > bodyEnd ;
if ( ! end ) {
const Scope * scope = tok - > scope ( ) ;
if ( scope )
end = scope - > bodyEnd ;
// If there is no local variables then pick the function scope
if ( ! local ) {
while ( scope & & scope - > isLocal ( ) )
scope = scope - > nestedIn ;
if ( scope & & scope - > isExecutable ( ) )
end = scope - > bodyEnd ;
}
}
return end ;
}
2018-11-14 06:59:25 +01:00
static void valueFlowForwardLifetime ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
2020-09-20 14:27:09 +02:00
// Forward lifetimes to constructed variable
2022-02-28 18:54:55 +01:00
if ( Token : : Match ( tok - > previous ( ) , " %var% {|( " ) & & isVariableDecl ( tok - > previous ( ) ) ) {
2020-09-20 14:27:09 +02:00
std : : list < ValueFlow : : Value > values = tok - > values ( ) ;
values . remove_if ( & isNotLifetimeValue ) ;
2022-02-24 06:50:34 +01:00
valueFlowForward ( nextAfterAstRightmostLeaf ( tok ) , getEndOfExprScope ( tok ) , tok - > previous ( ) , values , tokenlist , settings ) ;
2020-09-20 14:27:09 +02:00
return ;
}
2019-06-16 10:09:38 +02:00
Token * parent = tok - > astParent ( ) ;
2021-08-31 09:48:23 +02:00
while ( parent & & parent - > str ( ) = = " , " )
2018-11-16 06:12:28 +01:00
parent = parent - > astParent ( ) ;
if ( ! parent )
2018-11-14 06:59:25 +01:00
return ;
2018-11-16 06:12:28 +01:00
// Assignment
2019-02-22 06:37:02 +01:00
if ( parent - > str ( ) = = " = " & & ( ! parent - > astParent ( ) | | Token : : simpleMatch ( parent - > astParent ( ) , " ; " ) ) ) {
2018-11-16 06:12:28 +01:00
// Rhs values..
if ( ! parent - > astOperand2 ( ) | | parent - > astOperand2 ( ) - > values ( ) . empty ( ) )
return ;
2018-11-14 06:59:25 +01:00
2019-05-05 11:40:59 +02:00
if ( ! isLifetimeBorrowed ( parent - > astOperand2 ( ) , settings ) )
2018-11-16 06:12:28 +01:00
return ;
2018-11-14 06:59:25 +01:00
2022-02-24 06:50:34 +01:00
const Token * expr = getLHSVariableToken ( parent ) ;
2022-03-03 17:08:23 +01:00
if ( ! expr )
return ;
2018-11-14 06:59:25 +01:00
2022-05-05 06:55:02 +02:00
if ( expr - > exprId ( ) = = 0 )
return ;
2022-02-24 06:50:34 +01:00
const Token * endOfVarScope = getEndOfExprScope ( expr ) ;
2020-01-04 11:39:52 +01:00
// Only forward lifetime values
std : : list < ValueFlow : : Value > values = parent - > astOperand2 ( ) - > values ( ) ;
values . remove_if ( & isNotLifetimeValue ) ;
2022-05-05 06:55:02 +02:00
// Dont forward lifetimes that overlap
values . remove_if ( [ & ] ( const ValueFlow : : Value & value ) {
return findAstNode ( value . tokvalue , [ & ] ( const Token * child ) {
return child - > exprId ( ) = = expr - > exprId ( ) ;
} ) ;
} ) ;
2018-11-14 06:59:25 +01:00
2018-11-16 06:12:28 +01:00
// Skip RHS
const Token * nextExpression = nextAfterAstRightmostLeaf ( parent ) ;
2022-02-24 06:50:34 +01:00
if ( expr - > exprId ( ) > 0 ) {
valueFlowForwardExpression ( const_cast < Token * > ( nextExpression ) ,
endOfVarScope - > next ( ) ,
expr ,
values ,
tokenlist ,
settings ) ;
2020-01-04 11:39:52 +01:00
for ( ValueFlow : : Value & val : values ) {
if ( val . lifetimeKind = = ValueFlow : : Value : : LifetimeKind : : Address )
2020-08-07 04:08:30 +02:00
val . lifetimeKind = ValueFlow : : Value : : LifetimeKind : : SubObject ;
2020-01-04 11:39:52 +01:00
}
2022-02-24 06:50:34 +01:00
// TODO: handle `[`
if ( Token : : simpleMatch ( parent - > astOperand1 ( ) , " . " ) ) {
const Token * parentLifetime =
getParentLifetime ( tokenlist - > isCPP ( ) , parent - > astOperand1 ( ) - > astOperand2 ( ) , & settings - > library ) ;
if ( parentLifetime & & parentLifetime - > exprId ( ) > 0 ) {
valueFlowForward ( const_cast < Token * > ( nextExpression ) ,
endOfVarScope ,
parentLifetime ,
values ,
tokenlist ,
settings ) ;
}
2020-01-04 11:39:52 +01:00
}
2019-02-22 06:37:02 +01:00
}
2019-03-19 06:25:10 +01:00
// Constructor
2019-09-10 19:41:35 +02:00
} else if ( Token : : simpleMatch ( parent , " { " ) & & ! isScopeBracket ( parent ) ) {
2019-06-16 10:09:38 +02:00
valueFlowLifetimeConstructor ( parent , tokenlist , errorLogger , settings ) ;
2019-09-10 19:41:35 +02:00
valueFlowForwardLifetime ( parent , tokenlist , errorLogger , settings ) ;
2018-11-16 06:12:28 +01:00
// Function call
} else if ( Token : : Match ( parent - > previous ( ) , " %name% ( " ) ) {
2019-06-16 10:09:38 +02:00
valueFlowLifetimeFunction ( parent - > previous ( ) , tokenlist , errorLogger , settings ) ;
2019-09-10 19:41:35 +02:00
valueFlowForwardLifetime ( parent , tokenlist , errorLogger , settings ) ;
2018-11-21 08:43:57 +01:00
// Variable
} else if ( tok - > variable ( ) ) {
const Variable * var = tok - > variable ( ) ;
2020-01-11 09:17:32 +01:00
const Token * endOfVarScope = var - > scope ( ) - > bodyEnd ;
2018-11-21 08:43:57 +01:00
std : : list < ValueFlow : : Value > values = tok - > values ( ) ;
const Token * nextExpression = nextAfterAstRightmostLeaf ( parent ) ;
// Only forward lifetime values
values . remove_if ( & isNotLifetimeValue ) ;
2021-07-18 07:46:31 +02:00
valueFlowForward ( const_cast < Token * > ( nextExpression ) , endOfVarScope , tok , values , tokenlist , settings ) ;
2020-12-19 08:29:37 +01:00
// Cast
} else if ( parent - > isCast ( ) ) {
std : : list < ValueFlow : : Value > values = tok - > values ( ) ;
// Only forward lifetime values
values . remove_if ( & isNotLifetimeValue ) ;
2020-12-19 08:56:46 +01:00
for ( const ValueFlow : : Value & value : values )
2020-12-19 08:29:37 +01:00
setTokenValue ( parent , value , tokenlist - > getSettings ( ) ) ;
valueFlowForwardLifetime ( parent , tokenlist , errorLogger , settings ) ;
2018-11-16 06:12:28 +01:00
}
}
2018-11-14 06:59:25 +01:00
2018-11-16 06:12:28 +01:00
struct LifetimeStore {
const Token * argtok ;
std : : string message ;
ValueFlow : : Value : : LifetimeKind type ;
2019-01-29 09:47:52 +01:00
ErrorPath errorPath ;
2019-09-11 19:25:09 +02:00
bool inconclusive ;
2020-10-03 11:01:53 +02:00
bool forward ;
struct Context {
Token * tok ;
TokenList * tokenlist ;
ErrorLogger * errorLogger ;
const Settings * settings ;
} ;
2019-01-29 09:47:52 +01:00
2019-07-26 07:02:07 +02:00
LifetimeStore ( )
2020-10-03 11:01:53 +02:00
: argtok ( nullptr ) , message ( ) , type ( ) , errorPath ( ) , inconclusive ( false ) , forward ( true ) , mContext ( nullptr )
2019-07-26 07:02:07 +02:00
{ }
2020-10-03 11:01:53 +02:00
LifetimeStore ( const Token * argtok ,
const std : : string & message ,
2019-09-11 19:25:09 +02:00
ValueFlow : : Value : : LifetimeKind type = ValueFlow : : Value : : LifetimeKind : : Object ,
bool inconclusive = false )
2020-10-03 11:01:53 +02:00
: argtok ( argtok ) ,
2021-08-07 20:51:18 +02:00
message ( message ) ,
type ( type ) ,
errorPath ( ) ,
inconclusive ( inconclusive ) ,
forward ( true ) ,
mContext ( nullptr )
2019-01-29 09:47:52 +01:00
{ }
2018-11-14 06:59:25 +01:00
2021-08-07 20:51:18 +02:00
template < class F >
2020-10-03 11:01:53 +02:00
static void forEach ( const std : : vector < const Token * > & argtoks ,
const std : : string & message ,
ValueFlow : : Value : : LifetimeKind type ,
2020-10-03 11:02:11 +02:00
F f ) {
2020-10-03 11:01:53 +02:00
std : : map < const Token * , Context > forwardToks ;
for ( const Token * arg : argtoks ) {
LifetimeStore ls { arg , message , type } ;
Context c { } ;
ls . mContext = & c ;
ls . forward = false ;
f ( ls ) ;
if ( c . tok )
forwardToks [ c . tok ] = c ;
}
for ( const auto & p : forwardToks ) {
const Context & c = p . second ;
valueFlowForwardLifetime ( c . tok , c . tokenlist , c . errorLogger , c . settings ) ;
}
}
2019-07-26 07:02:07 +02:00
static LifetimeStore fromFunctionArg ( const Function * f , Token * tok , const Variable * var , TokenList * tokenlist , ErrorLogger * errorLogger ) {
if ( ! var )
return LifetimeStore { } ;
if ( ! var - > isArgument ( ) )
return LifetimeStore { } ;
int n = getArgumentPos ( var , f ) ;
if ( n < 0 )
return LifetimeStore { } ;
std : : vector < const Token * > args = getArguments ( tok ) ;
if ( n > = args . size ( ) ) {
if ( tokenlist - > getSettings ( ) - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok ,
" Argument mismatch: Function ' " + tok - > str ( ) + " ' returning lifetime from argument index " +
std : : to_string ( n ) + " but only " + std : : to_string ( args . size ( ) ) +
" arguments are available. " ) ;
return LifetimeStore { } ;
}
const Token * argtok2 = args [ n ] ;
2020-09-09 01:30:45 +02:00
return LifetimeStore { argtok2 , " Passed to ' " + tok - > expressionString ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } ;
2019-07-26 07:02:07 +02:00
}
2021-08-07 20:51:18 +02:00
template < class Predicate >
2020-10-03 11:02:11 +02:00
bool byRef ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings , Predicate pred ) const {
2019-09-11 19:25:09 +02:00
if ( ! argtok )
2020-10-03 11:01:53 +02:00
return false ;
bool update = false ;
2019-09-11 19:25:09 +02:00
for ( const LifetimeToken & lt : getLifetimeTokens ( argtok ) ) {
2021-02-24 22:00:06 +01:00
if ( ! settings - > certainty . isEnabled ( Certainty : : inconclusive ) & & lt . inconclusive )
2019-09-11 19:25:09 +02:00
continue ;
ErrorPath er = errorPath ;
er . insert ( er . end ( ) , lt . errorPath . begin ( ) , lt . errorPath . end ( ) ) ;
if ( ! lt . token )
2020-10-03 11:01:53 +02:00
return false ;
2019-09-11 19:25:09 +02:00
if ( ! pred ( lt . token ) )
2020-10-03 11:01:53 +02:00
return false ;
2019-09-11 19:25:09 +02:00
er . emplace_back ( argtok , message ) ;
2018-11-14 06:59:25 +01:00
2019-09-11 19:25:09 +02:00
ValueFlow : : Value value ;
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
2019-09-11 19:25:09 +02:00
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Local ;
value . tokvalue = lt . token ;
value . errorPath = std : : move ( er ) ;
value . lifetimeKind = type ;
value . setInconclusive ( lt . inconclusive | | inconclusive ) ;
// Don't add the value a second time
if ( std : : find ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , value ) ! = tok - > values ( ) . end ( ) )
2020-10-03 11:01:53 +02:00
return false ;
2019-09-11 19:25:09 +02:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2020-10-03 11:01:53 +02:00
update = true ;
2019-09-11 19:25:09 +02:00
}
2020-10-03 11:01:53 +02:00
if ( update & & forward )
forwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
return update ;
2018-11-16 06:12:28 +01:00
}
2020-10-03 11:02:11 +02:00
bool byRef ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) const {
return byRef ( tok , tokenlist , errorLogger , settings , [ ] ( const Token * ) {
return true ;
} ) ;
2018-11-16 06:12:28 +01:00
}
2021-08-07 20:51:18 +02:00
template < class Predicate >
2020-10-03 11:02:11 +02:00
bool byVal ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings , Predicate pred ) const {
2019-07-26 07:02:07 +02:00
if ( ! argtok )
2020-10-03 11:01:53 +02:00
return false ;
bool update = false ;
2019-01-29 09:47:52 +01:00
if ( argtok - > values ( ) . empty ( ) ) {
ErrorPath er ;
er . emplace_back ( argtok , message ) ;
2021-12-04 17:00:55 +01:00
for ( const LifetimeToken & lt : getLifetimeTokens ( argtok ) ) {
if ( ! settings - > certainty . isEnabled ( Certainty : : inconclusive ) & & lt . inconclusive )
continue ;
2019-01-29 09:47:52 +01:00
ValueFlow : : Value value ;
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
2021-12-04 17:00:55 +01:00
value . tokvalue = lt . token ;
2022-04-30 09:36:28 +02:00
value . capturetok = argtok ;
2019-01-29 09:47:52 +01:00
value . errorPath = er ;
value . lifetimeKind = type ;
2021-12-04 17:00:55 +01:00
value . setInconclusive ( inconclusive | | lt . inconclusive ) ;
const Variable * var = lt . token - > variable ( ) ;
if ( var & & var - > isArgument ( ) ) {
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Argument ;
} else {
continue ;
}
2019-01-29 09:47:52 +01:00
// Don't add the value a second time
if ( std : : find ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , value ) ! = tok - > values ( ) . end ( ) )
2021-12-04 17:00:55 +01:00
continue ;
2022-03-13 20:07:58 +01:00
2019-01-29 09:47:52 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2020-10-03 11:01:53 +02:00
update = true ;
2019-01-29 09:47:52 +01:00
}
}
2018-11-16 06:12:28 +01:00
for ( const ValueFlow : : Value & v : argtok - > values ( ) ) {
2018-11-14 06:59:25 +01:00
if ( ! v . isLifetimeValue ( ) )
continue ;
2018-11-16 06:12:28 +01:00
const Token * tok3 = v . tokvalue ;
2019-09-11 19:25:09 +02:00
for ( const LifetimeToken & lt : getLifetimeTokens ( tok3 ) ) {
2021-02-24 22:00:06 +01:00
if ( ! settings - > certainty . isEnabled ( Certainty : : inconclusive ) & & lt . inconclusive )
2019-09-11 19:25:09 +02:00
continue ;
ErrorPath er = v . errorPath ;
er . insert ( er . end ( ) , lt . errorPath . begin ( ) , lt . errorPath . end ( ) ) ;
if ( ! lt . token )
2020-10-03 11:01:53 +02:00
return false ;
2019-09-11 19:25:09 +02:00
if ( ! pred ( lt . token ) )
2020-10-03 11:01:53 +02:00
return false ;
2019-09-11 19:25:09 +02:00
er . emplace_back ( argtok , message ) ;
er . insert ( er . end ( ) , errorPath . begin ( ) , errorPath . end ( ) ) ;
2018-11-16 06:12:28 +01:00
2019-09-11 19:25:09 +02:00
ValueFlow : : Value value ;
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
2019-09-11 19:25:09 +02:00
value . lifetimeScope = v . lifetimeScope ;
2020-09-17 07:23:38 +02:00
value . path = v . path ;
2019-09-11 19:25:09 +02:00
value . tokvalue = lt . token ;
2022-04-30 09:36:28 +02:00
value . capturetok = argtok ;
2019-09-11 19:25:09 +02:00
value . errorPath = std : : move ( er ) ;
value . lifetimeKind = type ;
value . setInconclusive ( lt . inconclusive | | v . isInconclusive ( ) | | inconclusive ) ;
// Don't add the value a second time
if ( std : : find ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , value ) ! = tok - > values ( ) . end ( ) )
continue ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2020-10-03 11:01:53 +02:00
update = true ;
2019-09-11 19:25:09 +02:00
}
2018-11-16 06:12:28 +01:00
}
2020-10-03 11:01:53 +02:00
if ( update & & forward )
forwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
return update ;
2018-11-16 06:12:28 +01:00
}
2020-10-03 11:02:11 +02:00
bool byVal ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) const {
return byVal ( tok , tokenlist , errorLogger , settings , [ ] ( const Token * ) {
return true ;
} ) ;
2018-11-16 06:12:28 +01:00
}
2018-11-21 08:43:57 +01:00
2021-08-07 20:51:18 +02:00
template < class Predicate >
2018-11-21 08:43:57 +01:00
void byDerefCopy ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings , Predicate pred ) const {
2021-02-24 22:00:06 +01:00
if ( ! settings - > certainty . isEnabled ( Certainty : : inconclusive ) & & inconclusive )
2019-09-11 19:25:09 +02:00
return ;
2019-07-26 07:02:07 +02:00
if ( ! argtok )
return ;
2018-11-21 08:43:57 +01:00
for ( const ValueFlow : : Value & v : argtok - > values ( ) ) {
if ( ! v . isLifetimeValue ( ) )
continue ;
const Token * tok2 = v . tokvalue ;
2019-01-29 09:47:52 +01:00
ErrorPath er = v . errorPath ;
const Variable * var = getLifetimeVariable ( tok2 , er ) ;
er . insert ( er . end ( ) , errorPath . begin ( ) , errorPath . end ( ) ) ;
2018-11-21 08:43:57 +01:00
if ( ! var )
continue ;
2018-12-02 14:31:31 +01:00
for ( const Token * tok3 = tok ; tok3 & & tok3 ! = var - > declEndToken ( ) ; tok3 = tok3 - > previous ( ) ) {
2018-11-21 08:43:57 +01:00
if ( tok3 - > varId ( ) = = var - > declarationId ( ) ) {
2021-08-07 20:51:18 +02:00
LifetimeStore { tok3 , message , type , inconclusive } . byVal ( tok , tokenlist , errorLogger , settings , pred ) ;
2018-11-21 08:43:57 +01:00
break ;
}
}
}
}
void byDerefCopy ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) const {
2019-02-22 06:38:56 +01:00
byDerefCopy ( tok , tokenlist , errorLogger , settings , [ ] ( const Token * ) {
2018-11-21 08:43:57 +01:00
return true ;
} ) ;
}
2020-10-03 11:01:53 +02:00
2020-10-03 11:02:11 +02:00
private :
2020-10-03 11:01:53 +02:00
Context * mContext ;
2020-10-03 11:02:11 +02:00
void forwardLifetime ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) const {
2020-10-03 11:01:53 +02:00
if ( mContext ) {
mContext - > tok = tok ;
mContext - > tokenlist = tokenlist ;
mContext - > errorLogger = errorLogger ;
mContext - > settings = settings ;
}
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
2018-11-16 06:12:28 +01:00
} ;
2022-05-05 06:54:36 +02:00
static void valueFlowLifetimeUserConstructor ( Token * tok ,
const Function * constructor ,
const std : : string & name ,
std : : vector < const Token * > args ,
TokenList * tokenlist ,
ErrorLogger * errorLogger ,
const Settings * settings )
2022-04-14 18:59:12 +02:00
{
if ( ! constructor )
return ;
std : : unordered_map < const Token * , const Variable * > argToParam ;
for ( std : : size_t i = 0 ; i < args . size ( ) ; i + + )
argToParam [ args [ i ] ] = constructor - > getArgumentVar ( i ) ;
if ( const Token * initList = constructor - > constructorMemberInitialization ( ) ) {
std : : unordered_map < const Variable * , LifetimeCapture > paramCapture ;
for ( const Token * tok2 : astFlatten ( initList - > astOperand2 ( ) , " , " ) ) {
if ( ! Token : : simpleMatch ( tok2 , " ( " ) )
continue ;
if ( ! tok2 - > astOperand1 ( ) )
continue ;
if ( ! tok2 - > astOperand2 ( ) )
continue ;
const Variable * var = tok2 - > astOperand1 ( ) - > variable ( ) ;
const Token * expr = tok2 - > astOperand2 ( ) ;
if ( ! var )
continue ;
2022-05-03 11:42:32 +02:00
if ( ! isLifetimeBorrowed ( expr , settings ) )
continue ;
2022-04-14 18:59:12 +02:00
const Variable * argvar = getLifetimeVariable ( expr ) ;
if ( var - > isReference ( ) | | var - > isRValueReference ( ) ) {
if ( argvar & & argvar - > isArgument ( ) & & ( argvar - > isReference ( ) | | argvar - > isRValueReference ( ) ) ) {
paramCapture [ argvar ] = LifetimeCapture : : ByReference ;
}
} else {
bool found = false ;
for ( const ValueFlow : : Value & v : expr - > values ( ) ) {
if ( ! v . isLifetimeValue ( ) )
continue ;
if ( v . path > 0 )
continue ;
if ( ! v . tokvalue )
continue ;
const Variable * lifeVar = v . tokvalue - > variable ( ) ;
if ( ! lifeVar )
continue ;
LifetimeCapture c = LifetimeCapture : : Undefined ;
if ( ! v . isArgumentLifetimeValue ( ) & & ( lifeVar - > isReference ( ) | | lifeVar - > isRValueReference ( ) ) )
c = LifetimeCapture : : ByReference ;
else if ( v . isArgumentLifetimeValue ( ) )
c = LifetimeCapture : : ByValue ;
if ( c ! = LifetimeCapture : : Undefined ) {
paramCapture [ lifeVar ] = c ;
found = true ;
}
}
if ( ! found & & argvar & & argvar - > isArgument ( ) )
paramCapture [ argvar ] = LifetimeCapture : : ByValue ;
}
}
// TODO: Use SubExpressionAnalyzer for members
LifetimeStore : : forEach ( args ,
" Passed to constructor of ' " + name + " '. " ,
ValueFlow : : Value : : LifetimeKind : : SubObject ,
[ & ] ( const LifetimeStore & ls ) {
const Variable * paramVar = argToParam . at ( ls . argtok ) ;
if ( paramCapture . count ( paramVar ) = = 0 )
return ;
LifetimeCapture c = paramCapture . at ( paramVar ) ;
if ( c = = LifetimeCapture : : ByReference )
ls . byRef ( tok , tokenlist , errorLogger , settings ) ;
else
ls . byVal ( tok , tokenlist , errorLogger , settings ) ;
} ) ;
2022-04-30 08:23:58 +02:00
} else if ( ! constructor - > nestedIn - > varlist . empty ( ) ) {
2022-04-14 18:59:12 +02:00
LifetimeStore : : forEach ( args ,
" Passed to constructor of ' " + name + " '. " ,
ValueFlow : : Value : : LifetimeKind : : SubObject ,
[ & ] ( LifetimeStore & ls ) {
ls . inconclusive = true ;
const Variable * var = argToParam . at ( ls . argtok ) ;
if ( var & & ! var - > isConst ( ) & & var - > isReference ( ) )
ls . byRef ( tok , tokenlist , errorLogger , settings ) ;
else
ls . byVal ( tok , tokenlist , errorLogger , settings ) ;
} ) ;
}
}
2018-11-16 06:12:28 +01:00
static void valueFlowLifetimeFunction ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
if ( ! Token : : Match ( tok , " %name% ( " ) )
return ;
2021-11-28 15:25:21 +01:00
Token * memtok = nullptr ;
if ( Token : : Match ( tok - > astParent ( ) , " . %name% ( " ) & & astIsRHS ( tok ) )
memtok = tok - > astParent ( ) - > astOperand1 ( ) ;
2019-12-25 09:32:50 +01:00
int returnContainer = settings - > library . returnValueContainer ( tok ) ;
if ( returnContainer > = 0 ) {
std : : vector < const Token * > args = getArguments ( tok ) ;
for ( int argnr = 1 ; argnr < = args . size ( ) ; + + argnr ) {
const Library : : ArgumentChecks : : IteratorInfo * i = settings - > library . getArgIteratorInfo ( tok , argnr ) ;
if ( ! i )
continue ;
if ( i - > container ! = returnContainer )
continue ;
const Token * const argTok = args [ argnr - 1 ] ;
2021-09-09 20:43:46 +02:00
bool forward = false ;
for ( ValueFlow : : Value val : argTok - > values ( ) ) {
if ( ! val . isLifetimeValue ( ) )
continue ;
val . errorPath . emplace_back ( argTok , " Passed to ' " + tok - > str ( ) + " '. " ) ;
setTokenValue ( tok - > next ( ) , val , settings ) ;
forward = true ;
}
2019-12-31 08:09:04 +01:00
// Check if lifetime is available to avoid adding the lifetime twice
2021-09-09 20:43:46 +02:00
if ( forward ) {
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
2019-12-31 08:09:04 +01:00
break ;
}
2019-12-25 09:32:50 +01:00
}
} else if ( Token : : Match ( tok - > tokAt ( - 2 ) , " std :: ref|cref|tie|front_inserter|back_inserter " ) ) {
2018-11-16 06:12:28 +01:00
for ( const Token * argtok : getArguments ( tok ) ) {
2021-08-07 20:51:18 +02:00
LifetimeStore { argtok , " Passed to ' " + tok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } . byRef (
2018-11-16 06:12:28 +01:00
tok - > next ( ) , tokenlist , errorLogger , settings ) ;
}
2020-09-18 07:44:26 +02:00
} else if ( Token : : Match ( tok - > tokAt ( - 2 ) , " std :: make_tuple|tuple_cat|make_pair|make_reverse_iterator|next|prev|move|bind " ) ) {
2018-11-16 06:12:28 +01:00
for ( const Token * argtok : getArguments ( tok ) ) {
2021-08-07 20:51:18 +02:00
LifetimeStore { argtok , " Passed to ' " + tok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } . byVal (
2018-11-16 06:12:28 +01:00
tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2018-11-14 06:59:25 +01:00
}
2021-11-28 15:25:21 +01:00
} else if ( memtok & & Token : : Match ( tok - > astParent ( ) , " . push_back|push_front|insert|push|assign " ) & &
astIsContainer ( memtok ) ) {
2018-11-21 08:43:57 +01:00
std : : vector < const Token * > args = getArguments ( tok ) ;
2018-12-02 14:31:31 +01:00
std : : size_t n = args . size ( ) ;
2019-06-02 10:14:48 +02:00
if ( n > 1 & & Token : : typeStr ( args [ n - 2 ] ) = = Token : : typeStr ( args [ n - 1 ] ) & &
2018-12-02 14:31:31 +01:00
( ( ( astIsIterator ( args [ n - 2 ] ) & & astIsIterator ( args [ n - 1 ] ) ) | |
( astIsPointer ( args [ n - 2 ] ) & & astIsPointer ( args [ n - 1 ] ) ) ) ) ) {
2021-11-28 15:25:21 +01:00
LifetimeStore {
args . back ( ) , " Added to container ' " + memtok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object }
. byDerefCopy ( memtok , tokenlist , errorLogger , settings ) ;
2019-05-05 11:40:59 +02:00
} else if ( ! args . empty ( ) & & isLifetimeBorrowed ( args . back ( ) , settings ) ) {
2021-11-28 15:25:21 +01:00
LifetimeStore {
args . back ( ) , " Added to container ' " + memtok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object }
. byVal ( memtok , tokenlist , errorLogger , settings ) ;
2018-11-21 08:43:57 +01:00
}
2019-01-29 09:47:52 +01:00
} else if ( tok - > function ( ) ) {
const Function * f = tok - > function ( ) ;
2022-04-14 18:59:12 +02:00
if ( f - > isConstructor ( ) ) {
2022-05-05 06:54:36 +02:00
valueFlowLifetimeUserConstructor ( tok - > next ( ) , f , tok - > str ( ) , getArguments ( tok ) , tokenlist , errorLogger , settings ) ;
2022-04-14 18:59:12 +02:00
return ;
}
2019-01-29 09:47:52 +01:00
if ( Function : : returnsReference ( f ) )
return ;
2020-05-31 10:10:10 +02:00
std : : vector < const Token * > returns = Function : : findReturns ( f ) ;
2019-09-11 19:25:09 +02:00
const bool inconclusive = returns . size ( ) > 1 ;
2020-10-03 11:01:53 +02:00
bool update = false ;
2019-09-11 19:25:09 +02:00
for ( const Token * returnTok : returns ) {
if ( returnTok = = tok )
2019-01-29 09:47:52 +01:00
continue ;
2020-09-17 07:23:38 +02:00
const Variable * returnVar = getLifetimeVariable ( returnTok ) ;
2019-09-11 19:25:09 +02:00
if ( returnVar & & returnVar - > isArgument ( ) & & ( returnVar - > isConst ( ) | | ! isVariableChanged ( returnVar , settings , tokenlist - > isCPP ( ) ) ) ) {
LifetimeStore ls = LifetimeStore : : fromFunctionArg ( f , tok , returnVar , tokenlist , errorLogger ) ;
ls . inconclusive = inconclusive ;
2020-10-03 11:01:53 +02:00
ls . forward = false ;
update | = ls . byVal ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2019-01-29 09:47:52 +01:00
}
2019-09-11 19:25:09 +02:00
for ( const ValueFlow : : Value & v : returnTok - > values ( ) ) {
if ( ! v . isLifetimeValue ( ) )
continue ;
if ( ! v . tokvalue )
continue ;
2021-12-04 17:00:55 +01:00
if ( memtok & &
( contains ( { ValueFlow : : Value : : LifetimeScope : : ThisPointer , ValueFlow : : Value : : LifetimeScope : : ThisValue } ,
v . lifetimeScope ) | |
exprDependsOnThis ( v . tokvalue ) ) ) {
2021-11-28 15:25:21 +01:00
LifetimeStore ls = LifetimeStore { memtok ,
" Passed to member function ' " + tok - > expressionString ( ) + " '. " ,
ValueFlow : : Value : : LifetimeKind : : Object } ;
ls . inconclusive = inconclusive ;
ls . forward = false ;
ls . errorPath = v . errorPath ;
ls . errorPath . emplace_front ( returnTok , " Return " + lifetimeType ( returnTok , & v ) + " . " ) ;
2021-12-04 17:00:55 +01:00
if ( v . lifetimeScope = = ValueFlow : : Value : : LifetimeScope : : ThisValue )
update | = ls . byVal ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
else
update | = ls . byRef ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2021-11-28 15:25:21 +01:00
continue ;
}
2019-09-11 19:25:09 +02:00
const Variable * var = v . tokvalue - > variable ( ) ;
LifetimeStore ls = LifetimeStore : : fromFunctionArg ( f , tok , var , tokenlist , errorLogger ) ;
if ( ! ls . argtok )
continue ;
2020-10-03 11:01:53 +02:00
ls . forward = false ;
2019-09-11 19:25:09 +02:00
ls . inconclusive = inconclusive ;
ls . errorPath = v . errorPath ;
ls . errorPath . emplace_front ( returnTok , " Return " + lifetimeType ( returnTok , & v ) + " . " ) ;
2020-09-17 07:23:38 +02:00
if ( ! v . isArgumentLifetimeValue ( ) & & ( var - > isReference ( ) | | var - > isRValueReference ( ) ) ) {
2020-10-03 11:01:53 +02:00
update | = ls . byRef ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2019-09-11 19:25:09 +02:00
} else if ( v . isArgumentLifetimeValue ( ) ) {
2020-10-03 11:01:53 +02:00
update | = ls . byVal ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2019-09-11 19:25:09 +02:00
}
}
2019-01-29 09:47:52 +01:00
}
2020-10-03 11:01:53 +02:00
if ( update )
valueFlowForwardLifetime ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2021-10-05 08:28:19 +02:00
} else if ( tok - > valueType ( ) ) {
// TODO: Propagate lifetimes with library functions
if ( settings - > library . getFunction ( tok - > previous ( ) ) )
return ;
// Assume constructing the valueType
2022-02-28 18:54:55 +01:00
valueFlowLifetimeConstructor ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2021-10-05 08:28:19 +02:00
valueFlowForwardLifetime ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2018-11-14 06:59:25 +01:00
}
}
2022-02-28 18:54:55 +01:00
static bool isScope ( const Token * tok )
{
if ( ! tok )
return false ;
if ( ! Token : : simpleMatch ( tok , " { " ) )
return false ;
const Scope * scope = tok - > scope ( ) ;
if ( ! scope )
return false ;
if ( ! scope - > bodyStart )
return false ;
return scope - > bodyStart = = tok ;
}
static const Function * findConstructor ( const Scope * scope , const Token * tok , const std : : vector < const Token * > & args )
{
if ( ! tok )
return nullptr ;
const Function * f = tok - > function ( ) ;
if ( ! f & & tok - > astOperand1 ( ) )
f = tok - > astOperand1 ( ) - > function ( ) ;
// Search for a constructor
if ( ! f | | ! f - > isConstructor ( ) ) {
f = nullptr ;
std : : vector < const Function * > candidates ;
for ( const Function & function : scope - > functionList ) {
if ( function . argCount ( ) ! = args . size ( ) )
continue ;
if ( ! function . isConstructor ( ) )
continue ;
candidates . push_back ( & function ) ;
}
// TODO: Narrow the candidates
if ( candidates . size ( ) = = 1 )
f = candidates . front ( ) ;
}
if ( ! f )
return nullptr ;
return f ;
}
2022-05-05 06:54:36 +02:00
static void valueFlowLifetimeClassConstructor ( Token * tok ,
const Type * t ,
TokenList * tokenlist ,
ErrorLogger * errorLogger ,
const Settings * settings )
2019-03-19 06:25:10 +01:00
{
if ( ! Token : : Match ( tok , " (|{ " ) )
return ;
2022-02-28 18:54:55 +01:00
if ( isScope ( tok ) )
return ;
2020-09-20 14:27:09 +02:00
if ( ! t ) {
if ( tok - > valueType ( ) & & tok - > valueType ( ) - > type ! = ValueType : : RECORD )
return ;
2022-02-28 18:54:55 +01:00
if ( tok - > str ( ) ! = " { " & & ! Token : : Match ( tok - > previous ( ) , " %var% ( " ) & & ! isVariableDecl ( tok - > previous ( ) ) )
return ;
2020-09-20 14:27:09 +02:00
// If the type is unknown then assume it captures by value in the
// constructor, but make each lifetime inconclusive
std : : vector < const Token * > args = getArguments ( tok ) ;
2022-02-28 18:54:55 +01:00
LifetimeStore : : forEach ( args ,
" Passed to initializer list. " ,
ValueFlow : : Value : : LifetimeKind : : SubObject ,
[ & ] ( LifetimeStore & ls ) {
2020-10-03 11:02:11 +02:00
ls . inconclusive = true ;
ls . byVal ( tok , tokenlist , errorLogger , settings ) ;
} ) ;
2020-09-20 14:27:09 +02:00
return ;
}
2019-09-10 19:41:35 +02:00
const Scope * scope = t - > classScope ;
if ( ! scope )
return ;
// Only support aggregate constructors for now
2022-02-28 18:54:55 +01:00
if ( t - > derivedFrom . empty ( ) & & ( t - > isClassType ( ) | | t - > isStructType ( ) ) ) {
2019-09-10 19:41:35 +02:00
std : : vector < const Token * > args = getArguments ( tok ) ;
2022-02-28 18:54:55 +01:00
if ( scope - > numConstructors = = 0 ) {
auto it = scope - > varlist . begin ( ) ;
LifetimeStore : : forEach ( args ,
" Passed to constructor of ' " + t - > name ( ) + " '. " ,
ValueFlow : : Value : : LifetimeKind : : SubObject ,
[ & ] ( const LifetimeStore & ls ) {
if ( it = = scope - > varlist . end ( ) )
return ;
const Variable & var = * it ;
if ( var . isReference ( ) | | var . isRValueReference ( ) ) {
ls . byRef ( tok , tokenlist , errorLogger , settings ) ;
} else {
ls . byVal ( tok , tokenlist , errorLogger , settings ) ;
}
it + + ;
} ) ;
} else {
const Function * constructor = findConstructor ( scope , tok , args ) ;
2022-05-05 06:54:36 +02:00
valueFlowLifetimeUserConstructor ( tok , constructor , t - > name ( ) , args , tokenlist , errorLogger , settings ) ;
2022-02-28 18:54:55 +01:00
}
2019-09-10 19:41:35 +02:00
}
}
2019-11-17 03:22:04 +01:00
2019-09-10 19:41:35 +02:00
static void valueFlowLifetimeConstructor ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
if ( ! Token : : Match ( tok , " (|{ " ) )
return ;
2022-02-28 18:54:55 +01:00
if ( isScope ( tok ) )
return ;
2022-05-05 06:54:36 +02:00
std : : vector < ValueType > vts ;
if ( tok - > valueType ( ) ) {
vts = { * tok - > valueType ( ) } ;
} else if ( Token : : Match ( tok - > previous ( ) , " %var% { | ( " ) && isVariableDecl(tok->previous()) &&
tok - > previous ( ) - > valueType ( ) ) {
vts = { * tok - > previous ( ) - > valueType ( ) } ;
} else if ( Token : : simpleMatch ( tok , " { " ) & & ! Token : : Match ( tok - > previous ( ) , " %name% " ) ) {
vts = getParentValueTypes ( tok , settings ) ;
}
for ( const ValueType & vt : vts ) {
2022-05-13 06:51:07 +02:00
if ( vt . pointer > 0 ) {
std : : vector < const Token * > args = getArguments ( tok ) ;
LifetimeStore : : forEach ( args ,
" Passed to initializer list. " ,
ValueFlow : : Value : : LifetimeKind : : SubObject ,
[ & ] ( const LifetimeStore & ls ) {
ls . byVal ( tok , tokenlist , errorLogger , settings ) ;
} ) ;
} else if ( vt . container & & vt . type = = ValueType : : CONTAINER ) {
2022-05-05 06:54:36 +02:00
std : : vector < const Token * > args = getArguments ( tok ) ;
if ( args . size ( ) = = 1 & & vt . container - > view & & astIsContainerOwned ( args . front ( ) ) ) {
LifetimeStore { args . front ( ) , " Passed to container view. " , ValueFlow : : Value : : LifetimeKind : : SubObject }
. byRef ( tok , tokenlist , errorLogger , settings ) ;
} else if ( args . size ( ) = = 2 & & astIsIterator ( args [ 0 ] ) & & astIsIterator ( args [ 1 ] ) ) {
LifetimeStore : : forEach (
args ,
" Passed to initializer list. " ,
ValueFlow : : Value : : LifetimeKind : : SubObject ,
[ & ] ( const LifetimeStore & ls ) {
ls . byDerefCopy ( tok , tokenlist , errorLogger , settings ) ;
} ) ;
} else if ( vt . container - > hasInitializerListConstructor ) {
LifetimeStore : : forEach ( args ,
" Passed to initializer list. " ,
ValueFlow : : Value : : LifetimeKind : : SubObject ,
[ & ] ( const LifetimeStore & ls ) {
ls . byVal ( tok , tokenlist , errorLogger , settings ) ;
} ) ;
}
2020-04-02 10:17:58 +02:00
} else {
2022-05-13 06:51:07 +02:00
const Type * t = nullptr ;
if ( vt . typeScope & & vt . typeScope - > definedType )
t = vt . typeScope - > definedType ;
else
t = Token : : typeOf ( tok - > previous ( ) ) ;
valueFlowLifetimeClassConstructor ( tok , t , tokenlist , errorLogger , settings ) ;
2019-03-19 06:25:10 +01:00
}
}
}
2018-11-14 06:59:25 +01:00
struct Lambda {
2022-02-28 18:54:55 +01:00
explicit Lambda ( const Token * tok )
: capture ( nullptr ) ,
arguments ( nullptr ) ,
returnTok ( nullptr ) ,
bodyTok ( nullptr ) ,
explicitCaptures ( ) ,
implicitCapture ( LifetimeCapture : : Undefined )
{
2018-11-14 06:59:25 +01:00
if ( ! Token : : simpleMatch ( tok , " [ " ) | | ! tok - > link ( ) )
return ;
capture = tok ;
if ( Token : : simpleMatch ( capture - > link ( ) , " ] ( " ) ) {
arguments = capture - > link ( ) - > next ( ) ;
}
const Token * afterArguments = arguments ? arguments - > link ( ) - > next ( ) : capture - > link ( ) - > next ( ) ;
if ( afterArguments & & afterArguments - > originalName ( ) = = " -> " ) {
returnTok = afterArguments - > next ( ) ;
bodyTok = Token : : findsimplematch ( returnTok , " { " ) ;
} else if ( Token : : simpleMatch ( afterArguments , " { " ) ) {
bodyTok = afterArguments ;
}
2020-09-05 08:00:51 +02:00
for ( const Token * c : getCaptures ( ) ) {
2021-12-04 17:00:55 +01:00
if ( Token : : Match ( c , " this !!. " ) ) {
2022-02-28 18:54:55 +01:00
explicitCaptures [ c - > variable ( ) ] = std : : make_pair ( c , LifetimeCapture : : ByReference ) ;
2021-12-04 17:00:55 +01:00
} else if ( Token : : simpleMatch ( c , " * this " ) ) {
2022-02-28 18:54:55 +01:00
explicitCaptures [ c - > next ( ) - > variable ( ) ] = std : : make_pair ( c - > next ( ) , LifetimeCapture : : ByValue ) ;
2021-12-04 17:00:55 +01:00
} else if ( c - > variable ( ) ) {
2022-02-28 18:54:55 +01:00
explicitCaptures [ c - > variable ( ) ] = std : : make_pair ( c , LifetimeCapture : : ByValue ) ;
2020-09-05 07:56:01 +02:00
} else if ( c - > isUnaryOp ( " & " ) & & Token : : Match ( c - > astOperand1 ( ) , " %var% " ) ) {
2022-02-28 18:54:55 +01:00
explicitCaptures [ c - > astOperand1 ( ) - > variable ( ) ] =
std : : make_pair ( c - > astOperand1 ( ) , LifetimeCapture : : ByReference ) ;
2020-09-05 07:56:01 +02:00
} else {
const std : : string & s = c - > expressionString ( ) ;
if ( s = = " = " )
2022-02-28 18:54:55 +01:00
implicitCapture = LifetimeCapture : : ByValue ;
2020-09-05 07:56:01 +02:00
else if ( s = = " & " )
2022-02-28 18:54:55 +01:00
implicitCapture = LifetimeCapture : : ByReference ;
2020-09-05 07:56:01 +02:00
}
}
2018-11-14 06:59:25 +01:00
}
const Token * capture ;
const Token * arguments ;
const Token * returnTok ;
const Token * bodyTok ;
2022-02-28 18:54:55 +01:00
std : : unordered_map < const Variable * , std : : pair < const Token * , LifetimeCapture > > explicitCaptures ;
LifetimeCapture implicitCapture ;
2020-09-05 07:56:01 +02:00
std : : vector < const Token * > getCaptures ( ) {
return getArguments ( capture ) ;
}
2018-11-14 06:59:25 +01:00
bool isLambda ( ) const {
return capture & & bodyTok ;
}
} ;
2019-12-10 17:48:27 +01:00
static bool isDecayedPointer ( const Token * tok )
2018-12-15 17:58:45 +01:00
{
if ( ! tok )
return false ;
2020-05-20 16:02:13 +02:00
if ( ! tok - > astParent ( ) )
return false ;
2018-12-15 17:58:45 +01:00
if ( astIsPointer ( tok - > astParent ( ) ) & & ! Token : : simpleMatch ( tok - > astParent ( ) , " return " ) )
return true ;
2020-05-19 21:03:28 +02:00
if ( tok - > astParent ( ) - > isConstOp ( ) )
2019-02-23 08:32:08 +01:00
return true ;
2018-12-15 17:58:45 +01:00
if ( ! Token : : simpleMatch ( tok - > astParent ( ) , " return " ) )
return false ;
2019-10-12 11:40:02 +02:00
return astIsPointer ( tok - > astParent ( ) ) ;
2018-12-15 17:58:45 +01:00
}
2021-10-05 08:28:19 +02:00
static bool isConvertedToView ( const Token * tok , const Settings * settings )
{
std : : vector < ValueType > vtParents = getParentValueTypes ( tok , settings ) ;
return std : : any_of ( vtParents . begin ( ) , vtParents . end ( ) , [ & ] ( const ValueType & vt ) {
if ( ! vt . container )
return false ;
return vt . container - > view ;
} ) ;
}
2018-11-14 06:59:25 +01:00
static void valueFlowLifetime ( TokenList * tokenlist , SymbolDatabase * , ErrorLogger * errorLogger , const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2019-01-29 09:47:52 +01:00
if ( ! tok - > scope ( ) )
continue ;
if ( tok - > scope ( ) - > type = = Scope : : eGlobal )
continue ;
2018-11-14 06:59:25 +01:00
Lambda lam ( tok ) ;
2022-03-16 15:28:44 +01:00
// Lambdas
2018-11-14 06:59:25 +01:00
if ( lam . isLambda ( ) ) {
const Scope * bodyScope = lam . bodyTok - > scope ( ) ;
std : : set < const Scope * > scopes ;
2020-09-05 07:56:01 +02:00
// Avoid capturing a variable twice
std : : set < nonneg int > varids ;
2021-12-04 17:00:55 +01:00
bool capturedThis = false ;
2018-11-14 06:59:25 +01:00
2020-09-05 07:56:01 +02:00
auto isImplicitCapturingVariable = [ & ] ( const Token * varTok ) {
2019-02-22 06:38:56 +01:00
const Variable * var = varTok - > variable ( ) ;
if ( ! var )
return false ;
2020-09-05 07:56:01 +02:00
if ( varids . count ( var - > declarationId ( ) ) > 0 )
return false ;
2019-11-05 07:10:32 +01:00
if ( ! var - > isLocal ( ) & & ! var - > isArgument ( ) )
return false ;
2018-11-16 06:12:28 +01:00
const Scope * scope = var - > scope ( ) ;
2018-12-01 19:11:26 +01:00
if ( ! scope )
return false ;
2018-11-16 06:12:28 +01:00
if ( scopes . count ( scope ) > 0 )
return false ;
if ( scope - > isNestedIn ( bodyScope ) )
return false ;
scopes . insert ( scope ) ;
2020-09-05 07:56:01 +02:00
varids . insert ( var - > declarationId ( ) ) ;
2018-11-16 06:12:28 +01:00
return true ;
} ;
2020-10-03 11:01:53 +02:00
bool update = false ;
2022-02-28 18:54:55 +01:00
auto captureVariable = [ & ] ( const Token * tok2 , LifetimeCapture c , std : : function < bool ( const Token * ) > pred ) {
2020-09-05 07:56:01 +02:00
if ( varids . count ( tok - > varId ( ) ) > 0 )
return ;
2018-11-14 06:59:25 +01:00
ErrorPath errorPath ;
2022-02-28 18:54:55 +01:00
if ( c = = LifetimeCapture : : ByReference ) {
2020-10-03 11:01:53 +02:00
LifetimeStore ls {
tok2 , " Lambda captures variable by reference here. " , ValueFlow : : Value : : LifetimeKind : : Lambda } ;
ls . forward = false ;
update | = ls . byRef ( tok , tokenlist , errorLogger , settings , pred ) ;
2022-02-28 18:54:55 +01:00
} else if ( c = = LifetimeCapture : : ByValue ) {
2020-10-03 11:01:53 +02:00
LifetimeStore ls {
tok2 , " Lambda captures variable by value here. " , ValueFlow : : Value : : LifetimeKind : : Lambda } ;
ls . forward = false ;
update | = ls . byVal ( tok , tokenlist , errorLogger , settings , pred ) ;
2021-08-25 04:06:03 +02:00
pred ( tok2 ) ;
2018-11-14 06:59:25 +01:00
}
2020-09-05 07:56:01 +02:00
} ;
2022-02-28 18:54:55 +01:00
auto captureThisVariable = [ & ] ( const Token * tok2 , LifetimeCapture c ) {
2021-12-04 17:00:55 +01:00
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
2022-02-28 18:54:55 +01:00
if ( c = = LifetimeCapture : : ByReference )
2021-12-04 17:00:55 +01:00
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : ThisPointer ;
2022-02-28 18:54:55 +01:00
else if ( c = = LifetimeCapture : : ByValue )
2021-12-04 17:00:55 +01:00
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : ThisValue ;
value . tokvalue = tok2 ;
value . errorPath . push_back ( { tok2 , " Lambda captures the 'this' variable here. " } ) ;
value . lifetimeKind = ValueFlow : : Value : : LifetimeKind : : Lambda ;
capturedThis = true ;
// Don't add the value a second time
if ( std : : find ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , value ) ! = tok - > values ( ) . end ( ) )
return ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
update | = true ;
} ;
2020-09-05 07:56:01 +02:00
// Handle explicit capture
2020-09-05 08:00:51 +02:00
for ( const auto & p : lam . explicitCaptures ) {
2020-09-05 07:56:01 +02:00
const Variable * var = p . first ;
const Token * tok2 = p . second . first ;
2022-02-28 18:54:55 +01:00
LifetimeCapture c = p . second . second ;
2021-12-04 17:00:55 +01:00
if ( Token : : Match ( tok2 , " this !!. " ) ) {
captureThisVariable ( tok2 , c ) ;
} else if ( var ) {
captureVariable ( tok2 , c , [ ] ( const Token * ) {
return true ;
} ) ;
varids . insert ( var - > declarationId ( ) ) ;
}
2018-11-14 06:59:25 +01:00
}
2020-09-05 07:56:01 +02:00
2021-12-04 17:00:55 +01:00
auto isImplicitCapturingThis = [ & ] ( const Token * tok2 ) {
if ( capturedThis )
return false ;
if ( Token : : simpleMatch ( tok2 , " this " ) ) {
return true ;
} else if ( tok2 - > variable ( ) ) {
if ( Token : : simpleMatch ( tok2 - > previous ( ) , " . " ) )
return false ;
const Variable * var = tok2 - > variable ( ) ;
if ( var - > isLocal ( ) )
return false ;
if ( var - > isArgument ( ) )
return false ;
return exprDependsOnThis ( tok2 ) ;
} else if ( Token : : simpleMatch ( tok2 , " ( " ) ) {
return exprDependsOnThis ( tok2 ) ;
}
return false ;
} ;
2020-09-05 07:56:01 +02:00
for ( const Token * tok2 = lam . bodyTok ; tok2 ! = lam . bodyTok - > link ( ) ; tok2 = tok2 - > next ( ) ) {
2021-12-04 17:00:55 +01:00
if ( isImplicitCapturingThis ( tok2 ) ) {
2022-02-28 18:54:55 +01:00
captureThisVariable ( tok2 , LifetimeCapture : : ByReference ) ;
2021-12-04 17:00:55 +01:00
} else if ( tok2 - > variable ( ) ) {
captureVariable ( tok2 , lam . implicitCapture , isImplicitCapturingVariable ) ;
}
2018-11-14 06:59:25 +01:00
}
2020-10-03 11:01:53 +02:00
if ( update )
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
2018-11-14 06:59:25 +01:00
}
// address of
else if ( tok - > isUnaryOp ( " & " ) ) {
2019-09-11 19:25:09 +02:00
for ( const LifetimeToken & lt : getLifetimeTokens ( tok - > astOperand1 ( ) ) ) {
2021-02-24 22:00:06 +01:00
if ( ! settings - > certainty . isEnabled ( Certainty : : inconclusive ) & & lt . inconclusive )
2019-09-11 19:25:09 +02:00
continue ;
ErrorPath errorPath = lt . errorPath ;
errorPath . emplace_back ( tok , " Address of variable taken here. " ) ;
2018-11-14 06:59:25 +01:00
2019-09-11 19:25:09 +02:00
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Local ;
value . tokvalue = lt . token ;
value . errorPath = std : : move ( errorPath ) ;
2020-06-01 08:53:08 +02:00
if ( lt . addressOf | | astIsPointer ( lt . token ) | | ! Token : : Match ( lt . token - > astParent ( ) , " .|[ " ) )
2019-09-11 19:25:09 +02:00
value . lifetimeKind = ValueFlow : : Value : : LifetimeKind : : Address ;
value . setInconclusive ( lt . inconclusive ) ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2018-11-14 06:59:25 +01:00
2019-09-11 19:25:09 +02:00
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
2018-11-14 06:59:25 +01:00
}
2021-10-05 08:28:19 +02:00
// Converting to container view
else if ( astIsContainerOwned ( tok ) & & isConvertedToView ( tok , settings ) ) {
LifetimeStore ls =
LifetimeStore { tok , " Converted to container view " , ValueFlow : : Value : : LifetimeKind : : SubObject } ;
ls . byRef ( tok , tokenlist , errorLogger , settings ) ;
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
2018-11-14 06:59:25 +01:00
// container lifetimes
2019-08-15 21:14:27 +02:00
else if ( astIsContainer ( tok ) ) {
Token * parent = astParentSkipParens ( tok ) ;
if ( ! Token : : Match ( parent , " . %name% ( " ) )
2018-11-14 06:59:25 +01:00
continue ;
2020-07-15 19:22:36 +02:00
bool isContainerOfPointers = true ;
const Token * containerTypeToken = tok - > valueType ( ) - > containerTypeToken ;
2020-07-16 21:33:39 +02:00
if ( containerTypeToken ) {
ValueType vt = ValueType : : parseDecl ( containerTypeToken , settings ) ;
isContainerOfPointers = vt . pointer > 0 ;
2020-07-15 19:22:36 +02:00
}
2021-09-05 07:35:33 +02:00
ValueFlow : : Value master ;
master . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
master . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Local ;
if ( astIsIterator ( parent - > tokAt ( 2 ) ) ) {
master . errorPath . emplace_back ( parent - > tokAt ( 2 ) , " Iterator to container is created here. " ) ;
master . lifetimeKind = ValueFlow : : Value : : LifetimeKind : : Iterator ;
} else if ( ( astIsPointer ( parent - > tokAt ( 2 ) ) & & ! isContainerOfPointers ) | |
Token : : Match ( parent - > next ( ) , " data|c_str " ) ) {
master . errorPath . emplace_back ( parent - > tokAt ( 2 ) , " Pointer to container is created here. " ) ;
master . lifetimeKind = ValueFlow : : Value : : LifetimeKind : : Object ;
} else {
2019-08-15 21:14:27 +02:00
continue ;
2021-09-05 07:35:33 +02:00
}
2018-11-14 06:59:25 +01:00
2021-09-05 07:35:33 +02:00
std : : vector < const Token * > toks = { } ;
if ( tok - > isUnaryOp ( " * " ) | | parent - > originalName ( ) = = " -> " ) {
for ( const ValueFlow : : Value & v : tok - > values ( ) ) {
2022-01-10 07:34:26 +01:00
if ( ! v . isLocalLifetimeValue ( ) )
2021-09-05 07:35:33 +02:00
continue ;
2022-01-10 07:34:26 +01:00
if ( v . lifetimeKind ! = ValueFlow : : Value : : LifetimeKind : : Address )
2021-09-05 07:35:33 +02:00
continue ;
if ( ! v . tokvalue )
continue ;
toks . push_back ( v . tokvalue ) ;
2021-10-05 08:28:19 +02:00
}
} else if ( astIsContainerView ( tok ) ) {
for ( const ValueFlow : : Value & v : tok - > values ( ) ) {
if ( ! v . isLifetimeValue ( ) )
continue ;
if ( ! v . tokvalue )
continue ;
if ( ! astIsContainerOwned ( v . tokvalue ) )
continue ;
toks . push_back ( v . tokvalue ) ;
2021-09-05 07:35:33 +02:00
}
} else {
toks = { tok } ;
}
2018-11-14 06:59:25 +01:00
2021-09-05 07:35:33 +02:00
for ( const Token * tok2 : toks ) {
for ( const ReferenceToken & rt : followAllReferences ( tok2 , false ) ) {
ValueFlow : : Value value = master ;
value . tokvalue = rt . token ;
value . errorPath . insert ( value . errorPath . begin ( ) , rt . errors . begin ( ) , rt . errors . end ( ) ) ;
setTokenValue ( parent - > tokAt ( 2 ) , value , tokenlist - > getSettings ( ) ) ;
if ( ! rt . token - > variable ( ) ) {
LifetimeStore ls = LifetimeStore {
rt . token , master . errorPath . back ( ) . second , ValueFlow : : Value : : LifetimeKind : : Object } ;
ls . byRef ( parent - > tokAt ( 2 ) , tokenlist , errorLogger , settings ) ;
}
}
}
valueFlowForwardLifetime ( parent - > tokAt ( 2 ) , tokenlist , errorLogger , settings ) ;
2018-11-14 06:59:25 +01:00
}
2019-03-19 06:25:10 +01:00
// Check constructors
2022-05-13 06:51:07 +02:00
else if ( Token : : Match ( tok , " =|return|%name%|{|,|> { " ) & & ! isScope ( tok - > next ( ) ) ) {
2019-03-19 06:25:10 +01:00
valueFlowLifetimeConstructor ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
}
2018-11-16 06:12:28 +01:00
// Check function calls
2021-12-04 17:00:55 +01:00
else if ( Token : : Match ( tok , " %name% ( " ) & & ! Token : : simpleMatch ( tok - > next ( ) - > link ( ) , " ) { " )) {
2018-11-16 06:12:28 +01:00
valueFlowLifetimeFunction ( tok , tokenlist , errorLogger , settings ) ;
}
2021-10-06 08:46:25 +02:00
// Unique pointer lifetimes
2021-11-11 08:00:05 +01:00
else if ( astIsUniqueSmartPointer ( tok ) & & astIsLHS ( tok ) & & Token : : simpleMatch ( tok - > astParent ( ) , " . get ( ) " ) ) {
2021-10-06 08:46:25 +02:00
Token * ptok = tok - > astParent ( ) - > tokAt ( 2 ) ;
ErrorPath errorPath = { { ptok , " Raw pointer to smart pointer created here. " } } ;
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Local ;
value . lifetimeKind = ValueFlow : : Value : : LifetimeKind : : SubObject ;
value . tokvalue = tok ;
value . errorPath = errorPath ;
setTokenValue ( ptok , value , tokenlist - > getSettings ( ) ) ;
valueFlowForwardLifetime ( ptok , tokenlist , errorLogger , settings ) ;
}
2018-11-14 06:59:25 +01:00
// Check variables
else if ( tok - > variable ( ) ) {
ErrorPath errorPath ;
const Variable * var = getLifetimeVariable ( tok , errorPath ) ;
if ( ! var )
continue ;
2018-12-15 17:58:45 +01:00
if ( var - > nameToken ( ) = = tok )
continue ;
2019-12-10 17:48:27 +01:00
if ( var - > isArray ( ) & & ! var - > isStlType ( ) & & ! var - > isArgument ( ) & & isDecayedPointer ( tok ) ) {
2018-11-14 06:59:25 +01:00
errorPath . emplace_back ( tok , " Array decayed to pointer here. " ) ;
ValueFlow : : Value value ;
2019-07-16 11:12:35 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Local ;
2018-11-14 06:59:25 +01:00
value . tokvalue = var - > nameToken ( ) ;
value . errorPath = errorPath ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
}
2020-09-04 18:56:34 +02:00
// Forward any lifetimes
else if ( std : : any_of ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , std : : mem_fn ( & ValueFlow : : Value : : isLifetimeValue ) ) ) {
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
2018-11-14 06:59:25 +01:00
}
}
2016-11-20 15:14:49 +01:00
static bool isStdMoveOrStdForwarded ( Token * tok , ValueFlow : : Value : : MoveKind * moveKind , Token * * varTok = nullptr )
{
if ( tok - > str ( ) ! = " std " )
return false ;
2019-07-10 15:27:07 +02:00
ValueFlow : : Value : : MoveKind kind = ValueFlow : : Value : : MoveKind : : NonMovedVariable ;
2016-11-20 15:14:49 +01:00
Token * variableToken = nullptr ;
if ( Token : : Match ( tok , " std :: move ( %var% ) " ) ) {
variableToken = tok - > tokAt ( 4 ) ;
2019-07-10 15:27:07 +02:00
kind = ValueFlow : : Value : : MoveKind : : MovedVariable ;
2016-11-20 15:14:49 +01:00
} else if ( Token : : simpleMatch ( tok , " std :: forward < " ) ) {
2018-09-23 20:24:51 +02:00
const Token * const leftAngle = tok - > tokAt ( 3 ) ;
2016-11-20 15:14:49 +01:00
Token * rightAngle = leftAngle - > link ( ) ;
if ( Token : : Match ( rightAngle , " > ( %var% ) " ) ) {
variableToken = rightAngle - > tokAt ( 2 ) ;
2019-07-10 15:27:07 +02:00
kind = ValueFlow : : Value : : MoveKind : : ForwardedVariable ;
2016-11-20 15:14:49 +01:00
}
}
2017-09-01 17:19:25 +02:00
if ( ! variableToken )
2016-11-20 15:14:49 +01:00
return false ;
if ( variableToken - > strAt ( 2 ) = = " . " ) // Only partially moved
return false ;
2019-12-21 07:39:14 +01:00
if ( variableToken - > valueType ( ) & & variableToken - > valueType ( ) - > type > = ValueType : : Type : : VOID )
return false ;
2016-11-20 15:14:49 +01:00
if ( moveKind ! = nullptr )
* moveKind = kind ;
if ( varTok ! = nullptr )
* varTok = variableToken ;
return true ;
}
2019-07-15 13:47:17 +02:00
static bool isOpenParenthesisMemberFunctionCallOfVarId ( const Token * openParenthesisToken , nonneg int varId )
2016-12-11 22:32:57 +01:00
{
2017-09-01 17:16:08 +02:00
const Token * varTok = openParenthesisToken - > tokAt ( - 3 ) ;
return Token : : Match ( varTok , " %varid% . %name% ( " , varId ) & &
2022-04-09 19:02:37 +02:00
varTok - > next ( ) - > originalName ( ) . empty ( ) ;
2016-12-11 22:32:57 +01:00
}
2016-12-12 21:46:05 +01:00
static const Token * findOpenParentesisOfMove ( const Token * moveVarTok )
{
const Token * tok = moveVarTok ;
while ( tok & & tok - > str ( ) ! = " ( " )
tok = tok - > previous ( ) ;
return tok ;
}
static const Token * findEndOfFunctionCallForParameter ( const Token * parameterToken )
{
if ( ! parameterToken )
return nullptr ;
const Token * parent = parameterToken - > astParent ( ) ;
2016-12-17 18:51:16 +01:00
while ( parent & & ! parent - > isOp ( ) & & parent - > str ( ) ! = " ( " )
2016-12-12 21:46:05 +01:00
parent = parent - > astParent ( ) ;
if ( ! parent )
return nullptr ;
return nextAfterAstRightmostLeaf ( parent ) ;
}
2021-07-18 07:46:31 +02:00
static void valueFlowAfterMove ( TokenList * tokenlist , SymbolDatabase * symboldatabase , const Settings * settings )
2016-11-20 15:14:49 +01:00
{
if ( ! tokenlist - > isCPP ( ) | | settings - > standards . cpp < Standards : : CPP11 )
return ;
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2016-11-20 15:14:49 +01:00
if ( ! scope )
continue ;
2018-04-27 22:36:30 +02:00
const Token * start = scope - > bodyStart ;
2016-11-20 15:14:49 +01:00
if ( scope - > function ) {
const Token * memberInitializationTok = scope - > function - > constructorMemberInitialization ( ) ;
if ( memberInitializationTok )
start = memberInitializationTok ;
}
2018-04-27 22:36:30 +02:00
for ( Token * tok = const_cast < Token * > ( start ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2016-11-20 15:14:49 +01:00
Token * varTok ;
2022-04-09 19:02:37 +02:00
if ( Token : : Match ( tok , " %var% . reset|clear ( " ) & & tok - > next ( ) - > originalName ( ) . empty ( ) ) {
2016-12-12 14:29:27 +01:00
varTok = tok ;
ValueFlow : : Value value ;
2019-07-10 15:27:07 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : MOVED ;
value . moveKind = ValueFlow : : Value : : MoveKind : : NonMovedVariable ;
2018-04-11 09:44:35 +02:00
value . errorPath . emplace_back ( tok , " Calling " + tok - > next ( ) - > expressionString ( ) + " makes " + tok - > str ( ) + " 'non-moved' " ) ;
2016-12-12 14:29:27 +01:00
value . setKnown ( ) ;
std : : list < ValueFlow : : Value > values ;
values . push_back ( value ) ;
const Variable * var = varTok - > variable ( ) ;
if ( ! var | | ( ! var - > isLocal ( ) & & ! var - > isArgument ( ) ) )
continue ;
2020-01-11 09:17:32 +01:00
const Token * const endOfVarScope = var - > scope ( ) - > bodyEnd ;
2016-12-12 14:29:27 +01:00
setTokenValue ( varTok , value , settings ) ;
2021-07-18 07:46:31 +02:00
valueFlowForward ( varTok - > next ( ) , endOfVarScope , varTok , values , tokenlist , settings ) ;
2016-12-12 14:29:27 +01:00
continue ;
}
2016-11-20 15:14:49 +01:00
ValueFlow : : Value : : MoveKind moveKind ;
if ( ! isStdMoveOrStdForwarded ( tok , & moveKind , & varTok ) )
continue ;
2021-01-27 19:49:13 +01:00
const nonneg int varId = varTok - > varId ( ) ;
2016-11-20 15:14:49 +01:00
// x is not MOVED after assignment if code is: x = ... std::move(x) .. ;
const Token * parent = tok - > astParent ( ) ;
2016-12-11 22:32:57 +01:00
while ( parent & & parent - > str ( ) ! = " = " & & parent - > str ( ) ! = " return " & &
! ( parent - > str ( ) = = " ( " & & isOpenParenthesisMemberFunctionCallOfVarId ( parent , varId ) ) )
2016-11-20 15:14:49 +01:00
parent = parent - > astParent ( ) ;
2016-12-11 22:32:57 +01:00
if ( parent & &
( parent - > str ( ) = = " return " | | // MOVED in return statement
parent - > str ( ) = = " ( " ) ) // MOVED in self assignment, isOpenParenthesisMemberFunctionCallOfVarId == true
2016-11-20 15:14:49 +01:00
continue ;
2018-12-02 14:36:01 +01:00
if ( parent & & parent - > astOperand1 ( ) & & parent - > astOperand1 ( ) - > varId ( ) = = varId )
2016-11-20 15:14:49 +01:00
continue ;
const Variable * var = varTok - > variable ( ) ;
if ( ! var )
continue ;
2020-01-11 09:17:32 +01:00
const Token * const endOfVarScope = var - > scope ( ) - > bodyEnd ;
2016-11-20 15:14:49 +01:00
ValueFlow : : Value value ;
2019-07-10 15:27:07 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : MOVED ;
2016-11-20 15:14:49 +01:00
value . moveKind = moveKind ;
2019-07-10 15:27:07 +02:00
if ( moveKind = = ValueFlow : : Value : : MoveKind : : MovedVariable )
2018-04-11 09:44:35 +02:00
value . errorPath . emplace_back ( tok , " Calling std::move( " + varTok - > str ( ) + " ) " ) ;
2017-10-10 15:49:15 +02:00
else // if (moveKind == ValueFlow::Value::ForwardedVariable)
2018-04-11 09:44:35 +02:00
value . errorPath . emplace_back ( tok , " Calling std::forward( " + varTok - > str ( ) + " ) " ) ;
2016-11-20 15:14:49 +01:00
value . setKnown ( ) ;
std : : list < ValueFlow : : Value > values ;
values . push_back ( value ) ;
2016-12-12 21:46:05 +01:00
const Token * openParentesisOfMove = findOpenParentesisOfMove ( varTok ) ;
const Token * endOfFunctionCall = findEndOfFunctionCallForParameter ( openParentesisOfMove ) ;
if ( endOfFunctionCall )
2021-07-18 07:46:31 +02:00
valueFlowForward (
const_cast < Token * > ( endOfFunctionCall ) , endOfVarScope , varTok , values , tokenlist , settings ) ;
2016-11-20 15:14:49 +01:00
}
}
}
2021-01-11 07:56:16 +01:00
static const Token * findIncompleteVar ( const Token * start , const Token * end )
{
for ( const Token * tok = start ; tok ! = end ; tok = tok - > next ( ) ) {
if ( tok - > isIncompleteVar ( ) )
return tok ;
}
return nullptr ;
}
2021-01-23 08:47:39 +01:00
static ValueFlow : : Value makeConditionValue ( long long val , const Token * condTok , bool assume )
{
ValueFlow : : Value v ( val ) ;
v . setKnown ( ) ;
v . condition = condTok ;
if ( assume )
v . errorPath . emplace_back ( condTok , " Assuming condition ' " + condTok - > expressionString ( ) + " ' is true " ) ;
else
v . errorPath . emplace_back ( condTok , " Assuming condition ' " + condTok - > expressionString ( ) + " ' is false " ) ;
return v ;
}
2021-08-03 06:31:52 +02:00
static std : : vector < const Token * > getConditions ( const Token * tok , const char * op )
{
std : : vector < const Token * > conds = { tok } ;
if ( tok - > str ( ) = = op ) {
std : : vector < const Token * > args = astFlatten ( tok , op ) ;
std : : copy_if ( args . begin ( ) , args . end ( ) , std : : back_inserter ( conds ) , [ & ] ( const Token * tok2 ) {
if ( tok2 - > exprId ( ) = = 0 )
return false ;
if ( tok2 - > hasKnownIntValue ( ) )
return false ;
2022-01-21 09:56:24 +01:00
if ( Token : : Match ( tok2 , " %var%|. " ) & & ! astIsBool ( tok2 ) )
2022-01-09 22:43:08 +01:00
return false ;
2021-08-03 06:31:52 +02:00
return true ;
} ) ;
}
return conds ;
}
2021-01-25 22:51:50 +01:00
//
2021-01-23 08:47:39 +01:00
static void valueFlowConditionExpressions ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2021-01-11 07:56:16 +01:00
{
for ( const Scope * scope : symboldatabase - > functionScopes ) {
if ( const Token * incompleteTok = findIncompleteVar ( scope - > bodyStart , scope - > bodyEnd ) ) {
if ( incompleteTok - > isIncompleteVar ( ) ) {
if ( settings - > debugwarnings )
bailoutIncompleteVar ( tokenlist , errorLogger , incompleteTok , " Skipping function due to incomplete variable " + incompleteTok - > str ( ) ) ;
break ;
}
}
for ( const Token * tok = scope - > bodyStart ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : simpleMatch ( tok , " if ( " ) )
continue ;
2021-01-23 08:47:39 +01:00
Token * parenTok = tok - > next ( ) ;
2021-01-11 07:56:16 +01:00
if ( ! Token : : simpleMatch ( parenTok - > link ( ) , " ) { " ) )
continue ;
2021-01-23 08:47:39 +01:00
Token * blockTok = parenTok - > link ( ) - > tokAt ( 1 ) ;
const Token * condTok = parenTok - > astOperand2 ( ) ;
2021-08-03 06:31:52 +02:00
if ( condTok - > exprId ( ) = = 0 )
continue ;
2021-01-23 08:47:39 +01:00
if ( condTok - > hasKnownIntValue ( ) )
2021-01-22 10:51:46 +01:00
continue ;
2021-07-18 07:46:31 +02:00
if ( ! isConstExpression ( condTok , settings - > library , true , tokenlist - > isCPP ( ) ) )
continue ;
2021-06-26 09:16:45 +02:00
const bool is1 = ( condTok - > isComparisonOp ( ) | | condTok - > tokType ( ) = = Token : : eLogicalOp | | astIsBool ( condTok ) ) ;
2021-01-11 07:56:16 +01:00
2021-01-23 08:47:39 +01:00
Token * startTok = blockTok ;
// Inner condition
{
2021-08-03 06:31:52 +02:00
for ( const Token * condTok2 : getConditions ( condTok , " && " ) ) {
2021-06-26 09:16:45 +02:00
if ( is1 ) {
ExpressionAnalyzer a1 ( condTok2 , makeConditionValue ( 1 , condTok2 , true ) , tokenlist ) ;
valueFlowGenericForward ( startTok , startTok - > link ( ) , a1 , settings ) ;
}
2021-01-23 08:47:39 +01:00
OppositeExpressionAnalyzer a2 ( true , condTok2 , makeConditionValue ( 0 , condTok2 , true ) , tokenlist ) ;
valueFlowGenericForward ( startTok , startTok - > link ( ) , a2 , settings ) ;
}
}
2021-08-03 06:31:52 +02:00
std : : vector < const Token * > conds = getConditions ( condTok , " || " ) ;
2021-01-23 08:47:39 +01:00
// Check else block
if ( Token : : simpleMatch ( startTok - > link ( ) , " } else { " ) ) {
startTok = startTok - > link ( ) - > tokAt ( 2 ) ;
2021-01-25 22:51:50 +01:00
for ( const Token * condTok2 : conds ) {
2021-01-23 08:47:39 +01:00
ExpressionAnalyzer a1 ( condTok2 , makeConditionValue ( 0 , condTok2 , false ) , tokenlist ) ;
valueFlowGenericForward ( startTok , startTok - > link ( ) , a1 , settings ) ;
2021-06-26 09:16:45 +02:00
if ( is1 ) {
OppositeExpressionAnalyzer a2 ( true , condTok2 , makeConditionValue ( 1 , condTok2 , false ) , tokenlist ) ;
valueFlowGenericForward ( startTok , startTok - > link ( ) , a2 , settings ) ;
}
2021-01-23 08:47:39 +01:00
}
}
// Check if the block terminates early
if ( isEscapeScope ( blockTok , tokenlist ) ) {
2021-01-25 22:51:50 +01:00
for ( const Token * condTok2 : conds ) {
2021-01-23 08:47:39 +01:00
ExpressionAnalyzer a1 ( condTok2 , makeConditionValue ( 0 , condTok2 , false ) , tokenlist ) ;
valueFlowGenericForward ( startTok - > link ( ) - > next ( ) , scope - > bodyEnd , a1 , settings ) ;
2021-06-26 09:16:45 +02:00
if ( is1 ) {
OppositeExpressionAnalyzer a2 ( true , condTok2 , makeConditionValue ( 1 , condTok2 , false ) , tokenlist ) ;
valueFlowGenericForward ( startTok - > link ( ) - > next ( ) , scope - > bodyEnd , a2 , settings ) ;
}
2021-01-23 08:47:39 +01:00
}
}
2021-01-11 07:56:16 +01:00
}
}
}
2021-08-04 21:07:31 +02:00
static bool isTruncated ( const ValueType * src , const ValueType * dst , const Settings * settings )
{
if ( src - > pointer > 0 | | dst - > pointer > 0 )
return src - > pointer ! = dst - > pointer ;
if ( src - > smartPointer & & dst - > smartPointer )
return false ;
if ( ( src - > isIntegral ( ) & & dst - > isIntegral ( ) ) | | ( src - > isFloat ( ) & & dst - > isFloat ( ) ) ) {
size_t srcSize = ValueFlow : : getSizeOf ( * src , settings ) ;
size_t dstSize = ValueFlow : : getSizeOf ( * dst , settings ) ;
if ( srcSize > dstSize )
return true ;
if ( srcSize = = dstSize & & src - > sign ! = dst - > sign )
return true ;
} else if ( src - > type = = dst - > type ) {
if ( src - > type = = ValueType : : Type : : RECORD )
return src - > typeScope ! = dst - > typeScope ;
} else {
return true ;
}
return false ;
}
2021-08-19 22:01:55 +02:00
static void setSymbolic ( ValueFlow : : Value & value , const Token * tok )
{
2021-10-06 08:39:58 +02:00
assert ( tok & & tok - > exprId ( ) > 0 & & " Missing expr id for symbolic value " ) ;
2021-08-19 22:01:55 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : SYMBOLIC ;
value . tokvalue = tok ;
}
2021-07-30 21:29:35 +02:00
static ValueFlow : : Value makeSymbolic ( const Token * tok , MathLib : : bigint delta = 0 )
{
ValueFlow : : Value value ;
value . setKnown ( ) ;
2021-08-19 22:01:55 +02:00
setSymbolic ( value , tok ) ;
2021-07-30 21:29:35 +02:00
value . intvalue = delta ;
return value ;
}
2021-12-15 19:37:25 +01:00
static std : : set < nonneg int > getVarIds ( const Token * tok )
{
std : : set < nonneg int > result ;
visitAstNodes ( tok , [ & ] ( const Token * child ) {
if ( child - > varId ( ) > 0 )
result . insert ( child - > varId ( ) ) ;
return ChildrenToVisit : : op1_and_op2 ;
} ) ;
return result ;
}
2021-07-30 21:29:35 +02:00
static void valueFlowSymbolic ( TokenList * tokenlist , SymbolDatabase * symboldatabase )
{
for ( const Scope * scope : symboldatabase - > functionScopes ) {
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : simpleMatch ( tok , " = " ) )
continue ;
2021-08-04 21:07:55 +02:00
if ( tok - > astParent ( ) )
continue ;
2021-07-30 21:29:35 +02:00
if ( ! tok - > astOperand1 ( ) )
continue ;
if ( ! tok - > astOperand2 ( ) )
continue ;
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) )
continue ;
if ( tok - > astOperand2 ( ) - > hasKnownIntValue ( ) )
continue ;
2021-08-02 10:49:39 +02:00
if ( tok - > astOperand1 ( ) - > exprId ( ) = = 0 )
continue ;
if ( tok - > astOperand2 ( ) - > exprId ( ) = = 0 )
continue ;
2021-07-31 14:19:37 +02:00
if ( ! isConstExpression ( tok - > astOperand2 ( ) , tokenlist - > getSettings ( ) - > library , true , tokenlist - > isCPP ( ) ) )
continue ;
2021-08-04 21:07:31 +02:00
if ( tok - > astOperand1 ( ) - > valueType ( ) & & tok - > astOperand2 ( ) - > valueType ( ) ) {
if ( isTruncated (
tok - > astOperand2 ( ) - > valueType ( ) , tok - > astOperand1 ( ) - > valueType ( ) , tokenlist - > getSettings ( ) ) )
continue ;
} else if ( isDifferentType ( tok - > astOperand2 ( ) , tok - > astOperand1 ( ) ) ) {
continue ;
}
2021-12-15 19:37:25 +01:00
const std : : set < nonneg int > rhsVarIds = getVarIds ( tok - > astOperand2 ( ) ) ;
2021-10-09 16:20:04 +02:00
const std : : vector < const Variable * > vars = getLHSVariables ( tok ) ;
2021-12-15 19:37:25 +01:00
if ( std : : any_of ( vars . begin ( ) , vars . end ( ) , [ & ] ( const Variable * var ) {
if ( rhsVarIds . count ( var - > declarationId ( ) ) > 0 )
return true ;
2021-10-09 16:20:04 +02:00
if ( var - > isLocal ( ) )
return var - > isStatic ( ) ;
return ! var - > isArgument ( ) ;
} ) )
continue ;
2021-07-30 21:29:35 +02:00
2022-04-28 10:48:37 +02:00
if ( findAstNode ( tok , [ ] ( const Token * child ) {
return child - > isIncompleteVar ( ) ;
} ) )
continue ;
2021-07-30 21:29:35 +02:00
Token * start = nextAfterAstRightmostLeaf ( tok ) ;
2022-04-28 10:48:37 +02:00
const Token * end = getEndOfExprScope ( tok - > astOperand1 ( ) , scope ) ;
2021-07-30 21:29:35 +02:00
ValueFlow : : Value rhs = makeSymbolic ( tok - > astOperand2 ( ) ) ;
rhs . errorPath . emplace_back ( tok ,
tok - > astOperand1 ( ) - > expressionString ( ) + " is assigned ' " +
2021-08-01 14:05:30 +02:00
tok - > astOperand2 ( ) - > expressionString ( ) + " ' here. " ) ;
2021-07-30 21:29:35 +02:00
valueFlowForward ( start , end , tok - > astOperand1 ( ) , { rhs } , tokenlist , tokenlist - > getSettings ( ) ) ;
ValueFlow : : Value lhs = makeSymbolic ( tok - > astOperand1 ( ) ) ;
lhs . errorPath . emplace_back ( tok ,
tok - > astOperand1 ( ) - > expressionString ( ) + " is assigned ' " +
2021-08-01 14:05:30 +02:00
tok - > astOperand2 ( ) - > expressionString ( ) + " ' here. " ) ;
2021-07-30 21:29:35 +02:00
valueFlowForward ( start , end , tok - > astOperand2 ( ) , { lhs } , tokenlist , tokenlist - > getSettings ( ) ) ;
}
}
}
2021-10-14 16:46:36 +02:00
static void valueFlowSymbolicIdentity ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > hasKnownIntValue ( ) )
continue ;
if ( ! Token : : Match ( tok , " *|/|<<|>>|^|+|-|%or% " ) )
continue ;
if ( ! tok - > astOperand1 ( ) )
continue ;
if ( ! tok - > astOperand2 ( ) )
continue ;
if ( ! astIsIntegral ( tok - > astOperand1 ( ) , false ) & & ! astIsIntegral ( tok - > astOperand2 ( ) , false ) )
continue ;
const ValueFlow : : Value * constant = nullptr ;
const Token * vartok = nullptr ;
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) ) {
constant = & tok - > astOperand1 ( ) - > values ( ) . front ( ) ;
vartok = tok - > astOperand2 ( ) ;
}
if ( tok - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
constant = & tok - > astOperand2 ( ) - > values ( ) . front ( ) ;
vartok = tok - > astOperand1 ( ) ;
}
if ( ! constant )
continue ;
if ( ! vartok )
continue ;
if ( vartok - > exprId ( ) = = 0 )
continue ;
if ( Token : : Match ( tok , " <<|>>|/ " ) & & ! astIsLHS ( vartok ) )
continue ;
if ( Token : : Match ( tok , " <<|>>|^|+|-|%or% " ) & & constant - > intvalue ! = 0 )
continue ;
if ( Token : : Match ( tok , " *|/ " ) & & constant - > intvalue ! = 1 )
continue ;
std : : vector < ValueFlow : : Value > values = { makeSymbolic ( vartok ) } ;
std : : unordered_set < nonneg int > ids = { vartok - > exprId ( ) } ;
std : : copy_if (
vartok - > values ( ) . begin ( ) , vartok - > values ( ) . end ( ) , std : : back_inserter ( values ) , [ & ] ( const ValueFlow : : Value & v ) {
if ( ! v . isSymbolicValue ( ) )
return false ;
if ( ! v . tokvalue )
return false ;
return ids . insert ( v . tokvalue - > exprId ( ) ) . second ;
} ) ;
for ( const ValueFlow : : Value & v : values )
setTokenValue ( tok , v , tokenlist - > getSettings ( ) ) ;
}
}
2021-08-03 06:31:28 +02:00
static void valueFlowSymbolicAbs ( TokenList * tokenlist , SymbolDatabase * symboldatabase )
{
for ( const Scope * scope : symboldatabase - > functionScopes ) {
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : Match ( tok , " abs|labs|llabs|fabs|fabsf|fabsl ( " ) )
continue ;
if ( tok - > hasKnownIntValue ( ) )
continue ;
const Token * arg = tok - > next ( ) - > astOperand2 ( ) ;
if ( ! arg )
continue ;
ValueFlow : : Value c = inferCondition ( " >= " , arg , 0 ) ;
if ( ! c . isKnown ( ) )
continue ;
ValueFlow : : Value v = makeSymbolic ( arg ) ;
v . errorPath = c . errorPath ;
v . errorPath . emplace_back ( tok , " Passed to " + tok - > str ( ) ) ;
if ( c . intvalue = = 0 )
v . setImpossible ( ) ;
else
v . setKnown ( ) ;
setTokenValue ( tok - > next ( ) , v , tokenlist - > getSettings ( ) ) ;
}
}
}
2021-08-19 22:01:55 +02:00
struct SymbolicInferModel : InferModel {
const Token * expr ;
explicit SymbolicInferModel ( const Token * tok ) : expr ( tok ) {
assert ( expr - > exprId ( ) ! = 0 ) ;
}
2022-02-10 23:02:24 +01:00
virtual bool match ( const ValueFlow : : Value & value ) const override
2021-08-19 22:01:55 +02:00
{
return value . isSymbolicValue ( ) & & value . tokvalue & & value . tokvalue - > exprId ( ) = = expr - > exprId ( ) ;
}
2022-02-10 23:02:24 +01:00
virtual ValueFlow : : Value yield ( MathLib : : bigint value ) const override
2021-08-19 22:01:55 +02:00
{
ValueFlow : : Value result ( value ) ;
result . valueType = ValueFlow : : Value : : ValueType : : SYMBOLIC ;
result . tokvalue = expr ;
result . setKnown ( ) ;
return result ;
}
} ;
2021-07-30 21:29:35 +02:00
static void valueFlowSymbolicInfer ( TokenList * tokenlist , SymbolDatabase * symboldatabase )
{
for ( const Scope * scope : symboldatabase - > functionScopes ) {
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : Match ( tok , " -|%comp% " ) )
continue ;
if ( tok - > hasKnownIntValue ( ) )
continue ;
if ( ! tok - > astOperand1 ( ) )
continue ;
if ( ! tok - > astOperand2 ( ) )
continue ;
2021-08-19 22:01:55 +02:00
if ( tok - > astOperand1 ( ) - > exprId ( ) = = 0 )
continue ;
if ( tok - > astOperand2 ( ) - > exprId ( ) = = 0 )
continue ;
2021-07-30 21:29:35 +02:00
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) )
continue ;
if ( tok - > astOperand2 ( ) - > hasKnownIntValue ( ) )
continue ;
2021-08-02 10:49:39 +02:00
if ( astIsFloat ( tok - > astOperand1 ( ) , false ) )
continue ;
if ( astIsFloat ( tok - > astOperand2 ( ) , false ) )
continue ;
2021-07-30 21:29:35 +02:00
2021-08-19 22:01:55 +02:00
SymbolicInferModel leftModel { tok - > astOperand1 ( ) } ;
std : : vector < ValueFlow : : Value > values = infer ( leftModel , tok - > str ( ) , 0 , tok - > astOperand2 ( ) - > values ( ) ) ;
if ( values . empty ( ) ) {
SymbolicInferModel rightModel { tok - > astOperand2 ( ) } ;
values = infer ( rightModel , tok - > str ( ) , tok - > astOperand1 ( ) - > values ( ) , 0 ) ;
2021-07-30 21:29:35 +02:00
}
2021-08-19 22:01:55 +02:00
for ( const ValueFlow : : Value & value : values ) {
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2021-08-03 06:31:28 +02:00
}
2021-07-30 21:29:35 +02:00
}
}
}
2022-04-06 06:25:28 +02:00
template < class ContainerOfValue >
2021-12-06 09:16:42 +01:00
static void valueFlowForwardConst ( Token * start ,
const Token * end ,
const Variable * var ,
2022-04-06 06:25:28 +02:00
const ContainerOfValue & values ,
2021-12-06 09:16:42 +01:00
const Settings * const settings )
{
for ( Token * tok = start ; tok ! = end ; tok = tok - > next ( ) ) {
if ( tok - > varId ( ) = = var - > declarationId ( ) ) {
for ( const ValueFlow : : Value & value : values )
setTokenValue ( tok , value , settings ) ;
} else {
[ & ] {
// Follow references
std : : vector < ReferenceToken > refs = followAllReferences ( tok ) ;
for ( const ReferenceToken & ref : refs ) {
if ( ref . token - > varId ( ) = = var - > declarationId ( ) ) {
for ( ValueFlow : : Value value : values ) {
2022-04-06 06:25:28 +02:00
if ( refs . size ( ) > 1 )
value . setInconclusive ( ) ;
2021-12-06 09:16:42 +01:00
value . errorPath . insert ( value . errorPath . end ( ) , ref . errors . begin ( ) , ref . errors . end ( ) ) ;
setTokenValue ( tok , value , settings ) ;
}
return ;
}
}
// Follow symbolic vaues
for ( const ValueFlow : : Value & v : tok - > values ( ) ) {
if ( ! v . isSymbolicValue ( ) )
continue ;
if ( ! v . tokvalue )
continue ;
if ( v . tokvalue - > varId ( ) ! = var - > declarationId ( ) )
continue ;
for ( ValueFlow : : Value value : values ) {
if ( v . intvalue ! = 0 ) {
if ( ! value . isIntValue ( ) )
continue ;
value . intvalue + = v . intvalue ;
}
value . valueKind = v . valueKind ;
value . bound = v . bound ;
value . errorPath . insert ( value . errorPath . end ( ) , v . errorPath . begin ( ) , v . errorPath . end ( ) ) ;
setTokenValue ( tok , value , settings ) ;
}
}
} ( ) ;
}
}
}
2021-01-25 17:24:36 +01:00
static void valueFlowForwardAssign ( Token * const tok ,
const Token * expr ,
std : : vector < const Variable * > vars ,
2019-04-26 11:30:35 +02:00
std : : list < ValueFlow : : Value > values ,
2021-01-25 17:24:36 +01:00
const bool init ,
TokenList * const tokenlist ,
ErrorLogger * const errorLogger ,
const Settings * const settings )
2019-04-26 11:30:09 +02:00
{
2021-08-15 07:43:50 +02:00
if ( Token : : simpleMatch ( tok - > astParent ( ) , " return " ) )
return ;
2022-02-24 06:50:34 +01:00
const Token * endOfVarScope = getEndOfExprScope ( expr ) ;
2019-04-26 11:30:09 +02:00
if ( std : : any_of ( values . begin ( ) , values . end ( ) , std : : mem_fn ( & ValueFlow : : Value : : isLifetimeValue ) ) ) {
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
values . remove_if ( std : : mem_fn ( & ValueFlow : : Value : : isLifetimeValue ) ) ;
}
2021-01-25 17:24:36 +01:00
if ( std : : all_of (
2021-08-07 20:51:18 +02:00
vars . begin ( ) , vars . end ( ) , [ & ] ( const Variable * var ) {
return ! var - > isPointer ( ) & & ! var - > isSmartPointer ( ) ;
2021-01-25 22:51:50 +01:00
} ) )
2021-08-07 20:51:18 +02:00
values . remove_if ( std : : mem_fn ( & ValueFlow : : Value : : isTokValue ) ) ;
2019-04-26 11:30:09 +02:00
if ( tok - > astParent ( ) ) {
2020-09-21 19:30:47 +02:00
for ( ValueFlow : : Value & value : values ) {
2021-06-24 17:10:06 +02:00
std : : string valueKind ;
if ( value . valueKind = = ValueFlow : : Value : : ValueKind : : Impossible ) {
if ( value . bound = = ValueFlow : : Value : : Bound : : Point )
valueKind = " never " ;
else if ( value . bound = = ValueFlow : : Value : : Bound : : Lower )
valueKind = " less than " ;
else if ( value . bound = = ValueFlow : : Value : : Bound : : Upper )
valueKind = " greater than " ;
}
const std : : string info = " Assignment ' " + tok - > astParent ( ) - > expressionString ( ) + " ', assigned value is " + valueKind + value . infoString ( ) ;
2020-09-21 19:30:47 +02:00
value . errorPath . emplace_back ( tok , info ) ;
2019-04-26 11:30:35 +02:00
}
2019-04-26 11:30:09 +02:00
}
2021-01-25 17:24:36 +01:00
if ( tokenlist - > isCPP ( ) & & vars . size ( ) = = 1 & & Token : : Match ( vars . front ( ) - > typeStartToken ( ) , " bool|_Bool " ) ) {
2021-08-25 04:51:54 +02:00
for ( ValueFlow : : Value & value : values ) {
if ( value . isImpossible ( ) )
continue ;
if ( value . isIntValue ( ) )
value . intvalue = ( value . intvalue ! = 0 ) ;
if ( value . isTokValue ( ) )
value . intvalue = ( value . tokvalue ! = nullptr ) ;
2019-04-26 11:30:09 +02:00
}
}
// Static variable initialisation?
2021-01-25 17:24:36 +01:00
if ( vars . size ( ) = = 1 & & vars . front ( ) - > isStatic ( ) & & init )
2019-09-20 15:06:37 +02:00
lowerToPossible ( values ) ;
2019-04-26 11:30:09 +02:00
2022-01-01 23:15:14 +01:00
// is volatile
if ( std : : any_of ( vars . begin ( ) , vars . end ( ) , [ & ] ( const Variable * var ) {
return var - > isVolatile ( ) ;
} ) )
lowerToPossible ( values ) ;
2019-04-26 11:30:09 +02:00
// Skip RHS
const Token * nextExpression = tok - > astParent ( ) ? nextAfterAstRightmostLeaf ( tok - > astParent ( ) ) : tok - > next ( ) ;
2022-03-02 21:12:37 +01:00
if ( ! nextExpression )
return ;
2019-04-26 11:30:09 +02:00
2021-07-30 21:29:35 +02:00
for ( ValueFlow : : Value & value : values ) {
if ( value . isSymbolicValue ( ) )
continue ;
if ( value . isTokValue ( ) )
continue ;
2019-09-03 17:15:58 +02:00
value . tokvalue = tok ;
2021-07-30 21:29:35 +02:00
}
2021-12-06 09:16:42 +01:00
// Const variable
if ( expr - > variable ( ) & & expr - > variable ( ) - > isConst ( ) & & ! expr - > variable ( ) - > isReference ( ) ) {
auto it = std : : remove_if ( values . begin ( ) , values . end ( ) , [ ] ( const ValueFlow : : Value & value ) {
if ( ! value . isKnown ( ) )
return false ;
if ( value . isIntValue ( ) )
return true ;
if ( value . isFloatValue ( ) )
return true ;
if ( value . isContainerSizeValue ( ) )
return true ;
if ( value . isIteratorValue ( ) )
return true ;
return false ;
} ) ;
std : : list < ValueFlow : : Value > constValues ;
constValues . splice ( constValues . end ( ) , values , it , values . end ( ) ) ;
valueFlowForwardConst ( const_cast < Token * > ( nextExpression ) , endOfVarScope , expr - > variable ( ) , constValues , settings ) ;
}
2021-01-25 17:24:36 +01:00
valueFlowForward ( const_cast < Token * > ( nextExpression ) , endOfVarScope , expr , values , tokenlist , settings ) ;
}
static void valueFlowForwardAssign ( Token * const tok ,
const Variable * const var ,
const std : : list < ValueFlow : : Value > & values ,
const bool ,
const bool init ,
TokenList * const tokenlist ,
ErrorLogger * const errorLogger ,
const Settings * const settings )
{
valueFlowForwardAssign ( tok , var - > nameToken ( ) , { var } , values , init , tokenlist , errorLogger , settings ) ;
2019-04-26 11:30:09 +02:00
}
2021-06-26 09:16:04 +02:00
static std : : list < ValueFlow : : Value > truncateValues ( std : : list < ValueFlow : : Value > values ,
2021-08-07 20:51:18 +02:00
const ValueType * dst ,
const ValueType * src ,
const Settings * settings )
2019-05-01 17:05:16 +02:00
{
2021-06-26 09:16:04 +02:00
if ( ! dst | | ! dst - > isIntegral ( ) )
2019-05-01 17:05:16 +02:00
return values ;
2021-06-26 09:16:04 +02:00
const size_t sz = ValueFlow : : getSizeOf ( * dst , settings ) ;
if ( src ) {
const size_t osz = ValueFlow : : getSizeOf ( * src , settings ) ;
if ( osz > = sz & & dst - > sign = = ValueType : : Sign : : SIGNED & & src - > sign = = ValueType : : Sign : : UNSIGNED ) {
values . remove_if ( [ & ] ( const ValueFlow : : Value & value ) {
if ( ! value . isIntValue ( ) )
return false ;
if ( ! value . isImpossible ( ) )
return false ;
if ( value . bound ! = ValueFlow : : Value : : Bound : : Upper )
return false ;
if ( osz = = sz & & value . intvalue < 0 )
return true ;
if ( osz > sz )
return true ;
return false ;
} ) ;
}
}
2019-05-01 17:05:16 +02:00
for ( ValueFlow : : Value & value : values ) {
2021-05-19 11:49:19 +02:00
// Don't truncate impossible values since those can be outside of the valid range
2021-03-30 14:02:28 +02:00
if ( value . isImpossible ( ) )
continue ;
2019-05-01 17:05:16 +02:00
if ( value . isFloatValue ( ) ) {
value . intvalue = value . floatValue ;
2021-01-02 09:30:00 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : INT ;
2019-05-01 17:05:16 +02:00
}
if ( value . isIntValue ( ) & & sz > 0 & & sz < 8 ) {
const MathLib : : biguint unsignedMaxValue = ( 1ULL < < ( sz * 8 ) ) - 1ULL ;
const MathLib : : biguint signBit = 1ULL < < ( sz * 8 - 1 ) ;
value . intvalue & = unsignedMaxValue ;
2021-06-26 09:16:04 +02:00
if ( dst - > sign = = ValueType : : Sign : : SIGNED & & ( value . intvalue & signBit ) )
2019-05-01 17:05:16 +02:00
value . intvalue | = ~ unsignedMaxValue ;
}
}
return values ;
}
2020-06-14 21:14:05 +02:00
static bool isVariableInit ( const Token * tok )
{
2021-10-15 20:05:42 +02:00
return ( tok - > str ( ) = = " ( " | | tok - > str ( ) = = " { " ) & &
2020-06-14 21:14:05 +02:00
tok - > isBinaryOp ( ) & &
tok - > astOperand1 ( ) - > variable ( ) & &
tok - > astOperand1 ( ) - > variable ( ) - > nameToken ( ) = = tok - > astOperand1 ( ) & &
tok - > astOperand1 ( ) - > variable ( ) - > valueType ( ) & &
tok - > astOperand1 ( ) - > variable ( ) - > valueType ( ) - > type > = ValueType : : Type : : VOID & &
! Token : : simpleMatch ( tok - > astOperand2 ( ) , " , " ) ;
}
2022-02-11 06:52:28 +01:00
// Return true if two associative containers intersect
template < class C1 , class C2 >
static bool intersects ( const C1 & c1 , const C2 & c2 )
{
if ( c1 . size ( ) > c2 . size ( ) )
return intersects ( c2 , c1 ) ;
for ( auto & & x : c1 ) {
if ( c2 . find ( x ) ! = c2 . end ( ) )
return true ;
}
return false ;
}
2015-02-01 12:10:20 +01:00
static void valueFlowAfterAssign ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-01-21 21:13:49 +01:00
{
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2022-02-11 06:52:28 +01:00
std : : unordered_map < nonneg int , std : : unordered_set < nonneg int > > backAssigns ;
2018-04-27 22:36:30 +02:00
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2015-02-01 12:10:20 +01:00
// Assignment
2020-06-14 21:14:05 +02:00
if ( ( tok - > str ( ) ! = " = " & & ! isVariableInit ( tok ) ) | | ( tok - > astParent ( ) ) )
2015-02-01 12:10:20 +01:00
continue ;
2014-01-21 21:13:49 +01:00
2015-02-01 12:10:20 +01:00
// Lhs should be a variable
2021-01-25 17:24:36 +01:00
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand1 ( ) - > exprId ( ) )
2016-11-21 17:26:36 +01:00
continue ;
2021-01-25 17:24:36 +01:00
std : : vector < const Variable * > vars = getLHSVariables ( tok ) ;
2014-07-15 10:36:13 +02:00
2015-02-01 12:10:20 +01:00
// Rhs values..
2017-03-27 18:48:34 +02:00
if ( ! tok - > astOperand2 ( ) | | tok - > astOperand2 ( ) - > values ( ) . empty ( ) )
2015-02-01 12:10:20 +01:00
continue ;
2014-01-21 21:13:49 +01:00
2021-06-26 09:16:04 +02:00
std : : list < ValueFlow : : Value > values = truncateValues (
2021-08-07 20:51:18 +02:00
tok - > astOperand2 ( ) - > values ( ) , tok - > astOperand1 ( ) - > valueType ( ) , tok - > astOperand2 ( ) - > valueType ( ) , settings ) ;
2020-08-26 21:05:17 +02:00
// Remove known values
std : : set < ValueFlow : : Value : : ValueType > types ;
if ( tok - > astOperand1 ( ) - > hasKnownValue ( ) ) {
2020-08-28 19:29:33 +02:00
for ( const ValueFlow : : Value & value : tok - > astOperand1 ( ) - > values ( ) ) {
2021-07-30 21:29:35 +02:00
if ( value . isKnown ( ) & & ! value . isSymbolicValue ( ) )
2020-08-26 21:05:17 +02:00
types . insert ( value . valueType ) ;
}
}
2020-08-28 19:29:33 +02:00
values . remove_if ( [ & ] ( const ValueFlow : : Value & value ) {
return types . count ( value . valueType ) > 0 ;
} ) ;
2020-08-26 21:05:17 +02:00
// Remove container size if its not a container
if ( ! astIsContainer ( tok - > astOperand2 ( ) ) )
2020-08-28 19:29:33 +02:00
values . remove_if ( [ & ] ( const ValueFlow : : Value & value ) {
2021-08-07 20:51:18 +02:00
return value . valueType = = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
} ) ;
2021-08-04 21:07:31 +02:00
// Remove symbolic values that are the same as the LHS
values . remove_if ( [ & ] ( const ValueFlow : : Value & value ) {
if ( value . isSymbolicValue ( ) & & value . tokvalue )
return value . tokvalue - > exprId ( ) = = tok - > astOperand1 ( ) - > exprId ( ) ;
return false ;
} ) ;
2021-07-01 22:08:00 +02:00
// If assignment copy by value, remove Uninit values..
if ( ( tok - > astOperand1 ( ) - > valueType ( ) & & tok - > astOperand1 ( ) - > valueType ( ) - > pointer = = 0 ) | |
( tok - > astOperand1 ( ) - > variable ( ) & & tok - > astOperand1 ( ) - > variable ( ) - > isReference ( ) & & tok - > astOperand1 ( ) - > variable ( ) - > nameToken ( ) = = tok - > astOperand1 ( ) ) )
values . remove_if ( [ & ] ( const ValueFlow : : Value & value ) {
2021-08-07 20:51:18 +02:00
return value . isUninitValue ( ) ;
} ) ;
2020-08-26 21:05:17 +02:00
if ( values . empty ( ) )
continue ;
2022-03-22 07:24:40 +01:00
const bool init = vars . size ( ) = = 1 & & ( vars . front ( ) - > nameToken ( ) = = tok - > astOperand1 ( ) | | tok - > isSplittedVarDeclEq ( ) ) ;
2021-01-25 17:24:36 +01:00
valueFlowForwardAssign (
tok - > astOperand2 ( ) , tok - > astOperand1 ( ) , vars , values , init , tokenlist , errorLogger , settings ) ;
2021-10-06 08:39:58 +02:00
// Back propagate symbolic values
if ( tok - > astOperand1 ( ) - > exprId ( ) > 0 ) {
Token * start = nextAfterAstRightmostLeaf ( tok ) ;
const Token * end = scope - > bodyEnd ;
2022-02-11 06:52:28 +01:00
// Collect symbolic ids
std : : unordered_set < nonneg int > ids ;
for ( const ValueFlow : : Value & value : values ) {
if ( ! value . isSymbolicValue ( ) )
continue ;
if ( ! value . tokvalue )
continue ;
if ( value . tokvalue - > exprId ( ) = = 0 )
continue ;
ids . insert ( value . tokvalue - > exprId ( ) ) ;
}
2021-10-06 08:39:58 +02:00
for ( ValueFlow : : Value value : values ) {
if ( ! value . isSymbolicValue ( ) )
continue ;
const Token * expr = value . tokvalue ;
value . intvalue = - value . intvalue ;
value . tokvalue = tok - > astOperand1 ( ) ;
2022-02-11 06:52:28 +01:00
// Skip if it intersects with an already assigned symbol
auto & s = backAssigns [ value . tokvalue - > exprId ( ) ] ;
if ( intersects ( s , ids ) )
continue ;
s . insert ( expr - > exprId ( ) ) ;
2021-10-06 08:39:58 +02:00
value . errorPath . emplace_back ( tok ,
tok - > astOperand1 ( ) - > expressionString ( ) + " is assigned ' " +
tok - > astOperand2 ( ) - > expressionString ( ) + " ' here. " ) ;
valueFlowForward ( start , end , expr , { value } , tokenlist , settings ) ;
}
}
2015-02-01 12:10:20 +01:00
}
2014-06-15 16:47:01 +02:00
}
}
2014-02-22 12:09:54 +01:00
2021-10-15 10:58:16 +02:00
static std : : vector < const Variable * > getVariables ( const Token * tok )
{
std : : vector < const Variable * > result ;
visitAstNodes ( tok , [ & ] ( const Token * child ) {
if ( child - > variable ( ) )
result . push_back ( child - > variable ( ) ) ;
return ChildrenToVisit : : op1_and_op2 ;
} ) ;
return result ;
}
static void valueFlowAfterSwap ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * errorLogger ,
const Settings * settings )
{
for ( const Scope * scope : symboldatabase - > functionScopes ) {
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : simpleMatch ( tok , " swap ( " ) )
continue ;
if ( ! Token : : simpleMatch ( tok - > next ( ) - > astOperand2 ( ) , " , " ) )
continue ;
std : : vector < Token * > args = astFlatten ( tok - > next ( ) - > astOperand2 ( ) , " , " ) ;
if ( args . size ( ) ! = 2 )
continue ;
2022-01-02 08:09:23 +01:00
if ( args [ 0 ] - > exprId ( ) = = 0 )
continue ;
if ( args [ 1 ] - > exprId ( ) = = 0 )
continue ;
2021-10-15 10:58:16 +02:00
for ( int i = 0 ; i < 2 ; i + + ) {
std : : vector < const Variable * > vars = getVariables ( args [ 0 ] ) ;
std : : list < ValueFlow : : Value > values = args [ 0 ] - > values ( ) ;
valueFlowForwardAssign ( args [ 0 ] , args [ 1 ] , vars , values , false , tokenlist , errorLogger , settings ) ;
std : : swap ( args [ 0 ] , args [ 1 ] ) ;
}
}
}
}
2019-09-20 15:06:37 +02:00
static void valueFlowSetConditionToKnown ( const Token * tok , std : : list < ValueFlow : : Value > & values , bool then )
{
if ( values . empty ( ) )
return ;
if ( then & & ! Token : : Match ( tok , " ==|!|( " ) )
return ;
if ( ! then & & ! Token : : Match ( tok , " !=|%var%|( " ) )
return ;
if ( isConditionKnown ( tok , then ) )
changePossibleToKnown ( values ) ;
2019-08-08 07:46:47 +02:00
}
static bool isBreakScope ( const Token * const endToken )
{
if ( ! Token : : simpleMatch ( endToken , " } " ) )
return false ;
if ( ! Token : : simpleMatch ( endToken - > link ( ) , " { " ) )
return false ;
return Token : : findmatch ( endToken - > link ( ) , " break|goto " , endToken ) ;
}
2022-03-12 06:15:35 +01:00
ValueFlow : : Value asImpossible ( ValueFlow : : Value v )
2019-09-20 15:06:37 +02:00
{
v . invertRange ( ) ;
v . setImpossible ( ) ;
return v ;
}
2019-09-28 20:50:56 +02:00
static void insertImpossible ( std : : list < ValueFlow : : Value > & values , const std : : list < ValueFlow : : Value > & input )
2019-09-20 15:06:37 +02:00
{
std : : transform ( input . begin ( ) , input . end ( ) , std : : back_inserter ( values ) , & asImpossible ) ;
}
2020-08-25 07:12:41 +02:00
static void insertNegateKnown ( std : : list < ValueFlow : : Value > & values , const std : : list < ValueFlow : : Value > & input )
{
2020-08-26 10:15:09 +02:00
for ( ValueFlow : : Value value : input ) {
2020-08-25 07:12:41 +02:00
if ( ! value . isIntValue ( ) & & ! value . isContainerSizeValue ( ) )
continue ;
value . intvalue = ! value . intvalue ;
value . setKnown ( ) ;
values . push_back ( value ) ;
}
}
2020-12-24 20:07:46 +01:00
struct ConditionHandler {
2018-11-24 10:07:12 +01:00
struct Condition {
const Token * vartok ;
std : : list < ValueFlow : : Value > true_values ;
std : : list < ValueFlow : : Value > false_values ;
2020-08-25 07:12:41 +02:00
bool inverted = false ;
2021-08-14 06:48:38 +02:00
// Whether to insert impossible values for the condition or only use possible values
bool impossible = true ;
2015-02-01 12:10:20 +01:00
2022-04-07 06:48:13 +02:00
bool isBool ( ) const {
return astIsBool ( vartok ) ;
}
2021-08-14 06:48:38 +02:00
Condition ( ) : vartok ( nullptr ) , true_values ( ) , false_values ( ) , inverted ( false ) , impossible ( true ) { }
2018-11-24 10:07:12 +01:00
} ;
2020-12-24 20:07:46 +01:00
2021-06-19 13:59:48 +02:00
virtual Analyzer : : Result forward ( Token * start ,
const Token * stop ,
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
const Settings * settings ) const = 0 ;
2020-12-24 20:07:46 +01:00
2021-08-01 10:13:14 +02:00
virtual Analyzer : : Result forward ( Token * top ,
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
const Settings * settings ) const = 0 ;
2021-01-08 22:55:04 +01:00
virtual void reverse ( Token * start ,
2021-01-23 17:52:01 +01:00
const Token * endToken ,
2021-01-08 22:55:04 +01:00
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
const Settings * settings ) const = 0 ;
2021-07-26 22:23:19 +02:00
virtual std : : vector < Condition > parse ( const Token * tok , const Settings * settings ) const = 0 ;
2020-12-24 20:07:46 +01:00
2021-08-11 08:37:37 +02:00
void traverseCondition ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
const std : : function < void ( const Condition & cond , Token * tok , const Scope * scope ) > & f ) const
{
2018-11-24 10:07:12 +01:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2019-09-30 21:04:43 +02:00
if ( Token : : Match ( tok , " if|while|for ( " ) )
continue ;
2021-11-25 22:34:00 +01:00
if ( Token : : Match ( tok , " :|;|, " ) )
continue ;
2019-09-30 21:04:43 +02:00
2019-09-26 10:32:25 +02:00
const Token * top = tok - > astTop ( ) ;
if ( ! top )
continue ;
2022-02-25 05:53:51 +01:00
if ( ! Token : : Match ( top - > previous ( ) , " if|while|for ( " ) & & ! Token : : Match ( tok - > astParent ( ) , " &&|%oror%|?|! " ) )
2019-09-26 10:32:25 +02:00
continue ;
2021-08-11 08:37:37 +02:00
for ( const Condition & cond : parse ( tok , tokenlist - > getSettings ( ) ) ) {
2021-07-26 22:23:19 +02:00
if ( ! cond . vartok )
continue ;
2021-08-03 06:31:52 +02:00
if ( cond . vartok - > exprId ( ) = = 0 )
continue ;
2021-07-26 22:23:19 +02:00
if ( cond . vartok - > hasKnownIntValue ( ) )
continue ;
if ( cond . true_values . empty ( ) | | cond . false_values . empty ( ) )
continue ;
2021-08-11 08:37:37 +02:00
if ( ! isConstExpression ( cond . vartok , tokenlist - > getSettings ( ) - > library , true , tokenlist - > isCPP ( ) ) )
2021-07-26 22:23:19 +02:00
continue ;
2021-08-11 08:37:37 +02:00
f ( cond , tok , scope ) ;
2019-10-01 08:39:08 +02:00
}
2020-12-24 20:07:46 +01:00
}
}
}
2021-01-08 22:55:04 +01:00
void beforeCondition ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * errorLogger ,
2021-01-10 14:46:19 +01:00
const Settings * settings ) const {
2021-08-11 08:37:37 +02:00
traverseCondition ( tokenlist , symboldatabase , [ & ] ( const Condition & cond , Token * tok , const Scope * ) {
2021-01-10 14:46:19 +01:00
if ( cond . vartok - > exprId ( ) = = 0 )
return ;
2018-04-05 06:51:31 +02:00
2021-01-12 20:48:25 +01:00
// If condition is known then don't propagate value
2021-01-10 14:46:19 +01:00
if ( tok - > hasKnownIntValue ( ) )
return ;
2020-08-31 08:46:56 +02:00
2021-01-10 14:46:19 +01:00
const Token * top = tok - > astTop ( ) ;
2020-08-31 08:46:56 +02:00
2021-01-10 14:46:19 +01:00
if ( Token : : Match ( top , " %assign% " ) )
return ;
2021-01-23 17:52:01 +01:00
if ( Token : : Match ( cond . vartok - > astParent ( ) , " %assign%|++|-- " ) )
return ;
2020-08-31 08:46:56 +02:00
2021-01-10 14:46:19 +01:00
if ( Token : : simpleMatch ( tok - > astParent ( ) , " ? " ) & & tok - > astParent ( ) - > isExpandedMacro ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok ,
" variable ' " + cond . vartok - > expressionString ( ) + " ', condition is defined in macro " ) ;
return ;
}
2021-01-08 22:55:04 +01:00
2021-01-10 14:46:19 +01:00
// if,macro => bailout
if ( Token : : simpleMatch ( top - > previous ( ) , " if ( " ) & & top - > previous ( ) - > isExpandedMacro ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok ,
" variable ' " + cond . vartok - > expressionString ( ) + " ', condition is defined in macro " ) ;
return ;
}
2020-12-25 08:58:12 +01:00
2021-01-23 17:52:01 +01:00
std : : list < ValueFlow : : Value > values = cond . true_values ;
if ( cond . true_values ! = cond . false_values )
values . insert ( values . end ( ) , cond . false_values . begin ( ) , cond . false_values . end ( ) ) ;
// extra logic for unsigned variables 'i>=1' => possible value can also be 0
if ( Token : : Match ( tok , " <|> " ) ) {
values . remove_if ( [ ] ( const ValueFlow : : Value & v ) {
if ( v . isIntValue ( ) )
return v . intvalue ! = 0 ;
return false ;
} ) ;
if ( cond . vartok - > valueType ( ) & & cond . vartok - > valueType ( ) - > sign ! = ValueType : : Sign : : UNSIGNED )
return ;
}
if ( values . empty ( ) )
return ;
2021-01-10 14:46:19 +01:00
// bailout: for/while-condition, variable is changed in while loop
if ( Token : : Match ( top - > previous ( ) , " for|while ( " ) & & Token : : simpleMatch ( top - > link ( ) , " ) { " ) ) {
2021-01-08 22:55:04 +01:00
2021-01-10 14:46:19 +01:00
// Variable changed in 3rd for-expression
if ( Token : : simpleMatch ( top - > previous ( ) , " for ( " ) ) {
if ( top - > astOperand2 ( ) & & top - > astOperand2 ( ) - > astOperand2 ( ) & &
isExpressionChanged (
cond . vartok , top - > astOperand2 ( ) - > astOperand2 ( ) , top - > link ( ) , settings , tokenlist - > isCPP ( ) ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok ,
" variable ' " + cond . vartok - > expressionString ( ) + " ' used in loop " ) ;
return ;
2014-06-19 17:29:41 +02:00
}
2021-01-10 14:46:19 +01:00
}
2021-01-08 22:55:04 +01:00
2021-01-10 14:46:19 +01:00
// Variable changed in loop code
2021-01-23 17:52:01 +01:00
const Token * const start = top ;
const Token * const block = top - > link ( ) - > next ( ) ;
const Token * const end = block - > link ( ) ;
if ( isExpressionChanged ( cond . vartok , start , end , settings , tokenlist - > isCPP ( ) ) ) {
// If its reassigned in loop then analyze from the end
if ( ! Token : : Match ( tok , " %assign%|++|-- " ) & &
2021-08-07 20:51:18 +02:00
findExpression ( cond . vartok - > exprId ( ) , start , end , [ & ] ( const Token * tok2 ) {
return Token : : Match ( tok2 - > astParent ( ) , " %assign% " ) & & astIsLHS ( tok2 ) ;
2021-01-25 22:51:50 +01:00
} ) ) {
2021-01-23 17:52:01 +01:00
// Start at the end of the loop body
Token * bodyTok = top - > link ( ) - > next ( ) ;
reverse ( bodyTok - > link ( ) , bodyTok , cond . vartok , values , tokenlist , settings ) ;
2020-12-25 08:58:12 +01:00
}
2021-01-23 17:52:01 +01:00
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok ,
" variable ' " + cond . vartok - > expressionString ( ) + " ' used in loop " ) ;
return ;
2014-06-19 17:29:41 +02:00
}
2021-01-10 14:46:19 +01:00
}
2014-07-07 17:48:58 +02:00
2021-02-09 15:27:46 +01:00
Token * startTok = nullptr ;
if ( astIsRHS ( tok ) )
startTok = tok - > astParent ( ) ;
else if ( astIsLHS ( tok ) )
startTok = previousBeforeAstLeftmostLeaf ( tok - > astParent ( ) ) ;
if ( ! startTok )
startTok = tok - > previous ( ) ;
2021-01-23 17:52:01 +01:00
reverse ( startTok , nullptr , cond . vartok , values , tokenlist , settings ) ;
2021-01-10 14:46:19 +01:00
} ) ;
2021-01-08 22:55:04 +01:00
}
2020-12-25 08:58:12 +01:00
2022-04-07 06:48:13 +02:00
static Token * skipNotAndCasts ( Token * tok , bool * inverted = nullptr )
{
for ( ; tok - > astParent ( ) ; tok = tok - > astParent ( ) ) {
if ( Token : : simpleMatch ( tok - > astParent ( ) , " ! " ) ) {
if ( inverted )
* inverted ^ = true ;
continue ;
}
if ( Token : : Match ( tok - > astParent ( ) , " ==|!= " ) ) {
Token * sibling = tok - > astSibling ( ) ;
if ( sibling - > hasKnownIntValue ( ) & & ( astIsBool ( tok ) | | astIsBool ( sibling ) ) ) {
bool value = sibling - > values ( ) . front ( ) . intvalue ;
if ( inverted )
* inverted ^ = value = = Token : : simpleMatch ( tok - > astParent ( ) , " != " ) ;
continue ;
}
}
if ( tok - > astParent ( ) - > isCast ( ) & & astIsBool ( tok - > astParent ( ) ) )
continue ;
return tok ;
}
return tok ;
}
2021-01-08 22:55:04 +01:00
void afterCondition ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * errorLogger ,
const Settings * settings ) const {
2022-04-07 06:48:13 +02:00
traverseCondition ( tokenlist , symboldatabase , [ & ] ( const Condition & cond , Token * condTok , const Scope * scope ) {
const Token * top = condTok - > astTop ( ) ;
2021-01-08 22:55:04 +01:00
2021-01-10 14:46:19 +01:00
std : : list < ValueFlow : : Value > thenValues ;
std : : list < ValueFlow : : Value > elseValues ;
2021-01-08 22:55:04 +01:00
2022-04-07 06:48:13 +02:00
if ( ! Token : : Match ( condTok , " !=|=|(|. " ) & & condTok ! = cond . vartok ) {
2021-01-10 14:46:19 +01:00
thenValues . insert ( thenValues . end ( ) , cond . true_values . begin ( ) , cond . true_values . end ( ) ) ;
2022-04-07 06:48:13 +02:00
if ( cond . impossible & & isConditionKnown ( condTok , false ) )
2021-01-10 14:46:19 +01:00
insertImpossible ( elseValues , cond . false_values ) ;
}
2022-04-07 06:48:13 +02:00
if ( ! Token : : Match ( condTok , " ==|! " ) ) {
2021-01-10 14:46:19 +01:00
elseValues . insert ( elseValues . end ( ) , cond . false_values . begin ( ) , cond . false_values . end ( ) ) ;
2022-04-07 06:48:13 +02:00
if ( cond . impossible & & isConditionKnown ( condTok , true ) ) {
2021-01-10 14:46:19 +01:00
insertImpossible ( thenValues , cond . true_values ) ;
2022-04-07 06:48:13 +02:00
if ( cond . isBool ( ) )
2021-01-10 14:46:19 +01:00
insertNegateKnown ( thenValues , cond . true_values ) ;
2021-01-08 22:55:04 +01:00
}
2021-01-10 14:46:19 +01:00
}
2021-01-08 22:55:04 +01:00
2022-04-07 06:48:13 +02:00
bool inverted = cond . inverted ;
Token * ctx = skipNotAndCasts ( condTok , & inverted ) ;
if ( inverted )
2021-01-10 14:46:19 +01:00
std : : swap ( thenValues , elseValues ) ;
2020-12-25 08:58:12 +01:00
2022-04-07 06:48:13 +02:00
if ( Token : : Match ( ctx - > astParent ( ) , " %oror%|&& " ) ) {
Token * parent = ctx - > astParent ( ) ;
if ( astIsRHS ( ctx ) & & astIsLHS ( parent ) & & parent - > astParent ( ) & &
2021-08-01 10:13:14 +02:00
parent - > str ( ) = = parent - > astParent ( ) - > str ( ) )
2021-01-10 14:46:19 +01:00
parent = parent - > astParent ( ) ;
2022-04-07 06:48:13 +02:00
else if ( ! astIsLHS ( ctx ) ) {
2021-01-10 14:46:19 +01:00
parent = nullptr ;
}
if ( parent ) {
2021-08-02 10:51:34 +02:00
std : : vector < Token * > nextExprs = { parent - > astOperand2 ( ) } ;
if ( astIsLHS ( parent ) & & parent - > astParent ( ) & & parent - > astParent ( ) - > str ( ) = = parent - > str ( ) ) {
nextExprs . push_back ( parent - > astParent ( ) - > astOperand2 ( ) ) ;
}
2021-01-10 14:46:19 +01:00
const std : : string & op ( parent - > str ( ) ) ;
std : : list < ValueFlow : : Value > values ;
if ( op = = " && " )
values = thenValues ;
else if ( op = = " || " )
values = elseValues ;
2022-04-07 06:48:13 +02:00
if ( Token : : Match ( condTok , " ==|!= " ) | | cond . isBool ( ) )
2021-01-10 14:46:19 +01:00
changePossibleToKnown ( values ) ;
2021-08-01 10:13:14 +02:00
if ( astIsFloat ( cond . vartok , false ) | |
( ! cond . vartok - > valueType ( ) & &
2021-08-02 10:51:34 +02:00
std : : all_of ( values . begin ( ) , values . end ( ) , [ ] ( const ValueFlow : : Value & v ) {
2021-08-11 08:37:37 +02:00
return v . isIntValue ( ) | | v . isFloatValue ( ) ;
} ) ) )
values . remove_if ( [ & ] ( const ValueFlow : : Value & v ) {
return v . isImpossible ( ) ;
} ) ;
for ( Token * start : nextExprs ) {
2021-08-02 10:51:34 +02:00
Analyzer : : Result r = forward ( start , cond . vartok , values , tokenlist , settings ) ;
if ( r . terminate ! = Analyzer : : Terminate : : None )
return ;
}
2021-01-10 14:46:19 +01:00
}
}
{
2022-04-07 06:48:13 +02:00
const Token * tok2 = condTok ;
2021-01-10 14:46:19 +01:00
std : : string op ;
bool mixedOperators = false ;
while ( tok2 - > astParent ( ) ) {
const Token * parent = tok2 - > astParent ( ) ;
if ( Token : : Match ( parent , " %oror%|&& " ) ) {
if ( op . empty ( ) ) {
op = parent - > str ( ) ;
} else if ( op ! = parent - > str ( ) ) {
mixedOperators = true ;
break ;
2021-01-08 22:55:04 +01:00
}
}
2021-01-10 14:46:19 +01:00
if ( parent - > str ( ) = = " ! " ) {
op = ( op = = " && " ? " || " : " && " ) ;
}
tok2 = parent ;
}
if ( mixedOperators ) {
return ;
}
}
2021-11-14 18:30:36 +01:00
if ( ! top )
return ;
2020-09-10 16:18:30 +02:00
2021-11-25 18:40:15 +01:00
if ( top - > previous ( ) - > isExpandedMacro ( ) ) {
for ( std : : list < ValueFlow : : Value > * values : { & thenValues , & elseValues } ) {
for ( ValueFlow : : Value & v : * values )
v . macro = true ;
}
}
2021-11-14 18:30:36 +01:00
2022-04-07 06:48:13 +02:00
Token * condTop = ctx - > astParent ( ) ;
2022-02-25 05:53:51 +01:00
{
2022-04-07 06:48:13 +02:00
bool inverted2 = false ;
while ( Token : : Match ( condTop , " %oror%|&& " ) ) {
Token * parent = skipNotAndCasts ( condTop , & inverted2 ) - > astParent ( ) ;
if ( ! parent )
break ;
condTop = parent ;
2022-02-25 05:53:51 +01:00
}
2022-04-07 06:48:13 +02:00
if ( inverted2 )
std : : swap ( thenValues , elseValues ) ;
2022-02-25 05:53:51 +01:00
}
2022-04-10 22:47:58 +02:00
if ( ! condTop )
return ;
2022-04-07 06:48:13 +02:00
if ( Token : : simpleMatch ( condTop , " ? " ) ) {
Token * colon = condTop - > astOperand2 ( ) ;
2022-02-25 05:53:51 +01:00
forward ( colon - > astOperand1 ( ) , cond . vartok , thenValues , tokenlist , settings ) ;
forward ( colon - > astOperand2 ( ) , cond . vartok , elseValues , tokenlist , settings ) ;
// TODO: Handle after condition
return ;
}
2022-04-07 06:48:13 +02:00
if ( condTop ! = top & & condTop - > str ( ) ! = " ; " )
return ;
2021-11-14 18:30:36 +01:00
if ( ! Token : : Match ( top - > previous ( ) , " if|while|for ( " ) )
return ;
if ( top - > previous ( ) - > str ( ) = = " for " ) {
2022-04-07 06:48:13 +02:00
if ( ! Token : : Match ( condTok , " %comp% " ) )
2021-11-14 18:30:36 +01:00
return ;
2022-04-07 06:48:13 +02:00
if ( ! Token : : simpleMatch ( condTok - > astParent ( ) , " ; " ) )
2021-11-14 18:30:36 +01:00
return ;
const Token * stepTok = getStepTok ( top ) ;
if ( cond . vartok - > varId ( ) = = 0 )
return ;
if ( ! cond . vartok - > variable ( ) )
return ;
if ( ! Token : : Match ( stepTok , " ++|-- " ) )
return ;
std : : set < ValueFlow : : Value : : Bound > bounds ;
for ( const ValueFlow : : Value & v : thenValues ) {
if ( v . bound ! = ValueFlow : : Value : : Bound : : Point & & v . isImpossible ( ) )
2021-01-10 14:46:19 +01:00
continue ;
2021-11-14 18:30:36 +01:00
bounds . insert ( v . bound ) ;
2021-01-10 14:46:19 +01:00
}
2021-11-14 18:30:36 +01:00
if ( Token : : simpleMatch ( stepTok , " ++ " ) & & bounds . count ( ValueFlow : : Value : : Bound : : Lower ) > 0 )
return ;
if ( Token : : simpleMatch ( stepTok , " -- " ) & & bounds . count ( ValueFlow : : Value : : Bound : : Upper ) > 0 )
return ;
2022-04-07 06:48:13 +02:00
const Token * childTok = condTok - > astOperand1 ( ) ;
2021-11-14 18:30:36 +01:00
if ( ! childTok )
2022-04-07 06:48:13 +02:00
childTok = condTok - > astOperand2 ( ) ;
2021-11-14 18:30:36 +01:00
if ( ! childTok )
2021-01-10 14:46:19 +01:00
return ;
2021-11-14 18:30:36 +01:00
if ( childTok - > varId ( ) ! = cond . vartok - > varId ( ) )
return ;
const Token * startBlock = top - > link ( ) - > next ( ) ;
if ( isVariableChanged ( startBlock ,
startBlock - > link ( ) ,
cond . vartok - > varId ( ) ,
cond . vartok - > variable ( ) - > isGlobal ( ) ,
settings ,
tokenlist - > isCPP ( ) ) )
return ;
// Check if condition in for loop is always false
const Token * initTok = getInitTok ( top ) ;
ProgramMemory pm ;
execute ( initTok , & pm , nullptr , nullptr ) ;
MathLib : : bigint result = 1 ;
2022-04-07 06:48:13 +02:00
execute ( condTok , & pm , & result , nullptr ) ;
2021-11-14 18:30:36 +01:00
if ( result = = 0 )
return ;
// Remove condition since for condition is not redundant
for ( std : : list < ValueFlow : : Value > * values : { & thenValues , & elseValues } ) {
for ( ValueFlow : : Value & v : * values ) {
v . condition = nullptr ;
v . conditional = true ;
}
}
}
bool deadBranch [ ] = { false , false } ;
// start token of conditional code
Token * startTokens [ ] = { nullptr , nullptr } ;
// determine startToken(s)
if ( Token : : simpleMatch ( top - > link ( ) , " ) { " ) )
startTokens [ 0 ] = top - > link ( ) - > next ( ) ;
if ( Token : : simpleMatch ( top - > link ( ) - > linkAt ( 1 ) , " } else { " ) )
startTokens [ 1 ] = top - > link ( ) - > linkAt ( 1 ) - > tokAt ( 2 ) ;
int changeBlock = - 1 ;
int bailBlock = - 1 ;
for ( int i = 0 ; i < 2 ; i + + ) {
const Token * const startToken = startTokens [ i ] ;
if ( ! startToken )
continue ;
std : : list < ValueFlow : : Value > & values = ( i = = 0 ? thenValues : elseValues ) ;
2022-04-07 06:48:13 +02:00
valueFlowSetConditionToKnown ( condTok , values , i = = 0 ) ;
2021-11-14 18:30:36 +01:00
Analyzer : : Result r =
forward ( startTokens [ i ] , startTokens [ i ] - > link ( ) , cond . vartok , values , tokenlist , settings ) ;
deadBranch [ i ] = r . terminate = = Analyzer : : Terminate : : Escape ;
if ( r . action . isModified ( ) & & ! deadBranch [ i ] )
changeBlock = i ;
if ( r . terminate ! = Analyzer : : Terminate : : None & & r . terminate ! = Analyzer : : Terminate : : Escape & &
r . terminate ! = Analyzer : : Terminate : : Modified )
bailBlock = i ;
changeKnownToPossible ( values ) ;
}
if ( changeBlock > = 0 & & ! Token : : simpleMatch ( top - > previous ( ) , " while ( " ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
startTokens [ changeBlock ] - > link ( ) ,
" valueFlowAfterCondition: " + cond . vartok - > expressionString ( ) +
" is changed in conditional block " ) ;
return ;
} else if ( bailBlock > = 0 ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
startTokens [ bailBlock ] - > link ( ) ,
" valueFlowAfterCondition: bailing in conditional block " ) ;
return ;
}
// After conditional code..
if ( Token : : simpleMatch ( top - > link ( ) , " ) { " ) ) {
Token * after = top - > link ( ) - > linkAt ( 1 ) ;
bool dead_if = deadBranch [ 0 ] ;
bool dead_else = deadBranch [ 1 ] ;
const Token * unknownFunction = nullptr ;
2022-04-07 06:48:13 +02:00
if ( condTok - > astParent ( ) & & Token : : Match ( top - > previous ( ) , " while|for ( " ) )
2021-11-14 18:30:36 +01:00
dead_if = ! isBreakScope ( after ) ;
else if ( ! dead_if )
dead_if = isReturnScope ( after , & settings - > library , & unknownFunction ) ;
if ( ! dead_if & & unknownFunction ) {
2021-09-21 19:25:41 +02:00
if ( settings - > debugwarnings )
2021-11-14 18:30:36 +01:00
bailout ( tokenlist , errorLogger , unknownFunction , " possible noreturn scope " ) ;
2021-09-21 19:25:41 +02:00
return ;
2021-01-10 14:46:19 +01:00
}
2019-09-20 15:06:37 +02:00
2021-11-14 18:30:36 +01:00
if ( Token : : simpleMatch ( after , " } else { " ) ) {
after = after - > linkAt ( 2 ) ;
unknownFunction = nullptr ;
if ( ! dead_else )
dead_else = isReturnScope ( after , & settings - > library , & unknownFunction ) ;
if ( ! dead_else & & unknownFunction ) {
2019-09-20 15:06:37 +02:00
if ( settings - > debugwarnings )
2021-01-10 14:46:19 +01:00
bailout ( tokenlist , errorLogger , unknownFunction , " possible noreturn scope " ) ;
2020-12-24 20:07:46 +01:00
return ;
2019-09-20 15:06:37 +02:00
}
2021-11-14 18:30:36 +01:00
}
2018-11-24 10:07:12 +01:00
2021-11-14 18:30:36 +01:00
if ( dead_if & & dead_else )
return ;
2019-09-20 15:06:37 +02:00
2021-11-14 18:30:36 +01:00
std : : list < ValueFlow : : Value > values ;
if ( dead_if ) {
values = elseValues ;
} else if ( dead_else ) {
values = thenValues ;
} else {
std : : copy_if ( thenValues . begin ( ) ,
thenValues . end ( ) ,
std : : back_inserter ( values ) ,
std : : mem_fn ( & ValueFlow : : Value : : isPossible ) ) ;
std : : copy_if ( elseValues . begin ( ) ,
elseValues . end ( ) ,
std : : back_inserter ( values ) ,
std : : mem_fn ( & ValueFlow : : Value : : isPossible ) ) ;
}
2021-01-08 22:55:04 +01:00
2021-11-14 18:30:36 +01:00
if ( values . empty ( ) )
return ;
2021-04-09 07:43:54 +02:00
2021-11-14 18:30:36 +01:00
if ( dead_if | | dead_else ) {
2022-04-07 06:48:13 +02:00
const Token * parent = condTok - > astParent ( ) ;
2021-11-14 18:30:36 +01:00
// Skip the not operator
while ( Token : : simpleMatch ( parent , " ! " ) )
parent = parent - > astParent ( ) ;
bool possible = false ;
if ( Token : : Match ( parent , " &&|%oror% " ) ) {
std : : string op = parent - > str ( ) ;
while ( parent & & parent - > str ( ) = = op )
2021-04-09 07:43:54 +02:00
parent = parent - > astParent ( ) ;
2021-11-14 18:30:36 +01:00
if ( Token : : simpleMatch ( parent , " ! " ) | | Token : : simpleMatch ( parent , " == false " ) )
possible = op = = " || " ;
else
possible = op = = " && " ;
}
if ( possible ) {
values . remove_if ( std : : mem_fn ( & ValueFlow : : Value : : isImpossible ) ) ;
changeKnownToPossible ( values ) ;
} else {
2022-04-07 06:48:13 +02:00
valueFlowSetConditionToKnown ( condTok , values , true ) ;
valueFlowSetConditionToKnown ( condTok , values , false ) ;
2015-02-01 12:10:20 +01:00
}
2014-10-17 06:50:33 +02:00
}
2021-11-14 18:30:36 +01:00
if ( values . empty ( ) )
return ;
2022-02-02 13:01:44 +01:00
forward ( after , getEndOfExprScope ( cond . vartok , scope ) , cond . vartok , values , tokenlist , settings ) ;
2021-01-10 14:46:19 +01:00
}
} ) ;
2014-01-21 21:13:49 +01:00
}
2020-12-24 20:07:46 +01:00
virtual ~ ConditionHandler ( ) { }
2018-11-24 10:07:12 +01:00
} ;
2020-12-24 20:07:46 +01:00
static void valueFlowCondition ( const ValuePtr < ConditionHandler > & handler ,
TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * errorLogger ,
const Settings * settings )
2018-11-24 10:07:12 +01:00
{
2021-01-08 22:55:04 +01:00
handler - > beforeCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
2020-12-24 20:07:46 +01:00
handler - > afterCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
}
struct SimpleConditionHandler : ConditionHandler {
2021-06-19 13:59:48 +02:00
virtual Analyzer : : Result forward ( Token * start ,
const Token * stop ,
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
2022-02-10 23:02:24 +01:00
const Settings * settings ) const override {
2021-06-19 13:59:48 +02:00
return valueFlowForward ( start - > next ( ) , stop , exprTok , values , tokenlist , settings ) ;
2020-12-24 20:07:46 +01:00
}
2021-08-01 10:13:14 +02:00
virtual Analyzer : : Result forward ( Token * top ,
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
2022-02-10 23:02:24 +01:00
const Settings * settings ) const override {
2021-08-01 10:13:14 +02:00
return valueFlowForward ( top , exprTok , values , tokenlist , settings ) ;
}
2021-01-08 22:55:04 +01:00
virtual void reverse ( Token * start ,
2021-01-23 17:52:01 +01:00
const Token * endToken ,
2021-01-08 22:55:04 +01:00
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
2022-02-10 23:02:24 +01:00
const Settings * settings ) const override {
2021-01-23 17:52:01 +01:00
return valueFlowReverse ( start , endToken , exprTok , values , tokenlist , settings ) ;
2021-01-08 22:55:04 +01:00
}
2022-02-10 23:02:24 +01:00
virtual std : : vector < Condition > parse ( const Token * tok , const Settings * ) const override {
2020-12-24 20:07:46 +01:00
Condition cond ;
2018-11-24 10:07:12 +01:00
ValueFlow : : Value true_value ;
ValueFlow : : Value false_value ;
const Token * vartok = parseCompareInt ( tok , true_value , false_value ) ;
if ( vartok ) {
2021-07-30 21:29:35 +02:00
if ( vartok - > hasKnownIntValue ( ) )
2021-07-26 22:23:19 +02:00
return { } ;
2018-11-24 10:07:12 +01:00
if ( vartok - > str ( ) = = " = " & & vartok - > astOperand1 ( ) & & vartok - > astOperand2 ( ) )
vartok = vartok - > astOperand1 ( ) ;
cond . true_values . push_back ( true_value ) ;
cond . false_values . push_back ( false_value ) ;
cond . vartok = vartok ;
2021-07-26 22:23:19 +02:00
return { cond } ;
2018-11-24 10:07:12 +01:00
}
if ( tok - > str ( ) = = " ! " ) {
vartok = tok - > astOperand1 ( ) ;
2021-01-08 22:55:04 +01:00
} else if ( tok - > astParent ( ) & & ( Token : : Match ( tok - > astParent ( ) , " %oror%|&&|? " ) | |
2019-09-26 10:32:25 +02:00
Token : : Match ( tok - > astParent ( ) - > previous ( ) , " if|while ( " ) ) ) {
if ( Token : : simpleMatch ( tok , " = " ) )
vartok = tok - > astOperand1 ( ) ;
else if ( ! Token : : Match ( tok , " %comp%|%assign% " ) )
vartok = tok ;
2018-11-24 10:07:12 +01:00
}
2019-09-26 10:32:25 +02:00
if ( ! vartok )
2021-07-26 22:23:19 +02:00
return { } ;
2018-11-24 10:07:12 +01:00
cond . true_values . emplace_back ( tok , 0LL ) ;
2019-09-20 15:06:37 +02:00
cond . false_values . emplace_back ( tok , 0LL ) ;
2018-11-24 10:07:12 +01:00
cond . vartok = vartok ;
2021-07-26 22:23:19 +02:00
return { cond } ;
2020-12-24 20:07:46 +01:00
}
} ;
2014-01-21 21:13:49 +01:00
2021-10-09 16:19:06 +02:00
struct IntegralInferModel : InferModel {
2022-02-10 23:02:24 +01:00
virtual bool match ( const ValueFlow : : Value & value ) const override {
2021-10-09 16:19:06 +02:00
return value . isIntValue ( ) ;
2019-12-26 15:47:53 +01:00
}
2022-02-10 23:02:24 +01:00
virtual ValueFlow : : Value yield ( MathLib : : bigint value ) const override
2021-10-09 16:19:06 +02:00
{
ValueFlow : : Value result ( value ) ;
result . valueType = ValueFlow : : Value : : ValueType : : INT ;
result . setKnown ( ) ;
return result ;
2019-09-20 15:06:37 +02:00
}
2021-10-09 16:19:06 +02:00
} ;
2019-09-20 15:06:37 +02:00
2021-10-30 22:13:58 +02:00
ValuePtr < InferModel > makeIntegralInferModel ( ) {
return IntegralInferModel { } ;
}
2021-03-30 14:02:28 +02:00
ValueFlow : : Value inferCondition ( const std : : string & op , const Token * varTok , MathLib : : bigint val )
{
if ( ! varTok )
return ValueFlow : : Value { } ;
if ( varTok - > hasKnownIntValue ( ) )
return ValueFlow : : Value { } ;
2021-10-09 16:19:06 +02:00
std : : vector < ValueFlow : : Value > r = infer ( IntegralInferModel { } , op , varTok - > values ( ) , val ) ;
if ( r . size ( ) = = 1 & & r . front ( ) . isKnown ( ) )
return r . front ( ) ;
return ValueFlow : : Value { } ;
2021-03-30 14:02:28 +02:00
}
ValueFlow : : Value inferCondition ( std : : string op , MathLib : : bigint val , const Token * varTok )
{
2021-10-09 16:19:06 +02:00
if ( ! varTok )
return ValueFlow : : Value { } ;
if ( varTok - > hasKnownIntValue ( ) )
return ValueFlow : : Value { } ;
std : : vector < ValueFlow : : Value > r = infer ( IntegralInferModel { } , op , val , varTok - > values ( ) ) ;
if ( r . size ( ) = = 1 & & r . front ( ) . isKnown ( ) )
return r . front ( ) ;
return ValueFlow : : Value { } ;
2021-03-30 14:02:28 +02:00
}
2021-10-31 14:51:07 +01:00
struct IteratorInferModel : InferModel {
virtual ValueFlow : : Value : : ValueType getType ( ) const = 0 ;
2022-02-10 23:02:24 +01:00
virtual bool match ( const ValueFlow : : Value & value ) const override {
2021-10-31 14:51:07 +01:00
return value . valueType = = getType ( ) ;
}
2022-02-10 23:02:24 +01:00
virtual ValueFlow : : Value yield ( MathLib : : bigint value ) const override
2021-10-31 14:51:07 +01:00
{
ValueFlow : : Value result ( value ) ;
result . valueType = getType ( ) ;
result . setKnown ( ) ;
return result ;
}
} ;
struct EndIteratorInferModel : IteratorInferModel {
2022-02-10 23:02:24 +01:00
virtual ValueFlow : : Value : : ValueType getType ( ) const override {
2021-10-31 14:51:07 +01:00
return ValueFlow : : Value : : ValueType : : ITERATOR_END ;
}
} ;
struct StartIteratorInferModel : IteratorInferModel {
2022-02-10 23:02:24 +01:00
virtual ValueFlow : : Value : : ValueType getType ( ) const override {
2021-10-31 14:51:07 +01:00
return ValueFlow : : Value : : ValueType : : ITERATOR_END ;
}
} ;
2019-09-20 15:06:37 +02:00
static void valueFlowInferCondition ( TokenList * tokenlist ,
const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > astParent ( ) )
continue ;
2021-07-30 21:29:35 +02:00
if ( tok - > hasKnownIntValue ( ) )
2019-09-20 15:06:37 +02:00
continue ;
2020-05-19 21:03:28 +02:00
if ( tok - > variable ( ) & & ( Token : : Match ( tok - > astParent ( ) , " ?|&&|!|%oror% " ) | |
Token : : Match ( tok - > astParent ( ) - > previous ( ) , " if|while ( " ) ) ) {
2021-10-09 16:19:06 +02:00
std : : vector < ValueFlow : : Value > result = infer ( IntegralInferModel { } , " != " , tok - > values ( ) , 0 ) ;
if ( result . size ( ) ! = 1 )
2019-09-20 15:06:37 +02:00
continue ;
2021-10-09 16:19:06 +02:00
ValueFlow : : Value value = result . front ( ) ;
2019-09-20 15:06:37 +02:00
value . intvalue = 1 ;
2019-12-26 15:47:53 +01:00
value . bound = ValueFlow : : Value : : Bound : : Point ;
2019-09-20 15:06:37 +02:00
setTokenValue ( tok , value , settings ) ;
2021-10-09 16:19:06 +02:00
} else if ( Token : : Match ( tok , " %comp%|- " ) & & tok - > astOperand1 ( ) & & tok - > astOperand2 ( ) ) {
2021-10-31 14:51:07 +01:00
if ( astIsIterator ( tok - > astOperand1 ( ) ) | | astIsIterator ( tok - > astOperand2 ( ) ) ) {
2021-11-01 19:18:18 +01:00
static const std : : array < ValuePtr < InferModel > , 2 > iteratorModels = { EndIteratorInferModel { } ,
StartIteratorInferModel { } } ;
for ( const ValuePtr < InferModel > & model : iteratorModels ) {
2021-10-31 14:51:07 +01:00
std : : vector < ValueFlow : : Value > result =
infer ( model , tok - > str ( ) , tok - > astOperand1 ( ) - > values ( ) , tok - > astOperand2 ( ) - > values ( ) ) ;
for ( ValueFlow : : Value value : result ) {
value . valueType = ValueFlow : : Value : : ValueType : : INT ;
setTokenValue ( tok , value , settings ) ;
}
}
} else {
std : : vector < ValueFlow : : Value > result =
infer ( IntegralInferModel { } , tok - > str ( ) , tok - > astOperand1 ( ) - > values ( ) , tok - > astOperand2 ( ) - > values ( ) ) ;
for ( const ValueFlow : : Value & value : result ) {
setTokenValue ( tok , value , settings ) ;
}
2019-09-20 15:06:37 +02:00
}
}
}
}
2021-08-19 22:01:55 +02:00
struct SymbolicConditionHandler : SimpleConditionHandler {
2022-04-07 06:48:13 +02:00
static bool isNegatedBool ( const Token * tok )
{
if ( ! Token : : simpleMatch ( tok , " ! " ) )
return false ;
return ( astIsBool ( tok - > astOperand1 ( ) ) ) ;
}
static const Token * skipNot ( const Token * tok )
{
if ( ! Token : : simpleMatch ( tok , " ! " ) )
return tok ;
return tok - > astOperand1 ( ) ;
}
2022-04-28 10:48:37 +02:00
virtual std : : vector < Condition > parse ( const Token * tok , const Settings * settings ) const override
2021-08-19 22:01:55 +02:00
{
if ( ! Token : : Match ( tok , " %comp% " ) )
return { } ;
if ( tok - > hasKnownIntValue ( ) )
return { } ;
if ( ! tok - > astOperand1 ( ) | | tok - > astOperand1 ( ) - > hasKnownIntValue ( ) | | tok - > astOperand1 ( ) - > isLiteral ( ) )
return { } ;
if ( ! tok - > astOperand2 ( ) | | tok - > astOperand2 ( ) - > hasKnownIntValue ( ) | | tok - > astOperand2 ( ) - > isLiteral ( ) )
return { } ;
2022-04-28 10:48:37 +02:00
if ( ! isConstExpression ( tok , settings - > library , true , true ) )
return { } ;
2021-08-19 22:01:55 +02:00
std : : vector < Condition > result ;
2022-04-07 06:48:13 +02:00
auto addCond = [ & ] ( const Token * lhsTok , const Token * rhsTok , bool inverted ) {
for ( int i = 0 ; i < 2 ; i + + ) {
const bool lhs = i = = 0 ;
const Token * vartok = lhs ? lhsTok : rhsTok ;
const Token * valuetok = lhs ? rhsTok : lhsTok ;
if ( valuetok - > exprId ( ) = = 0 )
continue ;
if ( valuetok - > hasKnownSymbolicValue ( vartok ) )
continue ;
if ( vartok - > hasKnownSymbolicValue ( valuetok ) )
continue ;
ValueFlow : : Value true_value ;
ValueFlow : : Value false_value ;
setConditionalValues ( tok , ! lhs , 0 , true_value , false_value ) ;
setSymbolic ( true_value , valuetok ) ;
setSymbolic ( false_value , valuetok ) ;
Condition cond ;
cond . true_values = { true_value } ;
cond . false_values = { false_value } ;
cond . vartok = vartok ;
cond . inverted = inverted ;
result . push_back ( cond ) ;
}
} ;
addCond ( tok - > astOperand1 ( ) , tok - > astOperand2 ( ) , false ) ;
if ( Token : : Match ( tok , " ==|!= " ) & & ( isNegatedBool ( tok - > astOperand1 ( ) ) | | isNegatedBool ( tok - > astOperand2 ( ) ) ) ) {
const Token * lhsTok = skipNot ( tok - > astOperand1 ( ) ) ;
const Token * rhsTok = skipNot ( tok - > astOperand2 ( ) ) ;
addCond ( lhsTok , rhsTok , ! ( isNegatedBool ( tok - > astOperand1 ( ) ) & & isNegatedBool ( tok - > astOperand2 ( ) ) ) ) ;
2021-08-19 22:01:55 +02:00
}
return result ;
}
} ;
2014-03-17 18:43:47 +01:00
static bool valueFlowForLoop2 ( const Token * tok ,
2015-09-12 19:49:02 +02:00
ProgramMemory * memory1 ,
ProgramMemory * memory2 ,
ProgramMemory * memoryAfter )
2014-03-17 18:43:47 +01:00
{
2015-06-07 11:25:33 +02:00
// for ( firstExpression ; secondExpression ; thirdExpression )
2014-03-17 18:43:47 +01:00
const Token * firstExpression = tok - > next ( ) - > astOperand2 ( ) - > astOperand1 ( ) ;
const Token * secondExpression = tok - > next ( ) - > astOperand2 ( ) - > astOperand2 ( ) - > astOperand1 ( ) ;
const Token * thirdExpression = tok - > next ( ) - > astOperand2 ( ) - > astOperand2 ( ) - > astOperand2 ( ) ;
2015-09-12 19:49:02 +02:00
ProgramMemory programMemory ;
2014-03-30 10:22:06 +02:00
MathLib : : bigint result ( 0 ) ;
2014-03-17 18:43:47 +01:00
bool error = false ;
execute ( firstExpression , & programMemory , & result , & error ) ;
if ( error )
return false ;
execute ( secondExpression , & programMemory , & result , & error ) ;
2016-12-20 07:54:38 +01:00
if ( result = = 0 ) // 2nd expression is false => no looping
return false ;
2014-06-30 07:26:48 +02:00
if ( error ) {
// If a variable is reassigned in second expression, return false
2018-11-23 19:16:19 +01:00
bool reassign = false ;
visitAstNodes ( secondExpression ,
2021-08-07 20:51:18 +02:00
[ & ] ( const Token * t ) {
2015-09-12 19:49:02 +02:00
if ( t - > str ( ) = = " = " & & t - > astOperand1 ( ) & & programMemory . hasValue ( t - > astOperand1 ( ) - > varId ( ) ) )
2014-06-30 07:26:48 +02:00
// TODO: investigate what variable is assigned.
2018-11-23 19:16:19 +01:00
reassign = true ;
return reassign ? ChildrenToVisit : : done : ChildrenToVisit : : op1_and_op2 ;
} ) ;
if ( reassign )
return false ;
2014-06-30 07:26:48 +02:00
}
2014-03-24 00:16:02 +01:00
2015-09-12 19:49:02 +02:00
ProgramMemory startMemory ( programMemory ) ;
ProgramMemory endMemory ;
2014-03-17 18:43:47 +01:00
2019-07-15 13:47:17 +02:00
int maxcount = 10000 ;
while ( result ! = 0 & & ! error & & - - maxcount > 0 ) {
2014-03-17 18:43:47 +01:00
endMemory = programMemory ;
execute ( thirdExpression , & programMemory , & result , & error ) ;
if ( ! error )
execute ( secondExpression , & programMemory , & result , & error ) ;
}
2021-05-15 08:39:20 +02:00
if ( memory1 )
memory1 - > swap ( startMemory ) ;
2014-08-17 10:40:22 +02:00
if ( ! error ) {
2021-05-15 08:39:20 +02:00
if ( memory2 )
memory2 - > swap ( endMemory ) ;
if ( memoryAfter )
memoryAfter - > swap ( programMemory ) ;
2014-08-17 10:40:22 +02:00
}
2014-03-17 18:43:47 +01:00
2014-03-24 00:16:02 +01:00
return true ;
2014-03-17 18:43:47 +01:00
}
2022-02-06 20:15:45 +01:00
static void valueFlowForLoopSimplify ( Token * const bodyStart ,
const Token * expr ,
bool globalvar ,
const MathLib : : bigint value ,
TokenList * tokenlist ,
ErrorLogger * errorLogger ,
const Settings * settings )
2014-03-17 18:43:47 +01:00
{
2022-05-18 08:29:30 +02:00
// TODO: Refactor this to use arbitary expressions
assert ( expr - > varId ( ) > 0 ) ;
2014-03-17 18:43:47 +01:00
const Token * const bodyEnd = bodyStart - > link ( ) ;
// Is variable modified inside for loop
2022-02-06 20:15:45 +01:00
if ( isVariableChanged ( bodyStart , bodyEnd , expr - > varId ( ) , globalvar , settings , tokenlist - > isCPP ( ) ) )
2014-03-17 18:43:47 +01:00
return ;
for ( Token * tok2 = bodyStart - > next ( ) ; tok2 ! = bodyEnd ; tok2 = tok2 - > next ( ) ) {
2022-02-06 20:15:45 +01:00
if ( tok2 - > varId ( ) = = expr - > varId ( ) ) {
2014-03-17 18:43:47 +01:00
const Token * parent = tok2 - > astParent ( ) ;
while ( parent ) {
const Token * const p = parent ;
parent = parent - > astParent ( ) ;
2014-09-03 12:17:34 +02:00
if ( ! parent | | parent - > str ( ) = = " : " )
2014-03-17 18:43:47 +01:00
break ;
2014-09-03 12:17:34 +02:00
if ( parent - > str ( ) = = " ? " ) {
2014-03-25 18:22:22 +01:00
if ( parent - > astOperand2 ( ) ! = p )
2015-12-05 20:55:26 +01:00
parent = nullptr ;
2014-03-25 18:22:22 +01:00
break ;
}
2014-03-15 11:29:33 +01:00
}
2014-03-17 18:43:47 +01:00
if ( parent ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop variable " + tok2 - > str ( ) + " stopping on ? " ) ;
continue ;
}
ValueFlow : : Value value1 ( value ) ;
value1 . varId = tok2 - > varId ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok2 , value1 , settings ) ;
2014-03-16 08:38:52 +01:00
}
2014-01-07 19:20:56 +01:00
2014-08-01 16:12:57 +02:00
if ( Token : : Match ( tok2 , " %oror%|&& " ) ) {
2022-02-06 20:15:45 +01:00
const ProgramMemory programMemory ( getProgramMemory ( tok2 - > astTop ( ) , expr , ValueFlow : : Value ( value ) , settings ) ) ;
2016-12-11 21:19:24 +01:00
if ( ( tok2 - > str ( ) = = " && " & & ! conditionIsTrue ( tok2 - > astOperand1 ( ) , programMemory ) ) | |
( tok2 - > str ( ) = = " || " & & ! conditionIsFalse ( tok2 - > astOperand1 ( ) , programMemory ) ) ) {
2014-08-01 16:12:57 +02:00
// Skip second expression..
const Token * parent = tok2 ;
while ( parent & & parent - > str ( ) = = tok2 - > str ( ) )
parent = parent - > astParent ( ) ;
2014-08-13 04:03:17 +02:00
// Jump to end of condition
2014-08-13 05:36:17 +02:00
if ( parent & & parent - > str ( ) = = " ( " ) {
2014-08-01 16:12:57 +02:00
tok2 = parent - > link ( ) ;
2014-08-13 05:36:17 +02:00
// cast
if ( Token : : simpleMatch ( tok2 , " ) ( " ) )
tok2 = tok2 - > linkAt ( 1 ) ;
}
2014-08-01 16:12:57 +02:00
}
}
2022-02-17 22:12:19 +01:00
const Token * vartok = expr ;
2022-02-21 18:14:20 +01:00
const Token * rml = nextAfterAstRightmostLeaf ( vartok ) ;
2022-02-17 22:12:19 +01:00
if ( rml )
2022-02-21 18:14:20 +01:00
vartok = rml - > str ( ) = = " ] " ? rml : rml - > previous ( ) ;
if ( vartok - > str ( ) = = " ] " & & vartok - > link ( ) - > previous ( ) )
vartok = vartok - > link ( ) - > previous ( ) ;
2022-02-06 20:15:45 +01:00
if ( ( tok2 - > str ( ) = = " && " & &
conditionIsFalse ( tok2 - > astOperand1 ( ) ,
getProgramMemory ( tok2 - > astTop ( ) , expr , ValueFlow : : Value ( value ) , settings ) ) ) | |
( tok2 - > str ( ) = = " || " & &
conditionIsTrue ( tok2 - > astOperand1 ( ) ,
getProgramMemory ( tok2 - > astTop ( ) , expr , ValueFlow : : Value ( value ) , settings ) ) ) )
2014-08-01 16:12:57 +02:00
break ;
2022-02-26 23:48:29 +01:00
else if ( Token : : simpleMatch ( tok2 , " ) { " )) {
if ( vartok - > varId ( ) & & Token : : findmatch ( tok2 - > link ( ) , " %varid% " , tok2 , vartok - > varId ( ) ) ) {
if ( Token : : findmatch ( tok2 , " continue|break|return " , tok2 - > linkAt ( 1 ) , vartok - > varId ( ) ) ) {
2014-03-25 21:40:36 +01:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop variable bailout on conditional continue|break|return " ) ;
break ;
}
2022-02-26 23:48:29 +01:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop variable skipping conditional scope " ) ;
tok2 = tok2 - > next ( ) - > link ( ) ;
if ( Token : : simpleMatch ( tok2 , " } else { " ) ) {
if ( Token : : findmatch ( tok2 , " continue|break|return " , tok2 - > linkAt ( 2 ) , vartok - > varId ( ) ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop variable bailout on conditional continue|break|return " ) ;
break ;
}
tok2 = tok2 - > linkAt ( 2 ) ;
}
}
else {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop skipping {} code " ) ;
tok2 = tok2 - > linkAt ( 1 ) ;
if ( Token : : simpleMatch ( tok2 , " } else { " ) )
tok2 = tok2 - > linkAt ( 2 ) ;
2014-03-25 21:40:36 +01:00
}
2016-01-24 13:11:51 +01:00
}
2014-03-17 18:43:47 +01:00
}
}
2021-07-18 07:46:31 +02:00
static void valueFlowForLoopSimplifyAfter ( Token * fortok ,
2021-08-07 20:51:18 +02:00
nonneg int varid ,
const MathLib : : bigint num ,
TokenList * tokenlist ,
const Settings * settings )
2014-08-17 10:40:22 +02:00
{
const Token * vartok = nullptr ;
for ( const Token * tok = fortok ; tok ; tok = tok - > next ( ) ) {
if ( tok - > varId ( ) = = varid ) {
vartok = tok ;
break ;
}
}
if ( ! vartok | | ! vartok - > variable ( ) )
return ;
const Variable * var = vartok - > variable ( ) ;
const Token * endToken = nullptr ;
if ( var - > isLocal ( ) )
2020-01-11 09:17:32 +01:00
endToken = var - > scope ( ) - > bodyEnd ;
2014-08-17 10:40:22 +02:00
else
2018-04-27 22:36:30 +02:00
endToken = fortok - > scope ( ) - > bodyEnd ;
2014-08-17 10:40:22 +02:00
2020-03-20 07:16:05 +01:00
Token * blockTok = fortok - > linkAt ( 1 ) - > linkAt ( 1 ) ;
2014-08-17 10:40:22 +02:00
std : : list < ValueFlow : : Value > values ;
2018-04-11 09:44:35 +02:00
values . emplace_back ( num ) ;
values . back ( ) . errorPath . emplace_back ( fortok , " After for loop, " + var - > name ( ) + " has value " + values . back ( ) . infoString ( ) ) ;
2014-08-17 10:40:22 +02:00
2020-03-20 07:16:05 +01:00
if ( blockTok ! = endToken ) {
2021-07-18 07:46:31 +02:00
valueFlowForward ( blockTok - > next ( ) , endToken , vartok , values , tokenlist , settings ) ;
2020-03-20 07:16:05 +01:00
}
2014-08-17 10:40:22 +02:00
}
2015-02-01 12:10:20 +01:00
static void valueFlowForLoop ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-03-17 18:43:47 +01:00
{
2018-07-14 22:26:22 +02:00
for ( const Scope & scope : symboldatabase - > scopeList ) {
if ( scope . type ! = Scope : : eFor )
2014-03-17 18:43:47 +01:00
continue ;
2014-03-16 08:38:52 +01:00
2018-07-14 22:26:22 +02:00
Token * tok = const_cast < Token * > ( scope . classDef ) ;
Token * const bodyStart = const_cast < Token * > ( scope . bodyStart ) ;
2014-03-16 08:38:52 +01:00
2015-02-01 12:26:46 +01:00
if ( ! Token : : simpleMatch ( tok - > next ( ) - > astOperand2 ( ) , " ; " ) | |
! Token : : simpleMatch ( tok - > next ( ) - > astOperand2 ( ) - > astOperand2 ( ) , " ; " ) )
continue ;
2020-06-29 21:01:43 +02:00
nonneg int varid ;
2020-06-29 21:53:14 +02:00
bool knownInitValue , partialCond ;
2020-06-29 21:01:43 +02:00
MathLib : : bigint initValue , stepValue , lastValue ;
2014-03-16 08:38:52 +01:00
2020-06-29 21:53:14 +02:00
if ( extractForLoopValues ( tok , & varid , & knownInitValue , & initValue , & partialCond , & stepValue , & lastValue ) ) {
2020-06-29 21:01:43 +02:00
const bool executeBody = ! knownInitValue | | initValue < = lastValue ;
2021-08-16 09:19:07 +02:00
const Token * vartok = Token : : findmatch ( tok , " %varid% " , bodyStart , varid ) ;
if ( executeBody & & vartok ) {
std : : list < ValueFlow : : Value > initValues ;
initValues . emplace_back ( initValue , ValueFlow : : Value : : Bound : : Lower ) ;
initValues . push_back ( asImpossible ( initValues . back ( ) ) ) ;
Analyzer : : Result result =
valueFlowForward ( bodyStart , bodyStart - > link ( ) , vartok , initValues , tokenlist , settings ) ;
if ( ! result . action . isModified ( ) ) {
std : : list < ValueFlow : : Value > lastValues ;
lastValues . emplace_back ( lastValue , ValueFlow : : Value : : Bound : : Upper ) ;
lastValues . back ( ) . conditional = true ;
lastValues . push_back ( asImpossible ( lastValues . back ( ) ) ) ;
if ( stepValue ! = 1 )
lastValues . pop_front ( ) ;
valueFlowForward ( bodyStart , bodyStart - > link ( ) , vartok , lastValues , tokenlist , settings ) ;
}
2020-06-29 21:01:43 +02:00
}
const MathLib : : bigint afterValue = executeBody ? lastValue + stepValue : initValue ;
2021-07-18 07:46:31 +02:00
valueFlowForLoopSimplifyAfter ( tok , varid , afterValue , tokenlist , settings ) ;
2014-03-17 18:43:47 +01:00
} else {
2015-09-12 19:49:02 +02:00
ProgramMemory mem1 , mem2 , memAfter ;
2014-08-17 10:40:22 +02:00
if ( valueFlowForLoop2 ( tok , & mem1 , & mem2 , & memAfter ) ) {
2022-05-18 08:29:30 +02:00
for ( const auto & p : mem1 ) {
if ( ! p . second . isIntValue ( ) )
2016-11-14 06:42:52 +01:00
continue ;
2022-05-18 08:29:30 +02:00
if ( p . first . tok - > varId ( ) = = 0 )
continue ;
valueFlowForLoopSimplify ( bodyStart , p . first . tok , false , p . second . intvalue , tokenlist , errorLogger , settings ) ;
2016-11-14 06:42:52 +01:00
}
2022-05-18 08:29:30 +02:00
for ( const auto & p : mem2 ) {
if ( ! p . second . isIntValue ( ) )
continue ;
if ( p . first . tok - > varId ( ) = = 0 )
2016-11-14 06:42:52 +01:00
continue ;
2022-05-18 08:29:30 +02:00
valueFlowForLoopSimplify ( bodyStart , p . first . tok , false , p . second . intvalue , tokenlist , errorLogger , settings ) ;
2016-11-14 06:42:52 +01:00
}
2022-05-18 08:29:30 +02:00
for ( const auto & p : memAfter ) {
if ( ! p . second . isIntValue ( ) )
continue ;
if ( p . first . tok - > varId ( ) = = 0 )
2016-11-14 06:42:52 +01:00
continue ;
2022-05-18 08:29:30 +02:00
valueFlowForLoopSimplifyAfter ( tok , p . first . getExpressionId ( ) , p . second . intvalue , tokenlist , settings ) ;
2016-11-14 06:42:52 +01:00
}
2014-01-07 19:20:56 +01:00
}
}
}
}
2020-11-10 16:00:55 +01:00
struct MultiValueFlowAnalyzer : ValueFlowAnalyzer {
2020-03-01 16:46:20 +01:00
std : : unordered_map < nonneg int , ValueFlow : : Value > values ;
std : : unordered_map < nonneg int , const Variable * > vars ;
2021-01-11 08:00:13 +01:00
SymbolDatabase * symboldatabase ;
2020-03-01 16:46:20 +01:00
2021-01-11 08:00:13 +01:00
MultiValueFlowAnalyzer ( ) : ValueFlowAnalyzer ( ) , values ( ) , vars ( ) , symboldatabase ( nullptr ) { }
2020-03-01 16:46:20 +01:00
2021-01-11 08:00:13 +01:00
MultiValueFlowAnalyzer ( const std : : unordered_map < const Variable * , ValueFlow : : Value > & args , const TokenList * t , SymbolDatabase * s )
: ValueFlowAnalyzer ( t ) , values ( ) , vars ( ) , symboldatabase ( s ) {
2020-03-01 20:39:00 +01:00
for ( const auto & p : args ) {
2020-03-01 16:46:20 +01:00
values [ p . first - > declarationId ( ) ] = p . second ;
vars [ p . first - > declarationId ( ) ] = p . first ;
}
}
virtual const std : : unordered_map < nonneg int , const Variable * > & getVars ( ) const {
return vars ;
}
2022-02-10 23:02:24 +01:00
virtual const ValueFlow : : Value * getValue ( const Token * tok ) const override {
2020-03-01 16:46:20 +01:00
if ( tok - > varId ( ) = = 0 )
return nullptr ;
auto it = values . find ( tok - > varId ( ) ) ;
if ( it = = values . end ( ) )
return nullptr ;
return & it - > second ;
}
2022-02-10 23:02:24 +01:00
virtual ValueFlow : : Value * getValue ( const Token * tok ) override {
2020-03-01 16:46:20 +01:00
if ( tok - > varId ( ) = = 0 )
return nullptr ;
auto it = values . find ( tok - > varId ( ) ) ;
if ( it = = values . end ( ) )
return nullptr ;
return & it - > second ;
}
2022-02-10 23:02:24 +01:00
virtual void makeConditional ( ) override {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
p . second . conditional = true ;
}
}
2022-02-10 23:02:24 +01:00
virtual void addErrorPath ( const Token * tok , const std : : string & s ) override {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
p . second . errorPath . emplace_back ( tok , " Assuming condition is " + s ) ;
}
}
2022-02-10 23:02:24 +01:00
virtual bool isAlias ( const Token * tok , bool & inconclusive ) const override {
2021-02-10 08:18:21 +01:00
const auto range = SelectValueFromVarIdMapRange ( & values ) ;
2020-04-03 09:25:21 +02:00
2020-03-01 16:46:20 +01:00
for ( const auto & p : getVars ( ) ) {
nonneg int varid = p . first ;
const Variable * var = p . second ;
if ( tok - > varId ( ) = = varid )
return true ;
2021-02-10 08:18:21 +01:00
if ( isAliasOf ( var , tok , varid , range , & inconclusive ) )
2020-03-01 16:46:20 +01:00
return true ;
}
return false ;
}
2022-02-10 23:02:24 +01:00
virtual bool isGlobal ( ) const override {
2020-03-01 16:46:20 +01:00
return false ;
}
2022-02-10 23:02:24 +01:00
virtual bool lowerToPossible ( ) override {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
if ( p . second . isImpossible ( ) )
return false ;
p . second . changeKnownToPossible ( ) ;
}
return true ;
}
2022-02-10 23:02:24 +01:00
virtual bool lowerToInconclusive ( ) override {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
if ( p . second . isImpossible ( ) )
return false ;
p . second . setInconclusive ( ) ;
}
return true ;
}
2022-02-10 23:02:24 +01:00
virtual bool isConditional ( ) const override {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
if ( p . second . conditional )
return true ;
if ( p . second . condition )
return ! p . second . isImpossible ( ) ;
}
return false ;
}
2022-02-10 23:02:24 +01:00
virtual bool stopOnCondition ( const Token * ) const override {
2021-08-21 18:52:11 +02:00
return isConditional ( ) ;
}
2022-02-10 23:02:24 +01:00
virtual bool updateScope ( const Token * endBlock , bool ) const override {
2020-03-01 16:46:20 +01:00
const Scope * scope = endBlock - > scope ( ) ;
if ( ! scope )
return false ;
if ( scope - > type = = Scope : : eLambda ) {
2020-03-01 20:39:00 +01:00
for ( const auto & p : values ) {
2020-03-01 16:46:20 +01:00
if ( ! p . second . isLifetimeValue ( ) )
return false ;
}
return true ;
} else if ( scope - > type = = Scope : : eIf | | scope - > type = = Scope : : eElse | | scope - > type = = Scope : : eWhile | |
scope - > type = = Scope : : eFor ) {
auto pred = [ ] ( const ValueFlow : : Value & value ) {
if ( value . isKnown ( ) )
return true ;
if ( value . isImpossible ( ) )
return true ;
if ( value . isLifetimeValue ( ) )
return true ;
return false ;
} ;
if ( std : : all_of ( values . begin ( ) , values . end ( ) , std : : bind ( pred , std : : bind ( SelectMapValues { } , std : : placeholders : : _1 ) ) ) )
return true ;
if ( isConditional ( ) )
return false ;
const Token * condTok = getCondTokFromEnd ( endBlock ) ;
std : : set < nonneg int > varids ;
std : : transform ( getVars ( ) . begin ( ) , getVars ( ) . end ( ) , std : : inserter ( varids , varids . begin ( ) ) , SelectMapKeys { } ) ;
return bifurcate ( condTok , varids , getSettings ( ) ) ;
}
return false ;
}
2022-02-10 23:02:24 +01:00
virtual bool match ( const Token * tok ) const override {
2020-03-01 16:46:20 +01:00
return values . count ( tok - > varId ( ) ) > 0 ;
}
2022-02-10 23:02:24 +01:00
virtual ProgramState getProgramState ( ) const override {
2020-03-01 16:46:20 +01:00
ProgramState ps ;
2022-02-06 20:15:45 +01:00
for ( const auto & p : values ) {
const Variable * var = vars . at ( p . first ) ;
2022-02-10 07:43:27 +01:00
if ( ! var )
continue ;
2022-02-06 20:15:45 +01:00
ps [ var - > nameToken ( ) ] = p . second ;
}
2020-03-01 16:46:20 +01:00
return ps ;
}
2021-01-11 08:00:13 +01:00
2022-02-10 23:02:24 +01:00
virtual void forkScope ( const Token * endBlock ) override {
2022-02-06 20:15:45 +01:00
ProgramMemory pm { getProgramState ( ) } ;
2021-01-11 08:00:13 +01:00
const Scope * scope = endBlock - > scope ( ) ;
const Token * condTok = getCondTokFromEnd ( endBlock ) ;
if ( scope & & condTok )
programMemoryParseCondition ( pm , condTok , nullptr , getSettings ( ) , scope - > type ! = Scope : : eElse ) ;
2021-05-15 08:39:20 +02:00
if ( condTok & & Token : : simpleMatch ( condTok - > astParent ( ) , " ; " ) ) {
ProgramMemory endMemory ;
if ( valueFlowForLoop2 ( condTok - > astTop ( ) - > previous ( ) , nullptr , & endMemory , nullptr ) )
pm . replace ( endMemory ) ;
}
2021-01-11 08:00:13 +01:00
// ProgramMemory pm = pms.get(endBlock->link()->next(), getProgramState());
2022-02-06 20:15:45 +01:00
for ( const auto & p : pm ) {
nonneg int varid = p . first . getExpressionId ( ) ;
2021-10-23 14:47:10 +02:00
if ( symboldatabase & & ! symboldatabase - > isVarId ( varid ) )
2021-01-21 19:49:37 +01:00
continue ;
2021-01-11 08:00:13 +01:00
ValueFlow : : Value value = p . second ;
if ( vars . count ( varid ) ! = 0 )
continue ;
if ( value . isImpossible ( ) )
continue ;
value . setPossible ( ) ;
values [ varid ] = value ;
if ( symboldatabase )
vars [ varid ] = symboldatabase - > getVariableFromVarId ( varid ) ;
}
}
2020-03-01 16:46:20 +01:00
} ;
2021-08-27 05:46:33 +02:00
template < class Key , class F >
bool productParams ( const std : : unordered_map < Key , std : : list < ValueFlow : : Value > > & vars , F f )
2020-03-01 16:46:20 +01:00
{
2021-08-27 05:46:33 +02:00
using Args = std : : vector < std : : unordered_map < Key , ValueFlow : : Value > > ;
2020-03-01 16:46:20 +01:00
Args args ( 1 ) ;
// Compute cartesian product of all arguments
2020-03-01 20:39:00 +01:00
for ( const auto & p : vars ) {
2020-03-01 16:46:20 +01:00
if ( p . second . empty ( ) )
continue ;
args . back ( ) [ p . first ] = p . second . front ( ) ;
}
2020-03-01 20:39:00 +01:00
for ( const auto & p : vars ) {
2021-08-27 05:46:33 +02:00
if ( args . size ( ) > 256 )
return false ;
if ( p . second . empty ( ) )
continue ;
2020-03-01 16:46:20 +01:00
std : : for_each ( std : : next ( p . second . begin ( ) ) , p . second . end ( ) , [ & ] ( const ValueFlow : : Value & value ) {
Args new_args ;
2020-03-01 20:39:00 +01:00
for ( auto arg : args ) {
2020-04-01 22:33:09 +02:00
if ( value . path ! = 0 ) {
2020-04-04 10:31:38 +02:00
for ( const auto & q : arg ) {
2020-04-01 22:33:09 +02:00
if ( q . second . path = = 0 )
continue ;
if ( q . second . path ! = value . path )
return ;
}
}
2020-03-01 16:46:20 +01:00
arg [ p . first ] = value ;
new_args . push_back ( arg ) ;
}
std : : copy ( new_args . begin ( ) , new_args . end ( ) , std : : back_inserter ( args ) ) ;
} ) ;
}
2020-03-01 20:39:00 +01:00
for ( const auto & arg : args ) {
2020-03-01 16:46:20 +01:00
if ( arg . empty ( ) )
continue ;
bool skip = false ;
// Make sure all arguments are the same path
MathLib : : bigint path = arg . begin ( ) - > second . path ;
2020-03-01 20:39:00 +01:00
for ( const auto & p : arg ) {
2020-03-01 16:46:20 +01:00
if ( p . second . path ! = path ) {
skip = true ;
break ;
}
}
if ( skip )
continue ;
2021-08-27 05:46:33 +02:00
f ( arg ) ;
}
return true ;
}
static void valueFlowInjectParameter ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * errorLogger ,
const Settings * settings ,
const Scope * functionScope ,
const std : : unordered_map < const Variable * , std : : list < ValueFlow : : Value > > & vars )
{
bool r = productParams ( vars , [ & ] ( const std : : unordered_map < const Variable * , ValueFlow : : Value > & arg ) {
2021-01-11 08:00:13 +01:00
MultiValueFlowAnalyzer a ( arg , tokenlist , symboldatabase ) ;
2020-03-01 16:46:20 +01:00
valueFlowGenericForward ( const_cast < Token * > ( functionScope - > bodyStart ) , functionScope - > bodyEnd , a , settings ) ;
2021-08-27 05:46:33 +02:00
} ) ;
if ( ! r ) {
std : : string fname = " <unknown> " ;
Function * f = functionScope - > function ;
if ( f )
fname = f - > name ( ) ;
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , functionScope - > bodyStart , " Too many argument passed to " + fname ) ;
2020-03-01 16:46:20 +01:00
}
}
2021-07-18 07:46:31 +02:00
static void valueFlowInjectParameter ( TokenList * tokenlist ,
const Settings * settings ,
const Variable * arg ,
const Scope * functionScope ,
const std : : list < ValueFlow : : Value > & argvalues )
2015-02-01 15:05:00 +01:00
{
// Is argument passed by value or const reference, and is it a known non-class type?
if ( arg - > isReference ( ) & & ! arg - > isConst ( ) & & ! arg - > isClass ( ) )
return ;
// Set value in function scope..
2021-01-27 19:49:13 +01:00
const nonneg int varid2 = arg - > declarationId ( ) ;
2015-02-01 15:05:00 +01:00
if ( ! varid2 )
return ;
2021-07-18 07:46:31 +02:00
valueFlowForward ( const_cast < Token * > ( functionScope - > bodyStart - > next ( ) ) ,
functionScope - > bodyEnd ,
arg - > nameToken ( ) ,
argvalues ,
tokenlist ,
settings ) ;
2015-02-01 15:05:00 +01:00
}
2015-07-20 19:45:38 +02:00
static void valueFlowSwitchVariable ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
{
2018-07-14 22:26:22 +02:00
for ( const Scope & scope : symboldatabase - > scopeList ) {
if ( scope . type ! = Scope : : ScopeType : : eSwitch )
2015-07-20 19:45:38 +02:00
continue ;
2018-07-14 22:26:22 +02:00
if ( ! Token : : Match ( scope . classDef , " switch ( %var% ) { " ) )
2015-07-20 19:45:38 +02:00
continue ;
2018-07-14 22:26:22 +02:00
const Token * vartok = scope . classDef - > tokAt ( 2 ) ;
2015-08-12 09:57:36 +02:00
const Variable * var = vartok - > variable ( ) ;
if ( ! var )
2015-07-20 19:45:38 +02:00
continue ;
// bailout: global non-const variables
if ( ! ( var - > isLocal ( ) | | var - > isArgument ( ) ) & & ! var - > isConst ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , vartok , " switch variable " + var - > name ( ) + " is global " ) ;
continue ;
}
2018-07-14 22:26:22 +02:00
for ( Token * tok = scope . bodyStart - > next ( ) ; tok ! = scope . bodyEnd ; tok = tok - > next ( ) ) {
2015-07-20 19:45:38 +02:00
if ( tok - > str ( ) = = " { " ) {
tok = tok - > link ( ) ;
continue ;
}
if ( Token : : Match ( tok , " case %num% : " ) ) {
std : : list < ValueFlow : : Value > values ;
2018-04-11 09:44:35 +02:00
values . emplace_back ( MathLib : : toLongNumber ( tok - > next ( ) - > str ( ) ) ) ;
2015-07-29 19:54:57 +02:00
values . back ( ) . condition = tok ;
2017-10-18 22:31:44 +02:00
const std : : string info ( " case " + tok - > next ( ) - > str ( ) + " : " + vartok - > str ( ) + " is " + tok - > next ( ) - > str ( ) + " here. " ) ;
2018-04-11 09:44:35 +02:00
values . back ( ) . errorPath . emplace_back ( tok , info ) ;
2017-10-20 17:31:58 +02:00
bool known = false ;
2017-10-18 22:31:44 +02:00
if ( ( Token : : simpleMatch ( tok - > previous ( ) , " { " ) | | Token : : simpleMatch ( tok - > tokAt ( - 2 ) , " break ; " ) ) & & ! Token : : Match ( tok - > tokAt ( 3 ) , " ;| case " ) )
2017-10-20 17:31:58 +02:00
known = true ;
2015-07-27 14:57:02 +02:00
while ( Token : : Match ( tok - > tokAt ( 3 ) , " ;| case %num% : " ) ) {
2017-10-20 22:11:12 +02:00
known = false ;
2015-07-20 19:45:38 +02:00
tok = tok - > tokAt ( 3 ) ;
2015-07-27 14:57:02 +02:00
if ( ! tok - > isName ( ) )
tok = tok - > next ( ) ;
2018-04-11 09:44:35 +02:00
values . emplace_back ( MathLib : : toLongNumber ( tok - > next ( ) - > str ( ) ) ) ;
2015-07-29 19:54:57 +02:00
values . back ( ) . condition = tok ;
2017-10-18 22:31:44 +02:00
const std : : string info2 ( " case " + tok - > next ( ) - > str ( ) + " : " + vartok - > str ( ) + " is " + tok - > next ( ) - > str ( ) + " here. " ) ;
2018-04-11 09:44:35 +02:00
values . back ( ) . errorPath . emplace_back ( tok , info2 ) ;
2015-07-20 19:45:38 +02:00
}
for ( std : : list < ValueFlow : : Value > : : const_iterator val = values . begin ( ) ; val ! = values . end ( ) ; + + val ) {
valueFlowReverse ( tokenlist ,
2018-07-14 22:26:22 +02:00
const_cast < Token * > ( scope . classDef ) ,
2015-07-20 19:45:38 +02:00
vartok ,
* val ,
ValueFlow : : Value ( ) ,
errorLogger ,
settings ) ;
}
2017-10-20 17:31:58 +02:00
if ( vartok - > variable ( ) - > scope ( ) ) {
2017-10-20 22:11:12 +02:00
if ( known )
values . back ( ) . setKnown ( ) ;
2019-10-18 16:16:56 +02:00
// FIXME We must check if there is a return. See #9276
/*
2021-08-07 20:51:18 +02:00
valueFlowForwardVariable ( tok - > tokAt ( 3 ) ,
2019-09-26 10:32:25 +02:00
vartok - > variable ( ) - > scope ( ) - > bodyEnd ,
vartok - > variable ( ) ,
vartok - > varId ( ) ,
values ,
values . back ( ) . isKnown ( ) ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2021-08-07 20:51:18 +02:00
*/
2017-10-20 22:11:12 +02:00
}
2015-07-20 19:45:38 +02:00
}
}
}
}
2018-11-06 06:38:26 +01:00
static std : : list < ValueFlow : : Value > getFunctionArgumentValues ( const Token * argtok )
{
std : : list < ValueFlow : : Value > argvalues ( argtok - > values ( ) ) ;
2019-09-21 08:19:54 +02:00
removeImpossible ( argvalues ) ;
2018-11-06 07:02:20 +01:00
if ( argvalues . empty ( ) & & Token : : Match ( argtok , " %comp%|%oror%|&&|! " ) ) {
argvalues . emplace_back ( 0 ) ;
argvalues . emplace_back ( 1 ) ;
2018-11-06 06:38:26 +01:00
}
return argvalues ;
}
2018-11-04 20:52:12 +01:00
static void valueFlowLibraryFunction ( Token * tok , const std : : string & returnValue , const Settings * settings )
{
2021-08-27 05:46:33 +02:00
std : : unordered_map < nonneg int , std : : list < ValueFlow : : Value > > argValues ;
int argn = 1 ;
2018-11-04 20:52:12 +01:00
for ( const Token * argtok : getArguments ( tok - > previous ( ) ) ) {
2021-08-27 05:46:33 +02:00
argValues [ argn ] = getFunctionArgumentValues ( argtok ) ;
argn + + ;
2018-11-04 20:52:12 +01:00
}
2018-11-11 16:41:01 +01:00
if ( returnValue . find ( " arg " ) ! = std : : string : : npos & & argValues . empty ( ) )
return ;
2021-08-27 05:46:33 +02:00
productParams ( argValues , [ & ] ( const std : : unordered_map < nonneg int , ValueFlow : : Value > & arg ) {
2022-04-06 06:35:38 +02:00
ValueFlow : : Value value = evaluateLibraryFunction ( arg , returnValue , settings ) ;
if ( value . isUninitValue ( ) )
2021-08-27 05:46:33 +02:00
return ;
2022-04-06 06:35:38 +02:00
ValueFlow : : Value : : ValueKind kind = ValueFlow : : Value : : ValueKind : : Known ;
2021-08-27 05:46:33 +02:00
for ( auto & & p : arg ) {
if ( p . second . isPossible ( ) )
2022-04-06 06:35:38 +02:00
kind = p . second . valueKind ;
2021-08-27 05:46:33 +02:00
if ( p . second . isInconclusive ( ) ) {
2022-04-06 06:35:38 +02:00
kind = p . second . valueKind ;
2021-08-27 05:46:33 +02:00
break ;
}
}
2022-04-06 06:35:38 +02:00
if ( value . isImpossible ( ) & & kind ! = ValueFlow : : Value : : ValueKind : : Known )
return ;
if ( ! value . isImpossible ( ) )
value . valueKind = kind ;
2021-08-27 05:46:33 +02:00
setTokenValue ( tok , value , settings ) ;
} ) ;
2016-10-17 13:05:19 +02:00
}
2021-11-01 19:21:03 +01:00
template < class Iterator >
struct IteratorRange
{
Iterator mBegin ;
Iterator mEnd ;
Iterator begin ( ) const {
return mBegin ;
}
Iterator end ( ) const {
return mEnd ;
}
} ;
template < class Iterator >
IteratorRange < Iterator > MakeIteratorRange ( Iterator start , Iterator last )
{
return { start , last } ;
}
2020-02-17 10:31:08 +01:00
static void valueFlowSubFunction ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-01-06 16:37:52 +01:00
{
2021-05-22 08:20:09 +02:00
int id = 0 ;
2021-11-01 19:21:03 +01:00
for ( const Scope * scope : MakeIteratorRange ( symboldatabase - > functionScopes . rbegin ( ) , symboldatabase - > functionScopes . rend ( ) ) ) {
2020-02-17 10:31:08 +01:00
const Function * function = scope - > function ;
if ( ! function )
2014-01-08 16:17:47 +01:00
continue ;
2020-02-17 10:31:08 +01:00
for ( const Token * tok = scope - > bodyStart ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : Match ( tok , " %name% ( " ) )
continue ;
2014-01-08 16:17:47 +01:00
2020-02-17 10:31:08 +01:00
const Function * const calledFunction = tok - > function ( ) ;
if ( ! calledFunction ) {
// library function?
const std : : string & returnValue ( settings - > library . returnValue ( tok ) ) ;
if ( ! returnValue . empty ( ) )
valueFlowLibraryFunction ( tok - > next ( ) , returnValue , settings ) ;
continue ;
}
2014-01-06 16:37:52 +01:00
2020-02-17 10:31:08 +01:00
const Scope * const calledFunctionScope = calledFunction - > functionScope ;
if ( ! calledFunctionScope )
continue ;
2014-01-06 16:37:52 +01:00
2020-02-17 10:31:08 +01:00
id + + ;
2020-03-01 16:46:20 +01:00
std : : unordered_map < const Variable * , std : : list < ValueFlow : : Value > > argvars ;
2020-02-17 10:31:08 +01:00
// TODO: Rewrite this. It does not work well to inject 1 argument at a time.
const std : : vector < const Token * > & callArguments = getArguments ( tok ) ;
for ( int argnr = 0U ; argnr < callArguments . size ( ) ; + + argnr ) {
const Token * argtok = callArguments [ argnr ] ;
// Get function argument
const Variable * const argvar = calledFunction - > getArgumentVar ( argnr ) ;
if ( ! argvar )
break ;
2014-08-06 06:33:06 +02:00
2020-02-17 10:31:08 +01:00
// passing value(s) to function
std : : list < ValueFlow : : Value > argvalues ( getFunctionArgumentValues ( argtok ) ) ;
2015-07-16 20:49:14 +02:00
2020-09-15 07:11:52 +02:00
// Remove non-local lifetimes
argvalues . remove_if ( [ ] ( const ValueFlow : : Value & v ) {
if ( v . isLifetimeValue ( ) )
return ! v . isLocalLifetimeValue ( ) & & ! v . isSubFunctionLifetimeValue ( ) ;
return false ;
} ) ;
2021-07-01 22:08:00 +02:00
// Remove uninit values if argument is passed by value
if ( argtok - > variable ( ) & & ! argtok - > variable ( ) - > isPointer ( ) & & argvalues . size ( ) = = 1 & & argvalues . front ( ) . isUninitValue ( ) ) {
if ( CheckUninitVar : : isVariableUsage ( tokenlist - > isCPP ( ) , argtok , settings - > library , false , CheckUninitVar : : Alloc : : NO_ALLOC , 0 ) )
continue ;
}
2018-11-11 16:43:54 +01:00
2020-02-17 10:31:08 +01:00
if ( argvalues . empty ( ) )
continue ;
2017-05-19 14:34:59 +02:00
2020-02-17 10:31:08 +01:00
// Error path..
for ( ValueFlow : : Value & v : argvalues ) {
const std : : string nr = MathLib : : toString ( argnr + 1 ) + getOrdinalText ( argnr + 1 ) ;
v . errorPath . emplace_back ( argtok ,
" Calling function ' " +
calledFunction - > name ( ) +
" ', " +
nr +
" argument ' " +
argtok - > expressionString ( ) +
" ' value is " +
v . infoString ( ) ) ;
2021-05-22 08:20:09 +02:00
v . path = 256 * v . path + id % 256 ;
2020-09-04 18:56:34 +02:00
// Change scope of lifetime values
if ( v . isLifetimeValue ( ) )
v . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : SubFunction ;
2020-02-17 10:31:08 +01:00
}
2017-05-15 20:05:11 +02:00
2020-02-17 10:31:08 +01:00
// passed values are not "known"..
lowerToPossible ( argvalues ) ;
2015-07-16 20:49:14 +02:00
2020-03-01 16:46:20 +01:00
argvars [ argvar ] = argvalues ;
2020-02-17 10:31:08 +01:00
}
2021-01-11 08:00:13 +01:00
valueFlowInjectParameter ( tokenlist , symboldatabase , errorLogger , settings , calledFunctionScope , argvars ) ;
2015-02-01 15:05:00 +01:00
}
}
}
2014-08-06 06:33:06 +02:00
2021-07-18 07:46:31 +02:00
static void valueFlowFunctionDefaultParameter ( TokenList * tokenlist , SymbolDatabase * symboldatabase , const Settings * settings )
2015-02-01 15:05:00 +01:00
{
if ( ! tokenlist - > isCPP ( ) )
return ;
2014-08-06 06:33:06 +02:00
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2015-02-01 15:05:00 +01:00
const Function * function = scope - > function ;
if ( ! function )
continue ;
for ( std : : size_t arg = function - > minArgCount ( ) ; arg < function - > argCount ( ) ; arg + + ) {
const Variable * var = function - > getArgumentVar ( arg ) ;
if ( var & & var - > hasDefault ( ) & & Token : : Match ( var - > nameToken ( ) , " %var% = %num%|%str% [,)] " ) ) {
2017-03-27 18:48:34 +02:00
const std : : list < ValueFlow : : Value > & values = var - > nameToken ( ) - > tokAt ( 2 ) - > values ( ) ;
2015-07-17 16:05:07 +02:00
std : : list < ValueFlow : : Value > argvalues ;
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & value : values ) {
ValueFlow : : Value v ( value ) ;
2015-07-17 16:05:07 +02:00
v . defaultArg = true ;
2015-07-25 19:36:29 +02:00
v . changeKnownToPossible ( ) ;
if ( v . isPossible ( ) )
2015-07-17 16:05:07 +02:00
argvalues . push_back ( v ) ;
}
if ( ! argvalues . empty ( ) )
2021-07-18 07:46:31 +02:00
valueFlowInjectParameter ( tokenlist , settings , var , scope , argvalues ) ;
2014-01-06 16:37:52 +01:00
}
}
}
}
2016-10-22 12:24:15 +02:00
static bool isKnown ( const Token * tok )
2014-06-29 18:04:38 +02:00
{
2016-10-22 12:24:15 +02:00
return tok & & tok - > hasKnownIntValue ( ) ;
2014-06-29 18:04:38 +02:00
}
2016-11-05 09:59:48 +01:00
static void valueFlowFunctionReturn ( TokenList * tokenlist , ErrorLogger * errorLogger )
2014-06-29 18:04:38 +02:00
{
2016-10-22 17:22:57 +02:00
for ( Token * tok = tokenlist - > back ( ) ; tok ; tok = tok - > previous ( ) ) {
2022-04-07 06:49:25 +02:00
if ( tok - > str ( ) ! = " ( " | | ! tok - > astOperand1 ( ) )
continue ;
const Function * function = nullptr ;
if ( Token : : Match ( tok - > previous ( ) , " %name% ( " ) )
function = tok - > previous ( ) - > function ( ) ;
else
function = tok - > astOperand1 ( ) - > function ( ) ;
if ( ! function )
2014-06-29 18:04:38 +02:00
continue ;
2022-04-26 17:57:06 +02:00
// TODO: Check if member variable is a pointer or reference
if ( function - > isImplicitlyVirtual ( ) & & ! function - > hasFinalSpecifier ( ) )
continue ;
2014-06-29 18:04:38 +02:00
2018-11-03 15:53:24 +01:00
if ( tok - > hasKnownValue ( ) )
continue ;
2014-06-29 18:04:38 +02:00
// Arguments..
std : : vector < MathLib : : bigint > parvalues ;
2016-10-20 16:10:55 +02:00
if ( tok - > astOperand2 ( ) ) {
2014-06-29 18:04:38 +02:00
const Token * partok = tok - > astOperand2 ( ) ;
2016-10-22 12:24:15 +02:00
while ( partok & & partok - > str ( ) = = " , " & & isKnown ( partok - > astOperand2 ( ) ) )
2014-06-29 18:04:38 +02:00
partok = partok - > astOperand1 ( ) ;
2016-10-22 12:24:15 +02:00
if ( ! isKnown ( partok ) )
2014-06-29 18:04:38 +02:00
continue ;
2017-03-27 18:48:34 +02:00
parvalues . push_back ( partok - > values ( ) . front ( ) . intvalue ) ;
2014-06-29 18:04:38 +02:00
partok = partok - > astParent ( ) ;
while ( partok & & partok - > str ( ) = = " , " ) {
2017-03-27 18:48:34 +02:00
parvalues . push_back ( partok - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ) ;
2014-06-29 18:04:38 +02:00
partok = partok - > astParent ( ) ;
}
if ( partok ! = tok )
continue ;
}
2015-02-01 12:33:53 +01:00
// Get scope and args of function
const Scope * const functionScope = function - > functionScope ;
2018-04-27 22:36:30 +02:00
if ( ! functionScope | | ! Token : : simpleMatch ( functionScope - > bodyStart , " { return " ) ) {
if ( functionScope & & tokenlist - > getSettings ( ) - > debugwarnings & & Token : : findsimplematch ( functionScope - > bodyStart , " return " , functionScope - > bodyEnd ) )
2015-02-01 12:33:53 +01:00
bailout ( tokenlist , errorLogger , tok , " function return; nontrivial function body " ) ;
continue ;
}
2015-09-12 19:49:02 +02:00
ProgramMemory programMemory ;
2014-07-07 21:25:30 +02:00
for ( std : : size_t i = 0 ; i < parvalues . size ( ) ; + + i ) {
2014-06-29 18:04:38 +02:00
const Variable * const arg = function - > getArgumentVar ( i ) ;
2015-01-31 10:50:39 +01:00
if ( ! arg | | ! Token : : Match ( arg - > typeStartToken ( ) , " %type% %name% ,|) " ) ) {
2016-11-05 09:59:48 +01:00
if ( tokenlist - > getSettings ( ) - > debugwarnings )
2014-06-29 18:04:38 +02:00
bailout ( tokenlist , errorLogger , tok , " function return; unhandled argument type " ) ;
programMemory . clear ( ) ;
break ;
}
2022-02-06 20:15:45 +01:00
programMemory . setIntValue ( arg - > nameToken ( ) , parvalues [ i ] ) ;
2014-06-29 18:04:38 +02:00
}
2016-10-20 16:10:55 +02:00
if ( programMemory . empty ( ) & & ! parvalues . empty ( ) )
2014-06-29 18:04:38 +02:00
continue ;
// Determine return value of subfunction..
MathLib : : bigint result = 0 ;
bool error = false ;
2018-04-27 22:36:30 +02:00
execute ( functionScope - > bodyStart - > next ( ) - > astOperand1 ( ) ,
2014-06-29 18:04:38 +02:00
& programMemory ,
& result ,
& error ) ;
2016-10-20 16:10:55 +02:00
if ( ! error ) {
ValueFlow : : Value v ( result ) ;
2019-07-04 12:32:32 +02:00
if ( function - > hasVirtualSpecifier ( ) )
2016-12-11 10:42:39 +01:00
v . setPossible ( ) ;
else
v . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , v , tokenlist - > getSettings ( ) ) ;
2016-10-20 16:10:55 +02:00
}
2014-06-29 18:04:38 +02:00
}
}
2021-10-21 14:44:48 +02:00
static bool needsInitialization ( const Variable * var , bool cpp )
{
if ( ! var )
return false ;
if ( var - > isPointer ( ) )
return true ;
if ( var - > type ( ) & & var - > type ( ) - > isUnionType ( ) )
return false ;
if ( ! cpp )
return true ;
if ( var - > type ( ) & & var - > type ( ) - > needInitialization = = Type : : NeedInitialization : : True )
return true ;
2022-03-24 06:35:44 +01:00
if ( var - > valueType ( ) & & ( var - > valueType ( ) - > isPrimitive ( ) | | var - > valueType ( ) - > type = = ValueType : : Type : : POD ) )
2021-10-21 14:44:48 +02:00
return true ;
return false ;
}
2021-10-30 07:43:37 +02:00
static void addToErrorPath ( ValueFlow : : Value & value , const ValueFlow : : Value & from )
{
std : : unordered_set < const Token * > locations ;
2022-02-22 07:21:26 +01:00
std : : transform ( value . errorPath . begin ( ) ,
value . errorPath . end ( ) ,
std : : inserter ( locations , locations . begin ( ) ) ,
[ ] ( const ErrorPathItem & e ) {
return e . first ;
} ) ;
2021-10-30 07:43:37 +02:00
if ( from . condition & & ! value . condition )
value . condition = from . condition ;
std : : copy_if ( from . errorPath . begin ( ) ,
from . errorPath . end ( ) ,
std : : back_inserter ( value . errorPath ) ,
[ & ] ( const ErrorPathItem & e ) {
return locations . insert ( e . first ) . second ;
} ) ;
}
2021-07-18 07:46:31 +02:00
static void valueFlowUninit ( TokenList * tokenlist , SymbolDatabase * /*symbolDatabase*/ , const Settings * settings )
2017-04-23 18:05:14 +02:00
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > scope ( ) - > isExecutable ( ) )
continue ;
2022-03-24 06:35:44 +01:00
if ( ! Token : : Match ( tok , " %var% ;|[ " ) )
2017-04-23 18:05:14 +02:00
continue ;
2022-03-24 06:35:44 +01:00
const Variable * var = tok - > variable ( ) ;
if ( ! var )
2017-04-23 18:05:14 +02:00
continue ;
2022-03-24 06:35:44 +01:00
if ( var - > nameToken ( ) ! = tok | | var - > isInit ( ) )
2021-10-21 14:44:48 +02:00
continue ;
2022-03-24 06:35:44 +01:00
if ( ! needsInitialization ( var , tokenlist - > isCPP ( ) ) )
2017-04-23 18:05:14 +02:00
continue ;
2022-03-24 06:35:44 +01:00
if ( ! var - > isLocal ( ) | | var - > isStatic ( ) | | var - > isExtern ( ) | | var - > isReference ( ) | | var - > isThrow ( ) )
2019-08-15 10:44:55 +02:00
continue ;
2017-04-23 18:05:14 +02:00
ValueFlow : : Value uninitValue ;
uninitValue . setKnown ( ) ;
2021-01-02 09:30:00 +01:00
uninitValue . valueType = ValueFlow : : Value : : ValueType : : UNINIT ;
2022-03-24 06:35:44 +01:00
uninitValue . tokvalue = tok ;
if ( var - > isArray ( ) )
2022-03-27 10:01:58 +02:00
uninitValue . indirect = var - > dimensions ( ) . size ( ) ;
2017-04-23 18:05:14 +02:00
2021-10-21 14:44:48 +02:00
bool partial = false ;
2021-10-30 07:43:37 +02:00
std : : map < Token * , ValueFlow : : Value > partialReads ;
2021-10-21 14:44:48 +02:00
if ( const Scope * scope = var - > typeScope ( ) ) {
if ( Token : : findsimplematch ( scope - > bodyStart , " union " , scope - > bodyEnd ) )
continue ;
for ( const Variable & memVar : scope - > varlist ) {
if ( ! memVar . isPublic ( ) )
continue ;
2022-01-13 17:24:26 +01:00
// Skip array since we can't track partial initialization from nested subexpressions
2022-01-08 10:47:28 +01:00
if ( memVar . isArray ( ) )
continue ;
2021-10-21 14:44:48 +02:00
if ( ! needsInitialization ( & memVar , tokenlist - > isCPP ( ) ) ) {
partial = true ;
continue ;
}
2022-03-24 06:35:44 +01:00
MemberExpressionAnalyzer analyzer ( memVar . nameToken ( ) - > str ( ) , tok , uninitValue , tokenlist ) ;
valueFlowGenericForward ( tok - > next ( ) , tok - > scope ( ) - > bodyEnd , analyzer , settings ) ;
2021-10-30 07:43:37 +02:00
for ( auto & & p : * analyzer . partialReads ) {
Token * tok2 = p . first ;
const ValueFlow : : Value & v = p . second ;
// Try to insert into map
auto pp = partialReads . insert ( std : : make_pair ( tok2 , v ) ) ;
ValueFlow : : Value & v2 = pp . first - > second ;
bool inserted = pp . second ;
// Merge the two values if it is already in map
if ( ! inserted ) {
if ( v . valueType ! = v2 . valueType )
continue ;
addToErrorPath ( v2 , v ) ;
}
v2 . subexpressions . push_back ( memVar . nameToken ( ) - > str ( ) ) ;
}
2021-10-21 14:44:48 +02:00
}
}
2021-10-30 07:43:37 +02:00
for ( auto & & p : partialReads ) {
Token * tok2 = p . first ;
const ValueFlow : : Value & v = p . second ;
setTokenValue ( tok2 , v , settings ) ;
}
2021-10-21 14:44:48 +02:00
if ( partial )
continue ;
2022-03-24 06:35:44 +01:00
valueFlowForward ( tok - > next ( ) , tok - > scope ( ) - > bodyEnd , var - > nameToken ( ) , { uninitValue } , tokenlist , settings ) ;
2017-04-23 18:05:14 +02:00
}
}
2021-08-23 09:03:48 +02:00
static bool isContainerSizeChanged ( nonneg int varId ,
const Token * start ,
const Token * end ,
const Settings * settings = nullptr ,
int depth = 20 ) ;
2018-11-02 20:10:40 +01:00
2021-08-23 09:03:48 +02:00
static bool isContainerSizeChangedByFunction ( const Token * tok , const Settings * settings = nullptr , int depth = 20 )
2018-08-13 06:54:18 +02:00
{
2022-01-26 06:42:41 +01:00
if ( ! tok - > valueType ( ) )
return false ;
if ( ! astIsContainer ( tok ) )
2018-09-02 14:08:34 +02:00
return false ;
2019-09-06 21:18:45 +02:00
// If we are accessing an element then we are not changing the container size
if ( Token : : Match ( tok , " %name% . %name% ( " ) ) {
2022-01-26 06:42:41 +01:00
Library : : Container : : Yield yield = getLibraryContainer ( tok ) - > getYield ( tok - > strAt ( 2 ) ) ;
2019-09-06 21:18:45 +02:00
if ( yield ! = Library : : Container : : Yield : : NO_YIELD )
return false ;
}
if ( Token : : simpleMatch ( tok - > astParent ( ) , " [ " ) )
return false ;
// address of variable
2019-10-18 08:21:07 +02:00
const bool addressOf = tok - > valueType ( ) - > pointer | | ( tok - > astParent ( ) & & tok - > astParent ( ) - > isUnaryOp ( " & " ) ) ;
2019-09-06 21:18:45 +02:00
int narg ;
const Token * ftok = getTokenArgumentFunction ( tok , narg ) ;
if ( ! ftok )
return false ; // not a function => variable not changed
const Function * fun = ftok - > function ( ) ;
2020-10-09 17:21:27 +02:00
if ( fun & & ! fun - > hasVirtualSpecifier ( ) ) {
2019-09-06 21:18:45 +02:00
const Variable * arg = fun - > getArgumentVar ( narg ) ;
2019-09-10 19:39:44 +02:00
if ( arg ) {
if ( ! arg - > isReference ( ) & & ! addressOf )
return false ;
2019-12-15 20:10:12 +01:00
if ( ! addressOf & & arg - > isConst ( ) )
return false ;
if ( arg - > valueType ( ) & & arg - > valueType ( ) - > constness = = 1 )
2019-09-06 21:18:45 +02:00
return false ;
2019-09-10 19:39:44 +02:00
const Scope * scope = fun - > functionScope ;
if ( scope ) {
// Argument not used
if ( ! arg - > nameToken ( ) )
return false ;
if ( depth > 0 )
2021-08-23 09:03:48 +02:00
return isContainerSizeChanged (
arg - > declarationId ( ) , scope - > bodyStart , scope - > bodyEnd , settings , depth - 1 ) ;
2019-09-10 19:39:44 +02:00
}
2019-10-18 08:21:07 +02:00
// Don't know => Safe guess
return true ;
2019-09-06 21:18:45 +02:00
}
}
bool inconclusive = false ;
2021-08-23 09:03:48 +02:00
const bool isChanged = isVariableChangedByFunctionCall ( tok , 0 , settings , & inconclusive ) ;
2019-09-06 21:18:45 +02:00
return ( isChanged | | inconclusive ) ;
2018-08-13 06:54:18 +02:00
}
2021-07-18 07:46:31 +02:00
struct ContainerExpressionAnalyzer : ExpressionAnalyzer {
ContainerExpressionAnalyzer ( ) : ExpressionAnalyzer ( ) { }
2020-08-08 07:10:03 +02:00
2021-07-18 07:46:31 +02:00
ContainerExpressionAnalyzer ( const Token * expr , const ValueFlow : : Value & val , const TokenList * t )
: ExpressionAnalyzer ( expr , val , t )
2020-11-10 16:00:55 +01:00
{ }
2020-08-08 07:10:03 +02:00
2022-02-10 23:02:24 +01:00
virtual bool match ( const Token * tok ) const override {
2021-07-18 07:46:31 +02:00
return tok - > exprId ( ) = = expr - > exprId ( ) | | ( astIsIterator ( tok ) & & isAliasOf ( tok , expr - > exprId ( ) ) ) ;
2020-08-26 21:05:17 +02:00
}
2022-02-10 23:02:24 +01:00
virtual Action isWritable ( const Token * tok , Direction d ) const override {
2020-08-26 21:05:17 +02:00
if ( astIsIterator ( tok ) )
return Action : : None ;
2020-11-10 16:00:55 +01:00
if ( d = = Direction : : Reverse )
return Action : : None ;
2021-03-31 22:07:20 +02:00
if ( ! getValue ( tok ) )
2020-08-08 07:10:03 +02:00
return Action : : None ;
2022-01-26 06:42:41 +01:00
if ( ! tok - > valueType ( ) )
return Action : : None ;
if ( ! astIsContainer ( tok ) )
2020-08-08 07:10:03 +02:00
return Action : : None ;
const Token * parent = tok - > astParent ( ) ;
2022-01-26 06:42:41 +01:00
const Library : : Container * container = getLibraryContainer ( tok ) ;
2020-08-08 07:10:03 +02:00
2022-01-26 06:42:41 +01:00
if ( container - > stdStringLike & & Token : : simpleMatch ( parent , " += " ) & & astIsLHS ( tok ) & & parent - > astOperand2 ( ) ) {
2020-08-08 07:10:03 +02:00
const Token * rhs = parent - > astOperand2 ( ) ;
if ( rhs - > tokType ( ) = = Token : : eString )
2021-01-21 19:50:57 +01:00
return Action : : Read | Action : : Write | Action : : Incremental ;
2022-01-26 06:42:41 +01:00
const Library : : Container * rhsContainer = getLibraryContainer ( rhs ) ;
if ( rhsContainer & & rhsContainer - > stdStringLike ) {
2020-08-23 17:17:33 +02:00
if ( std : : any_of ( rhs - > values ( ) . begin ( ) , rhs - > values ( ) . end ( ) , [ & ] ( const ValueFlow : : Value & rhsval ) {
2021-08-07 20:51:18 +02:00
return rhsval . isKnown ( ) & & rhsval . isContainerSizeValue ( ) ;
2020-08-23 17:17:33 +02:00
} ) )
2021-08-07 20:51:18 +02:00
return Action : : Read | Action : : Write | Action : : Incremental ;
2020-08-08 07:10:03 +02:00
}
2020-08-10 05:52:03 +02:00
} else if ( Token : : Match ( tok , " %name% . %name% ( " ) ) {
2022-01-26 06:42:41 +01:00
Library : : Container : : Action action = container - > getAction ( tok - > strAt ( 2 ) ) ;
2021-02-02 14:57:48 +01:00
if ( action = = Library : : Container : : Action : : PUSH | | action = = Library : : Container : : Action : : POP ) {
std : : vector < const Token * > args = getArguments ( tok - > tokAt ( 3 ) ) ;
if ( args . size ( ) < 2 )
return Action : : Read | Action : : Write | Action : : Incremental ;
}
2020-08-08 07:10:03 +02:00
}
return Action : : None ;
}
2022-02-10 23:02:24 +01:00
virtual void writeValue ( ValueFlow : : Value * val , const Token * tok , Direction d ) const override {
2020-11-10 16:00:55 +01:00
if ( d = = Direction : : Reverse )
return ;
2021-03-31 22:07:20 +02:00
if ( ! val )
2020-08-08 07:10:03 +02:00
return ;
if ( ! tok - > astParent ( ) )
return ;
2022-01-26 06:42:41 +01:00
if ( ! tok - > valueType ( ) )
return ;
if ( ! astIsContainer ( tok ) )
2020-08-08 07:10:03 +02:00
return ;
2022-01-26 06:42:41 +01:00
const Token * parent = tok - > astParent ( ) ;
const Library : : Container * container = getLibraryContainer ( tok ) ;
2020-08-08 07:10:03 +02:00
2022-01-26 06:42:41 +01:00
if ( container - > stdStringLike & & Token : : simpleMatch ( parent , " += " ) & & parent - > astOperand2 ( ) ) {
2020-08-08 07:10:03 +02:00
const Token * rhs = parent - > astOperand2 ( ) ;
2022-01-26 06:42:41 +01:00
const Library : : Container * rhsContainer = getLibraryContainer ( rhs ) ;
2020-08-08 07:10:03 +02:00
if ( rhs - > tokType ( ) = = Token : : eString )
2021-03-31 22:07:20 +02:00
val - > intvalue + = Token : : getStrLength ( rhs ) ;
2022-01-26 06:42:41 +01:00
else if ( rhsContainer & & rhsContainer - > stdStringLike ) {
2020-08-08 07:10:03 +02:00
for ( const ValueFlow : : Value & rhsval : rhs - > values ( ) ) {
if ( rhsval . isKnown ( ) & & rhsval . isContainerSizeValue ( ) ) {
2021-03-31 22:07:20 +02:00
val - > intvalue + = rhsval . intvalue ;
2020-08-08 07:10:03 +02:00
}
}
}
2020-08-10 05:52:03 +02:00
} else if ( Token : : Match ( tok , " %name% . %name% ( " ) ) {
2022-01-26 06:42:41 +01:00
Library : : Container : : Action action = container - > getAction ( tok - > strAt ( 2 ) ) ;
2020-08-10 05:52:03 +02:00
if ( action = = Library : : Container : : Action : : PUSH )
2021-03-31 22:07:20 +02:00
val - > intvalue + + ;
2020-08-10 05:52:03 +02:00
if ( action = = Library : : Container : : Action : : POP )
2021-03-31 22:07:20 +02:00
val - > intvalue - - ;
2020-08-08 07:10:03 +02:00
}
}
2022-02-10 23:02:24 +01:00
virtual Action isModified ( const Token * tok ) const override {
2020-08-08 07:10:03 +02:00
Action read = Action : : Read ;
2020-10-15 19:24:13 +02:00
// An iterator won't change the container size
2020-08-26 21:05:17 +02:00
if ( astIsIterator ( tok ) )
return read ;
2020-08-08 07:10:03 +02:00
if ( Token : : Match ( tok - > astParent ( ) , " %assign% " ) & & astIsLHS ( tok ) )
return Action : : Invalid ;
if ( isLikelyStreamRead ( isCPP ( ) , tok - > astParent ( ) ) )
return Action : : Invalid ;
2021-08-23 09:03:48 +02:00
if ( astIsContainer ( tok ) & & isContainerSizeChanged ( tok , getSettings ( ) ) )
2020-08-08 07:10:03 +02:00
return Action : : Invalid ;
return read ;
}
} ;
2021-07-18 07:46:31 +02:00
static Analyzer : : Result valueFlowContainerForward ( Token * startToken ,
2021-08-07 20:51:18 +02:00
const Token * endToken ,
const Token * exprTok ,
const ValueFlow : : Value & value ,
TokenList * tokenlist )
2020-08-08 07:10:03 +02:00
{
2021-07-18 07:46:31 +02:00
ContainerExpressionAnalyzer a ( exprTok , value , tokenlist ) ;
return valueFlowGenericForward ( startToken , endToken , a , tokenlist - > getSettings ( ) ) ;
2020-08-08 07:10:03 +02:00
}
2021-07-18 07:46:31 +02:00
2021-08-01 10:13:14 +02:00
static Analyzer : : Result valueFlowContainerForwardRecursive ( Token * top ,
2021-08-02 10:51:34 +02:00
const Token * exprTok ,
const ValueFlow : : Value & value ,
TokenList * tokenlist )
2021-08-03 17:53:30 +02:00
{
ContainerExpressionAnalyzer a ( exprTok , value , tokenlist ) ;
return valueFlowGenericForward ( top , a , tokenlist - > getSettings ( ) ) ;
2021-08-01 10:13:14 +02:00
}
2021-07-18 07:46:31 +02:00
static Analyzer : : Result valueFlowContainerForward ( Token * startToken ,
2021-08-07 20:51:18 +02:00
const Token * exprTok ,
const ValueFlow : : Value & value ,
TokenList * tokenlist )
2020-08-08 07:10:03 +02:00
{
2021-07-18 07:46:31 +02:00
const Token * endToken = nullptr ;
const Function * f = Scope : : nestedInFunction ( startToken - > scope ( ) ) ;
if ( f & & f - > functionScope )
endToken = f - > functionScope - > bodyEnd ;
return valueFlowContainerForward ( startToken , endToken , exprTok , value , tokenlist ) ;
2020-08-08 07:10:03 +02:00
}
2021-01-08 22:55:04 +01:00
static void valueFlowContainerReverse ( Token * tok ,
2021-01-23 17:52:01 +01:00
const Token * const endToken ,
2021-01-08 22:55:04 +01:00
const Token * const varToken ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
const Settings * settings )
{
for ( const ValueFlow : : Value & value : values ) {
2021-07-18 07:46:31 +02:00
ContainerExpressionAnalyzer a ( varToken , value , tokenlist ) ;
2021-01-23 17:52:01 +01:00
valueFlowGenericReverse ( tok , endToken , a , settings ) ;
2021-01-08 22:55:04 +01:00
}
}
2021-12-20 07:28:40 +01:00
bool isContainerSizeChanged ( const Token * tok , const Settings * settings , int depth )
2020-08-08 07:10:03 +02:00
{
if ( ! tok )
return false ;
if ( ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > container )
return true ;
if ( Token : : Match ( tok , " %name% %assign%|<< " ) )
return true ;
2020-08-11 03:08:49 +02:00
if ( Token : : Match ( tok , " %var% [ " ) & & tok - > valueType ( ) - > container - > stdAssociativeLike )
return true ;
2020-08-08 07:10:03 +02:00
if ( Token : : Match ( tok , " %name% . %name% ( " ) ) {
Library : : Container : : Action action = tok - > valueType ( ) - > container - > getAction ( tok - > strAt ( 2 ) ) ;
Library : : Container : : Yield yield = tok - > valueType ( ) - > container - > getYield ( tok - > strAt ( 2 ) ) ;
switch ( action ) {
case Library : : Container : : Action : : RESIZE :
case Library : : Container : : Action : : CLEAR :
case Library : : Container : : Action : : PUSH :
case Library : : Container : : Action : : POP :
case Library : : Container : : Action : : CHANGE :
case Library : : Container : : Action : : INSERT :
case Library : : Container : : Action : : ERASE :
return true ;
case Library : : Container : : Action : : NO_ACTION : // might be unknown action
return yield = = Library : : Container : : Yield : : NO_YIELD ;
case Library : : Container : : Action : : FIND :
case Library : : Container : : Action : : CHANGE_CONTENT :
2021-11-26 09:50:02 +01:00
case Library : : Container : : Action : : CHANGE_INTERNAL :
2020-08-08 07:10:03 +02:00
break ;
}
}
2021-08-23 09:03:48 +02:00
if ( isContainerSizeChangedByFunction ( tok , settings , depth ) )
2020-08-08 07:10:03 +02:00
return true ;
return false ;
}
2021-08-23 09:03:48 +02:00
static bool isContainerSizeChanged ( nonneg int varId ,
const Token * start ,
const Token * end ,
const Settings * settings ,
int depth )
2018-08-12 22:23:19 +02:00
{
for ( const Token * tok = start ; tok ! = end ; tok = tok - > next ( ) ) {
if ( tok - > varId ( ) ! = varId )
continue ;
2021-08-23 09:03:48 +02:00
if ( isContainerSizeChanged ( tok , settings , depth ) )
2019-09-05 16:42:26 +02:00
return true ;
2018-08-12 22:23:19 +02:00
}
return false ;
}
2019-04-26 11:30:09 +02:00
static void valueFlowSmartPointer ( TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > scope ( ) )
continue ;
if ( ! tok - > scope ( ) - > isExecutable ( ) )
continue ;
2021-10-06 08:39:58 +02:00
if ( ! astIsSmartPointer ( tok ) )
continue ;
if ( tok - > variable ( ) & & Token : : Match ( tok , " %var% (|{|; " ) ) {
2020-09-09 04:55:53 +02:00
const Variable * var = tok - > variable ( ) ;
2020-09-09 04:54:38 +02:00
if ( ! var - > isSmartPointer ( ) )
continue ;
if ( var - > nameToken ( ) = = tok ) {
2020-09-09 04:55:53 +02:00
if ( Token : : Match ( tok , " %var% (|{ " ) & & tok - > next ( ) - > astOperand2 ( ) & &
tok - > next ( ) - > astOperand2 ( ) - > str ( ) ! = " , " ) {
Token * inTok = tok - > next ( ) - > astOperand2 ( ) ;
2020-09-09 04:54:38 +02:00
std : : list < ValueFlow : : Value > values = inTok - > values ( ) ;
const bool constValue = inTok - > isNumber ( ) ;
valueFlowForwardAssign ( inTok , var , values , constValue , true , tokenlist , errorLogger , settings ) ;
} else if ( Token : : Match ( tok , " %var% ; " ) ) {
std : : list < ValueFlow : : Value > values ;
ValueFlow : : Value v ( 0 ) ;
v . setKnown ( ) ;
values . push_back ( v ) ;
valueFlowForwardAssign ( tok , var , values , false , true , tokenlist , errorLogger , settings ) ;
}
2021-10-06 08:39:58 +02:00
}
} else if ( astIsLHS ( tok ) & & Token : : Match ( tok - > astParent ( ) , " . %name% ( " ) & &
tok - > astParent ( ) - > originalName ( ) ! = " -> " ) {
std : : vector < const Variable * > vars = getVariables ( tok ) ;
Token * ftok = tok - > astParent ( ) - > tokAt ( 2 ) ;
if ( Token : : simpleMatch ( tok - > astParent ( ) , " . reset ( " ) ) {
if ( Token : : simpleMatch ( ftok , " ( ) " ) ) {
2020-09-09 04:54:38 +02:00
std : : list < ValueFlow : : Value > values ;
ValueFlow : : Value v ( 0 ) ;
v . setKnown ( ) ;
values . push_back ( v ) ;
2021-10-06 08:39:58 +02:00
valueFlowForwardAssign ( ftok , tok , vars , values , false , tokenlist , errorLogger , settings ) ;
2020-09-09 04:54:38 +02:00
} else {
tok - > removeValues ( std : : mem_fn ( & ValueFlow : : Value : : isIntValue ) ) ;
2021-10-06 08:39:58 +02:00
Token * inTok = ftok - > astOperand2 ( ) ;
2020-09-09 04:54:38 +02:00
if ( ! inTok )
continue ;
std : : list < ValueFlow : : Value > values = inTok - > values ( ) ;
2021-10-06 08:39:58 +02:00
valueFlowForwardAssign ( inTok , tok , vars , values , false , tokenlist , errorLogger , settings ) ;
2020-09-09 04:54:38 +02:00
}
2021-10-06 08:39:58 +02:00
} else if ( Token : : simpleMatch ( tok - > astParent ( ) , " . release ( ) " ) ) {
const Token * parent = ftok - > astParent ( ) ;
2021-09-04 19:06:13 +02:00
bool hasParentReset = false ;
while ( parent ) {
2021-10-06 08:39:58 +02:00
if ( Token : : Match ( parent - > tokAt ( - 2 ) , " . release|reset ( " ) & &
parent - > tokAt ( - 2 ) - > astOperand1 ( ) - > exprId ( ) = = tok - > exprId ( ) ) {
2021-09-04 19:06:13 +02:00
hasParentReset = true ;
break ;
}
parent = parent - > astParent ( ) ;
}
if ( hasParentReset )
continue ;
2019-04-26 11:30:09 +02:00
std : : list < ValueFlow : : Value > values ;
ValueFlow : : Value v ( 0 ) ;
v . setKnown ( ) ;
values . push_back ( v ) ;
2021-10-06 08:39:58 +02:00
valueFlowForwardAssign ( ftok , tok , vars , values , false , tokenlist , errorLogger , settings ) ;
} else if ( Token : : simpleMatch ( tok - > astParent ( ) , " . get ( ) " ) ) {
ValueFlow : : Value v = makeSymbolic ( tok ) ;
setTokenValue ( tok - > astParent ( ) - > tokAt ( 2 ) , v , settings ) ;
2019-04-26 11:30:09 +02:00
}
2020-09-09 04:55:53 +02:00
} else if ( Token : : Match ( tok - > previous ( ) , " %name%|> (| { " ) && astIsSmartPointer(tok) &&
astIsSmartPointer ( tok - > astOperand1 ( ) ) ) {
2020-09-09 04:54:38 +02:00
std : : vector < const Token * > args = getArguments ( tok ) ;
if ( args . empty ( ) )
continue ;
2020-09-09 04:55:53 +02:00
for ( const ValueFlow : : Value & v : args . front ( ) - > values ( ) )
2020-09-09 04:54:38 +02:00
setTokenValue ( tok , v , settings ) ;
2019-04-26 11:30:09 +02:00
}
2019-04-26 11:30:35 +02:00
}
2019-04-26 11:30:09 +02:00
}
2020-08-21 17:23:55 +02:00
static void valueFlowIterators ( TokenList * tokenlist , const Settings * settings )
2020-08-17 23:36:45 +02:00
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > scope ( ) )
continue ;
if ( ! tok - > scope ( ) - > isExecutable ( ) )
continue ;
if ( ! astIsContainer ( tok ) )
continue ;
if ( Token : : Match ( tok - > astParent ( ) , " . %name% ( " ) ) {
2022-01-26 06:42:41 +01:00
Library : : Container : : Yield yield = getLibraryContainer ( tok ) - > getYield ( tok - > astParent ( ) - > strAt ( 1 ) ) ;
2020-08-17 23:36:45 +02:00
ValueFlow : : Value v ( 0 ) ;
v . setKnown ( ) ;
if ( yield = = Library : : Container : : Yield : : START_ITERATOR ) {
2021-01-02 09:30:00 +01:00
v . valueType = ValueFlow : : Value : : ValueType : : ITERATOR_START ;
2020-08-17 23:36:45 +02:00
setTokenValue ( tok - > astParent ( ) - > tokAt ( 2 ) , v , settings ) ;
} else if ( yield = = Library : : Container : : Yield : : END_ITERATOR ) {
2021-01-02 09:30:00 +01:00
v . valueType = ValueFlow : : Value : : ValueType : : ITERATOR_END ;
2020-08-17 23:36:45 +02:00
setTokenValue ( tok - > astParent ( ) - > tokAt ( 2 ) , v , settings ) ;
}
}
}
}
2021-07-20 11:21:47 +02:00
static std : : list < ValueFlow : : Value > getIteratorValues ( std : : list < ValueFlow : : Value > values , const ValueFlow : : Value : : ValueKind * kind = nullptr )
2020-08-28 19:29:09 +02:00
{
values . remove_if ( [ & ] ( const ValueFlow : : Value & v ) {
2020-08-31 08:46:56 +02:00
if ( kind & & v . valueKind ! = * kind )
return true ;
return ! v . isIteratorValue ( ) ;
2020-08-28 19:29:09 +02:00
} ) ;
return values ;
}
2020-12-24 20:07:46 +01:00
struct IteratorConditionHandler : SimpleConditionHandler {
2022-02-10 23:02:24 +01:00
virtual std : : vector < Condition > parse ( const Token * tok , const Settings * ) const override {
2020-12-24 20:07:46 +01:00
Condition cond ;
2020-08-28 19:29:09 +02:00
ValueFlow : : Value true_value ;
ValueFlow : : Value false_value ;
if ( Token : : Match ( tok , " ==|!= " ) ) {
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
2021-07-26 22:23:19 +02:00
return { } ;
2020-08-28 19:29:09 +02:00
2020-08-31 08:46:56 +02:00
ValueFlow : : Value : : ValueKind kind = ValueFlow : : Value : : ValueKind : : Known ;
std : : list < ValueFlow : : Value > values = getIteratorValues ( tok - > astOperand1 ( ) - > values ( ) , & kind ) ;
2020-08-28 19:29:09 +02:00
if ( ! values . empty ( ) ) {
cond . vartok = tok - > astOperand2 ( ) ;
} else {
2021-12-16 22:34:51 +01:00
values = getIteratorValues ( tok - > astOperand2 ( ) - > values ( ) , & kind ) ;
2020-08-28 19:29:09 +02:00
if ( ! values . empty ( ) )
cond . vartok = tok - > astOperand1 ( ) ;
}
2020-08-28 19:29:33 +02:00
for ( ValueFlow : : Value & v : values ) {
2020-08-28 19:29:09 +02:00
v . setPossible ( ) ;
v . assumeCondition ( tok ) ;
}
cond . true_values = values ;
cond . false_values = values ;
}
2021-07-26 22:23:19 +02:00
return { cond } ;
2020-12-24 20:07:46 +01:00
}
} ;
2020-08-28 19:29:09 +02:00
2020-08-31 08:46:56 +02:00
static void valueFlowIteratorInfer ( TokenList * tokenlist , const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > scope ( ) )
continue ;
if ( ! tok - > scope ( ) - > isExecutable ( ) )
continue ;
std : : list < ValueFlow : : Value > values = getIteratorValues ( tok - > values ( ) ) ;
values . remove_if ( [ & ] ( const ValueFlow : : Value & v ) {
if ( ! v . isImpossible ( ) )
return true ;
2021-12-15 19:31:28 +01:00
if ( ! v . condition )
return true ;
if ( v . bound ! = ValueFlow : : Value : : Bound : : Point )
return true ;
2020-08-31 08:46:56 +02:00
if ( v . isIteratorEndValue ( ) & & v . intvalue < = 0 )
return true ;
if ( v . isIteratorStartValue ( ) & & v . intvalue > = 0 )
return true ;
return false ;
} ) ;
2020-09-01 20:00:04 +02:00
for ( ValueFlow : : Value & v : values ) {
2020-08-31 08:46:56 +02:00
v . setPossible ( ) ;
if ( v . isIteratorStartValue ( ) )
v . intvalue + + ;
if ( v . isIteratorEndValue ( ) )
v . intvalue - - ;
setTokenValue ( tok , v , settings ) ;
}
}
}
2021-06-04 17:22:05 +02:00
static std : : vector < ValueFlow : : Value > getContainerValues ( const Token * tok )
2020-12-18 07:14:11 +01:00
{
2021-06-04 17:22:05 +02:00
std : : vector < ValueFlow : : Value > values ;
if ( tok ) {
std : : copy_if ( tok - > values ( ) . begin ( ) ,
tok - > values ( ) . end ( ) ,
2020-12-18 07:14:11 +01:00
std : : back_inserter ( values ) ,
std : : mem_fn ( & ValueFlow : : Value : : isContainerSizeValue ) ) ;
}
2021-06-04 17:22:05 +02:00
return values ;
}
static ValueFlow : : Value makeContainerSizeValue ( std : : size_t s , bool known = true )
{
ValueFlow : : Value value ( s ) ;
2020-12-18 07:14:11 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
2021-04-18 21:42:27 +02:00
if ( known )
value . setKnown ( ) ;
2021-06-04 17:22:05 +02:00
return value ;
}
static std : : vector < ValueFlow : : Value > makeContainerSizeValue ( const Token * tok , bool known = true )
{
if ( tok - > hasKnownIntValue ( ) )
return { makeContainerSizeValue ( tok - > values ( ) . front ( ) . intvalue , known ) } ;
return { } ;
}
2022-04-06 06:25:28 +02:00
static std : : vector < ValueFlow : : Value > getContainerSizeFromConstructorArgs ( const std : : vector < const Token * > & args ,
const Library : : Container * container ,
bool known )
2021-06-04 17:22:05 +02:00
{
2022-04-06 06:25:28 +02:00
if ( astIsIntegral ( args [ 0 ] , false ) ) { // { count, i } or { count }
if ( args . size ( ) = = 1 | | ( args . size ( ) > 1 & & ! astIsIntegral ( args [ 1 ] , false ) ) )
return { makeContainerSizeValue ( args [ 0 ] , known ) } ;
} else if ( astIsContainer ( args [ 0 ] ) & & args . size ( ) = = 1 ) { // copy constructor
return getContainerValues ( args [ 0 ] ) ;
} else if ( isIteratorPair ( args ) ) {
std : : vector < ValueFlow : : Value > result = getContainerValues ( args [ 0 ] ) ;
if ( ! result . empty ( ) )
return result ;
// (ptr, ptr + size)
if ( astIsPointer ( args [ 0 ] ) & & args [ 0 ] - > exprId ( ) ! = 0 ) {
// (ptr, ptr) is empty
// TODO: Use lifetime values to check if it points to the same address
if ( args [ 0 ] - > exprId ( ) = = args [ 1 ] - > exprId ( ) )
return { makeContainerSizeValue ( std : : size_t { 0 } , known ) } ;
// TODO: Insert iterator positions for pointers
if ( Token : : simpleMatch ( args [ 1 ] , " + " ) ) {
nonneg int eid = args [ 0 ] - > exprId ( ) ;
const Token * vartok = args [ 1 ] - > astOperand1 ( ) ;
const Token * sizetok = args [ 1 ] - > astOperand2 ( ) ;
if ( sizetok - > exprId ( ) = = eid )
std : : swap ( vartok , sizetok ) ;
if ( vartok - > exprId ( ) = = eid & & sizetok - > hasKnownIntValue ( ) )
return { makeContainerSizeValue ( sizetok , known ) } ;
}
}
} else if ( container - > stdStringLike ) {
if ( astIsPointer ( args [ 0 ] ) ) {
2021-10-05 18:04:48 +02:00
// TODO: Try to read size of string literal { "abc" }
if ( args . size ( ) = = 2 & & astIsIntegral ( args [ 1 ] , false ) ) // { char*, count }
2021-06-04 17:22:05 +02:00
return { makeContainerSizeValue ( args [ 1 ] , known ) } ;
} else if ( astIsContainer ( args [ 0 ] ) ) {
2021-10-05 18:04:48 +02:00
if ( args . size ( ) = = 1 ) // copy constructor { str }
2021-06-04 17:22:05 +02:00
return getContainerValues ( args [ 0 ] ) ;
2021-10-05 18:04:48 +02:00
if ( args . size ( ) = = 3 ) // { str, pos, count }
2021-06-04 17:22:05 +02:00
return { makeContainerSizeValue ( args [ 2 ] , known ) } ;
2021-10-05 18:04:48 +02:00
// TODO: { str, pos }, { ..., alloc }
2021-06-04 17:22:05 +02:00
}
}
2022-04-06 06:25:28 +02:00
return { } ;
}
static std : : vector < ValueFlow : : Value > getInitListSize ( const Token * tok ,
const ValueType * valueType ,
const Settings * settings ,
bool known = true )
{
std : : vector < const Token * > args = getArguments ( tok ) ;
if ( args . empty ( ) )
return { makeContainerSizeValue ( std : : size_t { 0 } , known ) } ;
bool initList = true ;
// Try to disambiguate init list from constructor
if ( args . size ( ) < 4 ) {
initList = ! isIteratorPair ( args ) & & ! ( args . size ( ) < 3 & & astIsIntegral ( args [ 0 ] , false ) ) ;
const Token * containerTypeToken = valueType - > containerTypeToken ;
if ( valueType - > container - > stdStringLike ) {
initList = astIsGenericChar ( args [ 0 ] ) & & ! astIsPointer ( args [ 0 ] ) ;
} else if ( containerTypeToken & & settings ) {
ValueType vt = ValueType : : parseDecl ( containerTypeToken , settings ) ;
if ( vt . pointer > 0 & & astIsPointer ( args [ 0 ] ) )
initList = true ;
else if ( vt . type = = ValueType : : ITERATOR & & astIsIterator ( args [ 0 ] ) )
initList = true ;
else if ( vt . isIntegral ( ) & & astIsIntegral ( args [ 0 ] , false ) )
initList = true ;
}
}
if ( ! initList )
return getContainerSizeFromConstructorArgs ( args , valueType - > container , known ) ;
2021-06-04 17:22:05 +02:00
return { makeContainerSizeValue ( args . size ( ) , known ) } ;
2020-12-18 07:14:11 +01:00
}
2022-04-06 06:25:28 +02:00
static std : : vector < ValueFlow : : Value > getContainerSizeFromConstructor ( const Token * tok ,
const ValueType * valueType ,
const Settings * settings ,
bool known = true )
{
std : : vector < const Token * > args = getArguments ( tok ) ;
if ( args . empty ( ) )
return { makeContainerSizeValue ( std : : size_t { 0 } , known ) } ;
// Init list in constructor
if ( args . size ( ) = = 1 & & Token : : simpleMatch ( args [ 0 ] , " { " ) )
return getInitListSize ( args [ 0 ] , valueType , settings , known ) ;
return getContainerSizeFromConstructorArgs ( args , valueType - > container , known ) ;
}
static void valueFlowContainerSize ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * /*errorLogger*/ ,
const Settings * settings )
2018-08-10 11:29:16 +02:00
{
2018-08-12 14:39:29 +02:00
// declaration
for ( const Variable * var : symboldatabase - > variableList ( ) ) {
2022-04-06 06:25:28 +02:00
if ( ! var )
2018-08-12 14:39:29 +02:00
continue ;
2022-04-06 19:00:05 +02:00
if ( ! var - > scope ( ) | | ! var - > scope ( ) - > bodyEnd | | ! var - > scope ( ) - > bodyStart )
continue ;
2018-08-12 14:39:29 +02:00
if ( ! var - > valueType ( ) | | ! var - > valueType ( ) - > container )
continue ;
2022-04-06 06:25:28 +02:00
if ( ! astIsContainer ( var - > nameToken ( ) ) )
2020-08-26 21:05:17 +02:00
continue ;
2022-04-06 06:25:28 +02:00
bool known = true ;
int size = 0 ;
bool nonLocal = ! var - > isLocal ( ) | | var - > isPointer ( ) | | var - > isReference ( ) | | var - > isStatic ( ) ;
bool constSize = var - > isConst ( ) & & ! nonLocal ;
bool staticSize = false ;
2021-03-31 22:07:54 +02:00
if ( var - > valueType ( ) - > container - > size_templateArgNo > = 0 ) {
2022-04-06 06:25:28 +02:00
staticSize = true ;
constSize = true ;
size = - 1 ;
if ( var - > dimensions ( ) . size ( ) = = 1 ) {
const Dimension & dim = var - > dimensions ( ) . front ( ) ;
if ( dim . known ) {
size = dim . num ;
} else if ( dim . tok & & dim . tok - > hasKnownIntValue ( ) ) {
size = dim . tok - > values ( ) . front ( ) . intvalue ;
}
}
if ( size < 0 )
continue ;
}
2022-04-12 08:07:55 +02:00
if ( ! staticSize & & nonLocal )
2022-04-06 06:25:28 +02:00
continue ;
if ( var - > nameToken ( ) - > hasKnownValue ( ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ) )
2021-03-31 22:07:54 +02:00
continue ;
2022-04-06 06:25:28 +02:00
if ( ! staticSize ) {
if ( ! Token : : Match ( var - > nameToken ( ) , " %name% ; " ) & &
! ( Token : : Match ( var - > nameToken ( ) , " %name% { " ) & &
Token : : simpleMatch ( var - > nameToken ( ) - > next ( ) - > link ( ) , " } ; " ) ) & &
! Token : : Match ( var - > nameToken ( ) , " %name% ( " ) )
continue ;
2021-03-31 22:07:54 +02:00
}
2022-04-06 06:25:28 +02:00
if ( var - > nameToken ( ) - > astTop ( ) & & Token : : Match ( var - > nameToken ( ) - > astTop ( ) - > previous ( ) , " for|while " ) )
known = ! isVariableChanged ( var , settings , true ) ;
std : : vector < ValueFlow : : Value > values { ValueFlow : : Value { size } } ;
2020-12-18 07:14:11 +01:00
values . back ( ) . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
2021-04-18 21:42:27 +02:00
if ( known )
values . back ( ) . setKnown ( ) ;
2022-04-06 06:25:28 +02:00
if ( ! staticSize ) {
if ( Token : : simpleMatch ( var - > nameToken ( ) - > next ( ) , " { " ) ) {
const Token * initList = var - > nameToken ( ) - > next ( ) ;
values = getInitListSize ( initList , var - > valueType ( ) , settings , known ) ;
} else if ( Token : : simpleMatch ( var - > nameToken ( ) - > next ( ) , " ( " ) ) {
const Token * constructorArgs = var - > nameToken ( ) - > next ( ) ;
values = getContainerSizeFromConstructor ( constructorArgs , var - > valueType ( ) , settings , known ) ;
}
}
for ( const ValueFlow : : Value & value : values ) {
if ( constSize )
valueFlowForwardConst ( var - > nameToken ( ) - > next ( ) , var - > scope ( ) - > bodyEnd , var , values , settings ) ;
2022-03-06 07:41:09 +01:00
else
2022-04-06 06:25:28 +02:00
valueFlowContainerForward ( var - > nameToken ( ) - > next ( ) , var - > nameToken ( ) , value , tokenlist ) ;
2022-03-06 07:41:09 +01:00
}
2018-08-12 14:39:29 +02:00
}
2018-08-12 22:01:58 +02:00
// after assignment
for ( const Scope * functionScope : symboldatabase - > functionScopes ) {
for ( const Token * tok = functionScope - > bodyStart ; tok ! = functionScope - > bodyEnd ; tok = tok - > next ( ) ) {
2022-04-06 06:25:28 +02:00
if ( Token : : Match ( tok , " %name%|;|{|} %var% = %str% ; " ) ) {
2018-08-12 22:01:58 +02:00
const Token * containerTok = tok - > next ( ) ;
2021-08-24 16:06:51 +02:00
if ( containerTok - > exprId ( ) = = 0 )
continue ;
if ( containerTok - > valueType ( ) & & containerTok - > valueType ( ) - > container & & containerTok - > valueType ( ) - > container - > stdStringLike ) {
2018-08-12 22:01:58 +02:00
ValueFlow : : Value value ( Token : : getStrLength ( containerTok - > tokAt ( 2 ) ) ) ;
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
value . setKnown ( ) ;
2021-07-18 07:46:31 +02:00
valueFlowContainerForward ( containerTok - > next ( ) , containerTok , value , tokenlist ) ;
2018-08-12 22:01:58 +02:00
}
2020-12-18 07:14:11 +01:00
} else if ( Token : : Match ( tok , " %name%|;|{|}|> %var% = { " ) & & Token : : simpleMatch ( tok - > linkAt ( 3 ) , " } ; " ) ) {
const Token * containerTok = tok - > next ( ) ;
2021-08-24 16:06:51 +02:00
if ( containerTok - > exprId ( ) = = 0 )
continue ;
2021-03-31 22:07:54 +02:00
if ( astIsContainer ( containerTok ) & & containerTok - > valueType ( ) - > container - > size_templateArgNo < 0 ) {
2022-04-06 06:25:28 +02:00
std : : vector < ValueFlow : : Value > values =
getInitListSize ( tok - > tokAt ( 3 ) , containerTok - > valueType ( ) , settings ) ;
2020-12-18 07:14:11 +01:00
for ( const ValueFlow : : Value & value : values )
2021-07-18 07:46:31 +02:00
valueFlowContainerForward ( containerTok - > next ( ) , containerTok , value , tokenlist ) ;
2020-12-18 07:14:11 +01:00
}
2022-04-06 06:25:28 +02:00
} else if ( Token : : Match ( tok , " . %name% ( " ) & & tok - > astOperand1 ( ) & & tok - > astOperand1 ( ) - > valueType ( ) & &
tok - > astOperand1 ( ) - > valueType ( ) - > container ) {
2021-07-25 18:13:55 +02:00
const Token * containerTok = tok - > astOperand1 ( ) ;
2021-08-24 16:06:51 +02:00
if ( containerTok - > exprId ( ) = = 0 )
continue ;
2021-07-25 18:13:55 +02:00
Library : : Container : : Action action = containerTok - > valueType ( ) - > container - > getAction ( tok - > strAt ( 1 ) ) ;
2020-08-10 05:52:03 +02:00
if ( action = = Library : : Container : : Action : : CLEAR ) {
ValueFlow : : Value value ( 0 ) ;
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
value . setKnown ( ) ;
2021-07-25 18:13:55 +02:00
valueFlowContainerForward ( tok - > next ( ) , containerTok , value , tokenlist ) ;
} else if ( action = = Library : : Container : : Action : : RESIZE & & tok - > tokAt ( 2 ) - > astOperand2 ( ) & &
tok - > tokAt ( 2 ) - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
ValueFlow : : Value value ( tok - > tokAt ( 2 ) - > astOperand2 ( ) - > values ( ) . front ( ) ) ;
2020-08-10 05:52:03 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
value . setKnown ( ) ;
2021-07-25 18:13:55 +02:00
valueFlowContainerForward ( tok - > next ( ) , containerTok , value , tokenlist ) ;
2020-08-10 05:52:03 +02:00
}
2018-08-12 22:01:58 +02:00
}
}
}
2018-11-24 10:07:12 +01:00
}
2018-08-10 22:18:38 +02:00
2020-12-24 20:07:46 +01:00
struct ContainerConditionHandler : ConditionHandler {
2021-06-19 13:59:48 +02:00
virtual Analyzer : : Result forward ( Token * start ,
const Token * stop ,
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
2022-02-10 23:02:24 +01:00
const Settings * ) const override {
2021-07-18 07:46:31 +02:00
Analyzer : : Result result { } ;
for ( const ValueFlow : : Value & value : values )
result . update ( valueFlowContainerForward ( start - > next ( ) , stop , exprTok , value , tokenlist ) ) ;
return result ;
2020-12-24 20:07:46 +01:00
}
2021-08-01 10:13:14 +02:00
virtual Analyzer : : Result forward ( Token * top ,
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
2022-02-10 23:02:24 +01:00
const Settings * ) const override {
2021-08-01 10:13:14 +02:00
Analyzer : : Result result { } ;
for ( const ValueFlow : : Value & value : values )
result . update ( valueFlowContainerForwardRecursive ( top , exprTok , value , tokenlist ) ) ;
return result ;
}
2021-01-08 22:55:04 +01:00
virtual void reverse ( Token * start ,
2021-01-23 17:52:01 +01:00
const Token * endTok ,
2021-01-08 22:55:04 +01:00
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
TokenList * tokenlist ,
2022-02-10 23:02:24 +01:00
const Settings * settings ) const override {
2021-01-23 17:52:01 +01:00
return valueFlowContainerReverse ( start , endTok , exprTok , values , tokenlist , settings ) ;
2021-01-08 22:55:04 +01:00
}
2022-02-10 23:02:24 +01:00
virtual std : : vector < Condition > parse ( const Token * tok , const Settings * settings ) const override
2021-08-23 09:03:48 +02:00
{
2020-12-24 20:07:46 +01:00
Condition cond ;
2018-11-24 10:07:12 +01:00
ValueFlow : : Value true_value ;
ValueFlow : : Value false_value ;
const Token * vartok = parseCompareInt ( tok , true_value , false_value ) ;
if ( vartok ) {
2021-08-23 09:03:48 +02:00
vartok = settings - > library . getContainerFromYield ( vartok , Library : : Container : : Yield : : SIZE ) ;
if ( ! vartok )
2021-07-26 22:23:19 +02:00
return { } ;
2021-01-02 09:30:00 +01:00
true_value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
false_value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
2018-11-24 10:07:12 +01:00
cond . true_values . push_back ( true_value ) ;
cond . false_values . push_back ( false_value ) ;
cond . vartok = vartok ;
2021-07-26 22:23:19 +02:00
return { cond } ;
2018-11-24 10:07:12 +01:00
}
2018-08-10 22:18:38 +02:00
2018-11-24 10:07:12 +01:00
// Empty check
if ( tok - > str ( ) = = " ( " ) {
2021-08-23 09:03:48 +02:00
vartok = settings - > library . getContainerFromYield ( tok , Library : : Container : : Yield : : EMPTY ) ;
2018-11-24 10:07:12 +01:00
// TODO: Handle .size()
2021-08-23 09:03:48 +02:00
if ( ! vartok )
2021-07-26 22:23:19 +02:00
return { } ;
2019-01-06 12:21:47 +01:00
const Token * parent = tok - > astParent ( ) ;
while ( parent ) {
2020-08-22 09:16:26 +02:00
if ( Token : : Match ( parent , " %comp% " ) )
2021-07-26 22:23:19 +02:00
return { } ;
2019-01-06 12:21:47 +01:00
parent = parent - > astParent ( ) ;
}
2018-11-24 10:07:12 +01:00
ValueFlow : : Value value ( tok , 0LL ) ;
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
cond . true_values . emplace_back ( value ) ;
2019-09-28 20:22:46 +02:00
cond . false_values . emplace_back ( std : : move ( value ) ) ;
2018-11-24 10:07:12 +01:00
cond . vartok = vartok ;
2020-08-25 07:12:41 +02:00
cond . inverted = true ;
2021-07-26 22:23:19 +02:00
return { cond } ;
2018-11-24 10:07:12 +01:00
}
// String compare
if ( Token : : Match ( tok , " ==|!= " ) ) {
const Token * strtok = nullptr ;
if ( Token : : Match ( tok - > astOperand1 ( ) , " %str% " ) ) {
strtok = tok - > astOperand1 ( ) ;
vartok = tok - > astOperand2 ( ) ;
} else if ( Token : : Match ( tok - > astOperand2 ( ) , " %str% " ) ) {
strtok = tok - > astOperand2 ( ) ;
vartok = tok - > astOperand1 ( ) ;
2018-08-10 22:18:38 +02:00
}
2018-11-24 10:07:12 +01:00
if ( ! strtok )
2021-07-26 22:23:19 +02:00
return { } ;
2018-11-24 10:07:12 +01:00
if ( ! astIsContainer ( vartok ) )
2021-07-26 22:23:19 +02:00
return { } ;
2018-11-24 10:07:12 +01:00
ValueFlow : : Value value ( tok , Token : : getStrLength ( strtok ) ) ;
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
cond . false_values . emplace_back ( value ) ;
2019-09-28 20:22:46 +02:00
cond . true_values . emplace_back ( std : : move ( value ) ) ;
2018-11-24 10:07:12 +01:00
cond . vartok = vartok ;
2021-08-14 06:48:38 +02:00
cond . impossible = false ;
2021-07-26 22:23:19 +02:00
return { cond } ;
2018-08-10 11:29:16 +02:00
}
2021-07-26 22:23:19 +02:00
return { } ;
2020-12-24 20:07:46 +01:00
}
} ;
2018-08-10 11:29:16 +02:00
2021-07-18 07:46:31 +02:00
static void valueFlowDynamicBufferSize ( TokenList * tokenlist , SymbolDatabase * symboldatabase , const Settings * settings )
2019-03-17 13:09:15 +01:00
{
2022-03-07 19:43:40 +01:00
auto getBufferSizeFromAllocFunc = [ & ] ( const Token * funcTok ) - > MathLib : : bigint {
MathLib : : bigint sizeValue = - 1 ;
const Library : : AllocFunc * allocFunc = settings - > library . getAllocFuncInfo ( funcTok ) ;
if ( ! allocFunc )
allocFunc = settings - > library . getReallocFuncInfo ( funcTok ) ;
if ( ! allocFunc | | allocFunc - > bufferSize = = Library : : AllocFunc : : BufferSize : : none )
return sizeValue ;
const std : : vector < const Token * > args = getArguments ( funcTok ) ;
const Token * const arg1 = ( args . size ( ) > = allocFunc - > bufferSizeArg1 ) ? args [ allocFunc - > bufferSizeArg1 - 1 ] : nullptr ;
const Token * const arg2 = ( args . size ( ) > = allocFunc - > bufferSizeArg2 ) ? args [ allocFunc - > bufferSizeArg2 - 1 ] : nullptr ;
switch ( allocFunc - > bufferSize ) {
case Library : : AllocFunc : : BufferSize : : none :
break ;
case Library : : AllocFunc : : BufferSize : : malloc :
if ( arg1 & & arg1 - > hasKnownIntValue ( ) )
sizeValue = arg1 - > getKnownIntValue ( ) ;
break ;
case Library : : AllocFunc : : BufferSize : : calloc :
if ( arg1 & & arg2 & & arg1 - > hasKnownIntValue ( ) & & arg2 - > hasKnownIntValue ( ) )
sizeValue = arg1 - > getKnownIntValue ( ) * arg2 - > getKnownIntValue ( ) ;
break ;
case Library : : AllocFunc : : BufferSize : : strdup :
if ( arg1 & & arg1 - > hasKnownValue ( ) ) {
const ValueFlow : : Value & value = arg1 - > values ( ) . back ( ) ;
if ( value . isTokValue ( ) & & value . tokvalue - > tokType ( ) = = Token : : eString )
sizeValue = Token : : getStrLength ( value . tokvalue ) + 1 ; // Add one for the null terminator
}
break ;
}
return sizeValue ;
} ;
auto getBufferSizeFromNew = [ & ] ( const Token * newTok ) - > MathLib : : bigint {
MathLib : : bigint sizeValue = - 1 , numElem = - 1 ;
if ( newTok & & newTok - > astOperand1 ( ) ) { // number of elements
const Token * bracTok = nullptr , * typeTok = nullptr ;
if ( newTok - > astOperand1 ( ) - > str ( ) = = " [ " )
bracTok = newTok - > astOperand1 ( ) ;
else if ( newTok - > astOperand1 ( ) - > str ( ) = = " ( " ) {
if ( newTok - > astOperand1 ( ) - > astOperand1 ( ) & & newTok - > astOperand1 ( ) - > astOperand1 ( ) - > str ( ) = = " [ " )
bracTok = newTok - > astOperand1 ( ) - > astOperand1 ( ) ;
else
typeTok = newTok - > astOperand1 ( ) - > astOperand1 ( ) ;
}
else {
typeTok = newTok - > astOperand1 ( ) ;
2022-04-01 19:50:40 +02:00
if ( typeTok - > str ( ) = = " { " )
2022-03-24 20:08:51 +01:00
typeTok = typeTok - > astOperand1 ( ) ;
2022-03-07 19:43:40 +01:00
}
if ( bracTok & & bracTok - > astOperand2 ( ) & & bracTok - > astOperand2 ( ) - > hasKnownIntValue ( ) )
numElem = bracTok - > astOperand2 ( ) - > getKnownIntValue ( ) ;
else if ( Token : : Match ( typeTok , " %type% " ) )
numElem = 1 ;
}
if ( numElem > = 0 & & newTok - > astParent ( ) & & newTok - > astParent ( ) - > isAssignmentOp ( ) ) { // size of the allocated type
const Token * typeTok = newTok - > astParent ( ) - > astOperand1 ( ) ; // TODO: implement fallback for e.g. "auto p = new Type;"
if ( ! typeTok | | ! typeTok - > varId ( ) )
typeTok = newTok - > astParent ( ) - > previous ( ) ; // hack for "int** z = ..."
if ( typeTok & & typeTok - > valueType ( ) ) {
const MathLib : : bigint typeSize = typeTok - > valueType ( ) - > typeSize ( * settings , typeTok - > valueType ( ) - > pointer > 1 ) ;
if ( typeSize > = 0 )
sizeValue = numElem * typeSize ;
}
}
return sizeValue ;
} ;
2019-03-17 13:09:15 +01:00
for ( const Scope * functionScope : symboldatabase - > functionScopes ) {
for ( const Token * tok = functionScope - > bodyStart ; tok ! = functionScope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : Match ( tok , " [;{}] %var% = " ) )
continue ;
if ( ! tok - > next ( ) - > variable ( ) )
continue ;
const Token * rhs = tok - > tokAt ( 2 ) - > astOperand2 ( ) ;
while ( rhs & & rhs - > isCast ( ) )
rhs = rhs - > astOperand2 ( ) ? rhs - > astOperand2 ( ) : rhs - > astOperand1 ( ) ;
if ( ! rhs )
continue ;
2022-03-07 19:43:40 +01:00
const bool isNew = symboldatabase - > isCPP ( ) & & rhs - > str ( ) = = " new " ;
if ( ! isNew & & ! Token : : Match ( rhs - > previous ( ) , " %name% ( " ) )
2019-03-17 13:09:15 +01:00
continue ;
2022-03-07 19:43:40 +01:00
const MathLib : : bigint sizeValue = isNew ? getBufferSizeFromNew ( rhs ) : getBufferSizeFromAllocFunc ( rhs - > previous ( ) ) ;
2019-03-20 06:46:55 +01:00
if ( sizeValue < 0 )
2019-03-17 13:09:15 +01:00
continue ;
2019-03-20 06:46:55 +01:00
ValueFlow : : Value value ( sizeValue ) ;
value . errorPath . emplace_back ( tok - > tokAt ( 2 ) , " Assign " + tok - > strAt ( 1 ) + " , buffer with size " + MathLib : : toString ( sizeValue ) ) ;
2019-03-17 13:09:15 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : BUFFER_SIZE ;
value . setKnown ( ) ;
const std : : list < ValueFlow : : Value > values { value } ;
2021-07-18 07:46:31 +02:00
valueFlowForward ( const_cast < Token * > ( rhs ) , functionScope - > bodyEnd , tok - > next ( ) , values , tokenlist , settings ) ;
2019-03-17 13:09:15 +01:00
}
}
}
2019-07-10 21:26:20 +02:00
static bool getMinMaxValues ( const ValueType * vt , const cppcheck : : Platform & platform , MathLib : : bigint * minValue , MathLib : : bigint * maxValue )
2019-07-10 16:59:05 +02:00
{
if ( ! vt | | ! vt - > isIntegral ( ) | | vt - > pointer )
return false ;
int bits ;
switch ( vt - > type ) {
case ValueType : : Type : : BOOL :
bits = 1 ;
break ;
case ValueType : : Type : : CHAR :
bits = platform . char_bit ;
break ;
case ValueType : : Type : : SHORT :
bits = platform . short_bit ;
break ;
case ValueType : : Type : : INT :
bits = platform . int_bit ;
break ;
case ValueType : : Type : : LONG :
bits = platform . long_bit ;
break ;
case ValueType : : Type : : LONGLONG :
bits = platform . long_long_bit ;
break ;
default :
return false ;
2020-04-21 17:27:51 +02:00
}
2019-07-10 16:59:05 +02:00
if ( bits = = 1 ) {
* minValue = 0 ;
* maxValue = 1 ;
} else if ( bits < 62 ) {
if ( vt - > sign = = ValueType : : Sign : : UNSIGNED ) {
* minValue = 0 ;
* maxValue = ( 1LL < < bits ) - 1 ;
} else {
* minValue = - ( 1LL < < ( bits - 1 ) ) ;
* maxValue = ( 1LL < < ( bits - 1 ) ) - 1 ;
}
} else if ( bits = = 64 ) {
if ( vt - > sign = = ValueType : : Sign : : UNSIGNED ) {
* minValue = 0 ;
* maxValue = LLONG_MAX ; // todo max unsigned value
} else {
* minValue = LLONG_MIN ;
* maxValue = LLONG_MAX ;
}
} else {
return false ;
}
return true ;
}
2019-07-10 21:26:20 +02:00
static bool getMinMaxValues ( const std : : string & typestr , const Settings * settings , MathLib : : bigint * minvalue , MathLib : : bigint * maxvalue )
2019-07-10 20:00:21 +02:00
{
TokenList typeTokens ( settings ) ;
std : : istringstream istr ( typestr + " ; " ) ;
if ( ! typeTokens . createTokens ( istr ) )
return false ;
typeTokens . simplifyPlatformTypes ( ) ;
typeTokens . simplifyStdType ( ) ;
const ValueType & vt = ValueType : : parseDecl ( typeTokens . front ( ) , settings ) ;
return getMinMaxValues ( & vt , * settings , minvalue , maxvalue ) ;
}
2021-07-18 07:46:31 +02:00
static void valueFlowSafeFunctions ( TokenList * tokenlist , SymbolDatabase * symboldatabase , const Settings * settings )
2019-07-10 16:59:05 +02:00
{
for ( const Scope * functionScope : symboldatabase - > functionScopes ) {
if ( ! functionScope - > bodyStart )
continue ;
const Function * function = functionScope - > function ;
if ( ! function )
continue ;
2019-07-24 11:39:18 +02:00
const bool safe = function - > isSafe ( settings ) ;
const bool all = safe & & settings - > platformType ! = cppcheck : : Platform : : PlatformType : : Unspecified ;
2019-07-10 16:59:05 +02:00
for ( const Variable & arg : function - > argumentList ) {
2019-07-24 12:08:56 +02:00
if ( ! arg . nameToken ( ) | | ! arg . valueType ( ) )
2019-07-10 16:59:05 +02:00
continue ;
2019-07-24 12:08:56 +02:00
if ( arg . valueType ( ) - > type = = ValueType : : Type : : CONTAINER ) {
2019-07-24 11:39:18 +02:00
if ( ! safe )
continue ;
std : : list < ValueFlow : : Value > argValues ;
argValues . emplace_back ( 0 ) ;
argValues . back ( ) . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , " Assuming " + arg . name ( ) + " is empty " ) ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . safe = true ;
2019-07-24 11:39:18 +02:00
argValues . emplace_back ( 1000000 ) ;
argValues . back ( ) . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , " Assuming " + arg . name ( ) + " size is 1000000 " ) ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . safe = true ;
2019-07-24 11:39:18 +02:00
for ( const ValueFlow : : Value & value : argValues )
2021-07-18 07:46:31 +02:00
valueFlowContainerForward (
const_cast < Token * > ( functionScope - > bodyStart ) , arg . nameToken ( ) , value , tokenlist ) ;
2019-07-24 11:39:18 +02:00
continue ;
}
2019-07-12 11:09:24 +02:00
MathLib : : bigint low , high ;
bool isLow = arg . nameToken ( ) - > getCppcheckAttribute ( TokenImpl : : CppcheckAttributes : : Type : : LOW , & low ) ;
bool isHigh = arg . nameToken ( ) - > getCppcheckAttribute ( TokenImpl : : CppcheckAttributes : : Type : : HIGH , & high ) ;
2019-07-10 16:59:05 +02:00
2019-07-12 11:09:24 +02:00
if ( ! isLow & & ! isHigh & & ! all )
continue ;
2019-07-25 17:19:51 +02:00
const bool safeLow = ! isLow ;
const bool safeHigh = ! isHigh ;
2019-07-12 11:09:24 +02:00
if ( ( ! isLow | | ! isHigh ) & & all ) {
MathLib : : bigint minValue , maxValue ;
if ( getMinMaxValues ( arg . valueType ( ) , * settings , & minValue , & maxValue ) ) {
if ( ! isLow )
low = minValue ;
if ( ! isHigh )
high = maxValue ;
isLow = isHigh = true ;
2019-07-24 12:08:56 +02:00
} else if ( arg . valueType ( ) - > type = = ValueType : : Type : : FLOAT | | arg . valueType ( ) - > type = = ValueType : : Type : : DOUBLE | | arg . valueType ( ) - > type = = ValueType : : Type : : LONGDOUBLE ) {
std : : list < ValueFlow : : Value > argValues ;
argValues . emplace_back ( 0 ) ;
argValues . back ( ) . valueType = ValueFlow : : Value : : ValueType : : FLOAT ;
argValues . back ( ) . floatValue = isLow ? low : - 1E25 f ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , " Safe checks: Assuming argument has value " + MathLib : : toString ( argValues . back ( ) . floatValue ) ) ;
argValues . back ( ) . safe = true ;
2019-07-24 12:30:33 +02:00
argValues . emplace_back ( 0 ) ;
2019-07-24 12:08:56 +02:00
argValues . back ( ) . valueType = ValueFlow : : Value : : ValueType : : FLOAT ;
argValues . back ( ) . floatValue = isHigh ? high : 1E25 f ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , " Safe checks: Assuming argument has value " + MathLib : : toString ( argValues . back ( ) . floatValue ) ) ;
argValues . back ( ) . safe = true ;
2021-07-18 07:46:31 +02:00
valueFlowForward ( const_cast < Token * > ( functionScope - > bodyStart - > next ( ) ) ,
functionScope - > bodyEnd ,
arg . nameToken ( ) ,
argValues ,
tokenlist ,
settings ) ;
2019-07-24 12:08:56 +02:00
continue ;
2019-07-12 11:09:24 +02:00
}
}
std : : list < ValueFlow : : Value > argValues ;
2019-07-17 22:17:34 +02:00
if ( isLow ) {
2019-07-12 11:09:24 +02:00
argValues . emplace_back ( low ) ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , std : : string ( safeLow ? " Safe checks: " : " " ) + " Assuming argument has value " + MathLib : : toString ( low ) ) ;
argValues . back ( ) . safe = safeLow ;
2019-07-17 22:17:34 +02:00
}
if ( isHigh ) {
2019-07-12 11:09:24 +02:00
argValues . emplace_back ( high ) ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , std : : string ( safeHigh ? " Safe checks: " : " " ) + " Assuming argument has value " + MathLib : : toString ( high ) ) ;
argValues . back ( ) . safe = safeHigh ;
2019-07-17 22:17:34 +02:00
}
2019-07-12 11:09:24 +02:00
if ( ! argValues . empty ( ) )
2021-07-18 07:46:31 +02:00
valueFlowForward ( const_cast < Token * > ( functionScope - > bodyStart - > next ( ) ) ,
functionScope - > bodyEnd ,
arg . nameToken ( ) ,
argValues ,
tokenlist ,
settings ) ;
2019-07-10 16:59:05 +02:00
}
}
}
2019-07-11 16:05:34 +02:00
static void valueFlowUnknownFunctionReturn ( TokenList * tokenlist , const Settings * settings )
2019-07-10 20:00:21 +02:00
{
2019-07-11 16:05:34 +02:00
if ( settings - > checkUnknownFunctionReturn . empty ( ) )
2019-07-10 20:00:21 +02:00
return ;
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2020-05-19 21:03:28 +02:00
if ( ! tok - > astParent ( ) | | tok - > str ( ) ! = " ( " | | ! tok - > previous ( ) - > isName ( ) )
2019-07-10 20:00:21 +02:00
continue ;
2019-07-11 16:05:34 +02:00
if ( settings - > checkUnknownFunctionReturn . find ( tok - > previous ( ) - > str ( ) ) = = settings - > checkUnknownFunctionReturn . end ( ) )
continue ;
std : : vector < MathLib : : bigint > unknownValues = settings - > library . unknownReturnValues ( tok - > astOperand1 ( ) ) ;
if ( unknownValues . empty ( ) )
2019-07-10 20:00:21 +02:00
continue ;
// Get min/max values for return type
2019-07-10 22:08:27 +02:00
const std : : string & typestr = settings - > library . returnValueType ( tok - > previous ( ) ) ;
2019-07-10 21:26:20 +02:00
MathLib : : bigint minvalue , maxvalue ;
2019-07-10 20:00:21 +02:00
if ( ! getMinMaxValues ( typestr , settings , & minvalue , & maxvalue ) )
continue ;
2019-07-11 16:05:34 +02:00
for ( MathLib : : bigint value : unknownValues ) {
if ( value < minvalue )
value = minvalue ;
else if ( value > maxvalue )
value = maxvalue ;
setTokenValue ( const_cast < Token * > ( tok ) , ValueFlow : : Value ( value ) , settings ) ;
}
2019-07-10 20:00:21 +02:00
}
}
2021-08-16 09:19:07 +02:00
ValueFlow : : Value : : Value ( const Token * c , long long val , Bound b )
2021-01-02 09:30:00 +01:00
: valueType ( ValueType : : INT ) ,
2021-08-16 09:19:07 +02:00
bound ( b ) ,
2021-08-07 20:51:18 +02:00
intvalue ( val ) ,
tokvalue ( nullptr ) ,
floatValue ( 0.0 ) ,
moveKind ( MoveKind : : NonMovedVariable ) ,
varvalue ( val ) ,
condition ( c ) ,
varId ( 0 ) ,
safe ( false ) ,
conditional ( false ) ,
2021-11-25 18:40:15 +01:00
macro ( false ) ,
2021-08-07 20:51:18 +02:00
defaultArg ( false ) ,
indirect ( 0 ) ,
path ( 0 ) ,
wideintvalue ( 0 ) ,
2021-10-30 07:43:37 +02:00
subexpressions ( ) ,
2022-04-30 09:36:28 +02:00
capturetok ( nullptr ) ,
2021-08-07 20:51:18 +02:00
lifetimeKind ( LifetimeKind : : Object ) ,
lifetimeScope ( LifetimeScope : : Local ) ,
valueKind ( ValueKind : : Possible )
2017-05-16 23:12:35 +02:00
{
2018-04-11 09:44:35 +02:00
errorPath . emplace_back ( c , " Assuming that condition ' " + c - > expressionString ( ) + " ' is not redundant " ) ;
2017-05-16 23:12:35 +02:00
}
2020-08-28 19:29:09 +02:00
void ValueFlow : : Value : : assumeCondition ( const Token * tok )
{
condition = tok ;
errorPath . emplace_back ( tok , " Assuming that condition ' " + tok - > expressionString ( ) + " ' is not redundant " ) ;
}
2017-05-19 16:32:58 +02:00
std : : string ValueFlow : : Value : : infoString ( ) const
{
switch ( valueType ) {
2021-01-02 09:30:00 +01:00
case ValueType : : INT :
2017-05-20 08:47:35 +02:00
return MathLib : : toString ( intvalue ) ;
2021-01-02 09:30:00 +01:00
case ValueType : : TOK :
2017-05-20 08:47:35 +02:00
return tokvalue - > str ( ) ;
2021-01-02 09:30:00 +01:00
case ValueType : : FLOAT :
2017-05-20 08:47:35 +02:00
return MathLib : : toString ( floatValue ) ;
2021-01-02 09:30:00 +01:00
case ValueType : : MOVED :
2017-05-20 08:47:35 +02:00
return " <Moved> " ;
2021-01-02 09:30:00 +01:00
case ValueType : : UNINIT :
2017-05-20 08:47:35 +02:00
return " <Uninit> " ;
2021-01-02 09:30:00 +01:00
case ValueType : : BUFFER_SIZE :
case ValueType : : CONTAINER_SIZE :
2018-08-10 11:29:16 +02:00
return " size= " + MathLib : : toString ( intvalue ) ;
2021-01-02 09:30:00 +01:00
case ValueType : : ITERATOR_START :
2020-08-17 23:36:45 +02:00
return " start= " + MathLib : : toString ( intvalue ) ;
2021-01-02 09:30:00 +01:00
case ValueType : : ITERATOR_END :
2020-08-17 23:36:45 +02:00
return " end= " + MathLib : : toString ( intvalue ) ;
2021-01-02 09:30:00 +01:00
case ValueType : : LIFETIME :
2018-11-10 16:40:40 +01:00
return " lifetime= " + tokvalue - > str ( ) ;
2021-07-30 21:29:35 +02:00
case ValueType : : SYMBOLIC :
std : : string result = " symbolic= " + tokvalue - > expressionString ( ) ;
if ( intvalue > 0 )
result + = " + " + MathLib : : toString ( intvalue ) ;
else if ( intvalue < 0 )
result + = " - " + MathLib : : toString ( - intvalue ) ;
return result ;
2020-04-21 17:27:51 +02:00
}
2017-05-19 16:32:58 +02:00
throw InternalError ( nullptr , " Invalid ValueFlow Value type " ) ;
}
2020-05-10 20:32:59 +02:00
const char * ValueFlow : : Value : : toString ( MoveKind moveKind )
{
switch ( moveKind ) {
case MoveKind : : NonMovedVariable :
return " NonMovedVariable " ;
case MoveKind : : MovedVariable :
return " MovedVariable " ;
case MoveKind : : ForwardedVariable :
return " ForwardedVariable " ;
}
return " " ;
}
2021-06-04 21:41:30 +02:00
const char * ValueFlow : : Value : : toString ( LifetimeKind lifetimeKind )
{
switch ( lifetimeKind ) {
case LifetimeKind : : Object :
return " Object " ;
case LifetimeKind : : SubObject :
return " SubObject " ;
case LifetimeKind : : Lambda :
return " Lambda " ;
case LifetimeKind : : Iterator :
return " Iterator " ;
case LifetimeKind : : Address :
return " Address " ;
}
return " " ;
}
2020-05-10 20:32:59 +02:00
2021-08-19 22:01:55 +02:00
bool ValueFlow : : Value : : sameToken ( const Token * tok1 , const Token * tok2 )
{
if ( tok1 = = tok2 )
return true ;
if ( ! tok1 )
return false ;
if ( tok1 - > exprId ( ) = = 0 | | tok2 - > exprId ( ) = = 0 )
return false ;
return tok1 - > exprId ( ) = = tok2 - > exprId ( ) ;
}
2021-08-15 08:02:31 +02:00
const char * ValueFlow : : Value : : toString ( LifetimeScope lifetimeScope )
{
switch ( lifetimeScope ) {
case ValueFlow : : Value : : LifetimeScope : : Local :
return " Local " ;
case ValueFlow : : Value : : LifetimeScope : : Argument :
return " Argument " ;
case ValueFlow : : Value : : LifetimeScope : : SubFunction :
return " SubFunction " ;
2022-03-30 19:30:02 +02:00
case ValueFlow : : Value : : LifetimeScope : : ThisPointer :
return " ThisPointer " ;
case ValueFlow : : Value : : LifetimeScope : : ThisValue :
return " ThisValue " ;
2021-08-15 08:02:31 +02:00
}
return " " ;
}
const char * ValueFlow : : Value : : toString ( Bound bound )
{
switch ( bound ) {
case ValueFlow : : Value : : Bound : : Point :
return " Point " ;
case ValueFlow : : Value : : Bound : : Upper :
return " Upper " ;
case ValueFlow : : Value : : Bound : : Lower :
return " Lower " ;
}
return " " ;
}
2019-06-16 10:09:38 +02:00
const ValueFlow : : Value * ValueFlow : : valueFlowConstantFoldAST ( Token * expr , const Settings * settings )
2016-05-07 20:18:07 +02:00
{
2017-03-27 18:48:34 +02:00
if ( expr & & expr - > values ( ) . empty ( ) ) {
2016-07-08 20:39:34 +02:00
valueFlowConstantFoldAST ( expr - > astOperand1 ( ) , settings ) ;
valueFlowConstantFoldAST ( expr - > astOperand2 ( ) , settings ) ;
2016-10-18 21:44:02 +02:00
valueFlowSetConstantValue ( expr , settings , true /* TODO: this is a guess */ ) ;
2016-05-08 11:17:10 +02:00
}
2018-07-14 13:09:53 +02:00
return expr & & expr - > hasKnownValue ( ) ? & expr - > values ( ) . front ( ) : nullptr ;
2016-05-07 20:18:07 +02:00
}
2018-11-03 15:53:24 +01:00
static std : : size_t getTotalValues ( TokenList * tokenlist )
{
std : : size_t n = 1 ;
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) )
n + = tok - > values ( ) . size ( ) ;
return n ;
}
2016-05-07 20:18:07 +02:00
2015-02-01 12:10:20 +01:00
void ValueFlow : : setValues ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-01-04 20:57:02 +01:00
{
2014-01-05 20:06:46 +01:00
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) )
2017-03-27 18:48:34 +02:00
tok - > clearValueFlow ( ) ;
2014-01-04 20:57:02 +01:00
Fix #9647: Set correct enum value (#2856)
* Tokenize: Set varId for variables in enum
Set varIds in enum values. It was previously disabled in 5119ae84b879fad
to avoid issues with enums named the same as global variables. Take care
to only set varids to variables used to set the value of an enumerator,
not the enumerator itself. This is somewhat complicated by the fact that
at the time this happens, astOperand1(), astOperand2(), astParent() etc
are not set. The current implementation is not perfect, for example in
the code below, y will not have a varid set, but x and z will. This is
deemed sufficient for now.
int x, y, z;
enum E { a = f(x, y, z); };
* Fix #9647: Value of enums with variables as init values
C++ allows enum values to be set using constexprs, which cppcheck did
not handle before. To solve this, add a new pass to valueflow to update
enum values after global consts have been processed. In order to do so,
I moved all settings of enum values to valueflow. After setting the enum
values, we need another call to valueFlowNumber() to actually set users
of the enums.
There is still room for improvements, since each pass of
valueFlowGlobalConstVar() and valueFlowEnumValue() only sets variables
that are possible to set directly, and not if setting the value of a
variable allows us to set the value of another. For example
constexpr int a = 5;
constexpr int b = a + 5;
enum E { X = a };
constexpr E e = X;
Here both b and e will not have their values set, even though cppcheck
should be possible to figure out their values. That's for another PR
though.
This was tested by running test-my-pr.py with 500 packages. The only
difference was one error message in fairy-stockfish_11.1, where cppcheck
now printed the correct size of an array instead of 2147483648 which I
assume is some kind of default value. In that package, using a constexpr
when setting enum values is common, but as mentioned, there was no
change in the number of warnings.
2020-10-22 07:45:04 +02:00
valueFlowEnumValue ( symboldatabase , settings ) ;
2014-01-18 19:30:44 +01:00
valueFlowNumber ( tokenlist ) ;
2014-08-03 20:11:22 +02:00
valueFlowString ( tokenlist ) ;
2015-02-24 15:57:39 +01:00
valueFlowArray ( tokenlist ) ;
2019-07-11 16:05:34 +02:00
valueFlowUnknownFunctionReturn ( tokenlist , settings ) ;
2019-04-21 06:54:32 +02:00
valueFlowGlobalConstVar ( tokenlist , settings ) ;
Fix #9647: Set correct enum value (#2856)
* Tokenize: Set varId for variables in enum
Set varIds in enum values. It was previously disabled in 5119ae84b879fad
to avoid issues with enums named the same as global variables. Take care
to only set varids to variables used to set the value of an enumerator,
not the enumerator itself. This is somewhat complicated by the fact that
at the time this happens, astOperand1(), astOperand2(), astParent() etc
are not set. The current implementation is not perfect, for example in
the code below, y will not have a varid set, but x and z will. This is
deemed sufficient for now.
int x, y, z;
enum E { a = f(x, y, z); };
* Fix #9647: Value of enums with variables as init values
C++ allows enum values to be set using constexprs, which cppcheck did
not handle before. To solve this, add a new pass to valueflow to update
enum values after global consts have been processed. In order to do so,
I moved all settings of enum values to valueflow. After setting the enum
values, we need another call to valueFlowNumber() to actually set users
of the enums.
There is still room for improvements, since each pass of
valueFlowGlobalConstVar() and valueFlowEnumValue() only sets variables
that are possible to set directly, and not if setting the value of a
variable allows us to set the value of another. For example
constexpr int a = 5;
constexpr int b = a + 5;
enum E { X = a };
constexpr E e = X;
Here both b and e will not have their values set, even though cppcheck
should be possible to figure out their values. That's for another PR
though.
This was tested by running test-my-pr.py with 500 packages. The only
difference was one error message in fairy-stockfish_11.1, where cppcheck
now printed the correct size of an array instead of 2147483648 which I
assume is some kind of default value. In that package, using a constexpr
when setting enum values is common, but as mentioned, there was no
change in the number of warnings.
2020-10-22 07:45:04 +02:00
valueFlowEnumValue ( symboldatabase , settings ) ;
valueFlowNumber ( tokenlist ) ;
2017-09-15 15:58:19 +02:00
valueFlowGlobalStaticVar ( tokenlist , settings ) ;
2014-08-03 20:11:22 +02:00
valueFlowPointerAlias ( tokenlist ) ;
2018-11-10 16:40:40 +01:00
valueFlowLifetime ( tokenlist , symboldatabase , errorLogger , settings ) ;
2021-07-30 21:29:35 +02:00
valueFlowSymbolic ( tokenlist , symboldatabase ) ;
2014-04-14 06:45:39 +02:00
valueFlowBitAnd ( tokenlist ) ;
2018-12-16 07:35:27 +01:00
valueFlowSameExpressions ( tokenlist ) ;
2021-01-23 08:47:39 +01:00
valueFlowConditionExpressions ( tokenlist , symboldatabase , errorLogger , settings ) ;
2018-11-03 15:53:24 +01:00
std : : size_t values = 0 ;
2019-09-23 19:35:39 +02:00
std : : size_t n = 4 ;
while ( n > 0 & & values < getTotalValues ( tokenlist ) ) {
2018-11-03 15:53:24 +01:00
values = getTotalValues ( tokenlist ) ;
2021-03-30 14:02:28 +02:00
valueFlowImpossibleValues ( tokenlist , settings ) ;
2021-10-14 16:46:36 +02:00
valueFlowSymbolicIdentity ( tokenlist ) ;
2021-08-03 06:31:28 +02:00
valueFlowSymbolicAbs ( tokenlist , symboldatabase ) ;
2021-08-19 22:01:55 +02:00
valueFlowCondition ( SymbolicConditionHandler { } , tokenlist , symboldatabase , errorLogger , settings ) ;
2021-07-30 21:29:35 +02:00
valueFlowSymbolicInfer ( tokenlist , symboldatabase ) ;
2019-01-21 20:05:35 +01:00
valueFlowArrayBool ( tokenlist ) ;
2019-01-01 14:15:50 +01:00
valueFlowRightShift ( tokenlist , settings ) ;
2021-08-11 08:37:37 +02:00
valueFlowAfterAssign ( tokenlist , symboldatabase , errorLogger , settings ) ;
2021-10-15 10:58:16 +02:00
valueFlowAfterSwap ( tokenlist , symboldatabase , errorLogger , settings ) ;
2020-12-24 20:07:46 +01:00
valueFlowCondition ( SimpleConditionHandler { } , tokenlist , symboldatabase , errorLogger , settings ) ;
2019-09-28 20:50:56 +02:00
valueFlowInferCondition ( tokenlist , settings ) ;
2018-11-03 15:53:24 +01:00
valueFlowSwitchVariable ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowForLoop ( tokenlist , symboldatabase , errorLogger , settings ) ;
2020-02-17 10:31:08 +01:00
valueFlowSubFunction ( tokenlist , symboldatabase , errorLogger , settings ) ;
2020-09-09 04:54:38 +02:00
valueFlowFunctionReturn ( tokenlist , errorLogger ) ;
2020-09-04 18:56:34 +02:00
valueFlowLifetime ( tokenlist , symboldatabase , errorLogger , settings ) ;
2021-07-18 07:46:31 +02:00
valueFlowFunctionDefaultParameter ( tokenlist , symboldatabase , settings ) ;
valueFlowUninit ( tokenlist , symboldatabase , settings ) ;
2018-11-24 10:07:12 +01:00
if ( tokenlist - > isCPP ( ) ) {
2021-08-11 08:37:37 +02:00
valueFlowAfterMove ( tokenlist , symboldatabase , settings ) ;
2019-04-26 11:30:09 +02:00
valueFlowSmartPointer ( tokenlist , errorLogger , settings ) ;
2020-08-21 17:23:55 +02:00
valueFlowIterators ( tokenlist , settings ) ;
2020-12-24 20:07:46 +01:00
valueFlowCondition ( IteratorConditionHandler { } , tokenlist , symboldatabase , errorLogger , settings ) ;
2020-08-31 08:46:56 +02:00
valueFlowIteratorInfer ( tokenlist , settings ) ;
2018-11-03 15:53:24 +01:00
valueFlowContainerSize ( tokenlist , symboldatabase , errorLogger , settings ) ;
2020-12-24 20:07:46 +01:00
valueFlowCondition ( ContainerConditionHandler { } , tokenlist , symboldatabase , errorLogger , settings ) ;
2018-11-24 10:07:12 +01:00
}
2021-07-18 07:46:31 +02:00
valueFlowSafeFunctions ( tokenlist , symboldatabase , settings ) ;
2019-09-23 19:35:39 +02:00
n - - ;
2018-11-03 15:53:24 +01:00
}
2019-03-17 13:09:15 +01:00
2021-07-18 07:46:31 +02:00
valueFlowDynamicBufferSize ( tokenlist , symboldatabase , settings ) ;
2014-01-04 20:57:02 +01:00
}
2015-07-29 19:54:57 +02:00
2021-08-16 09:19:07 +02:00
ValueFlow : : Value ValueFlow : : Value : : unknown ( )
{
Value v ;
v . valueType = Value : : ValueType : : UNINIT ;
return v ;
}
2015-07-29 19:54:57 +02:00
std : : string ValueFlow : : eitherTheConditionIsRedundant ( const Token * condition )
{
if ( ! condition )
return " Either the condition is redundant " ;
if ( condition - > str ( ) = = " case " ) {
std : : string expr ;
for ( const Token * tok = condition ; tok & & tok - > str ( ) ! = " : " ; tok = tok - > next ( ) ) {
expr + = tok - > str ( ) ;
if ( Token : : Match ( tok , " %name%|%num% %name%|%num% " ) )
expr + = ' ' ;
}
return " Either the switch case ' " + expr + " ' is redundant " ;
}
return " Either the condition ' " + condition - > expressionString ( ) + " ' is redundant " ;
2020-05-19 21:55:28 +02:00
}
2021-06-04 17:17:41 +02:00
const ValueFlow : : Value * ValueFlow : : findValue ( const std : : list < ValueFlow : : Value > & values ,
2021-08-07 20:51:18 +02:00
const Settings * settings ,
std : : function < bool ( const ValueFlow : : Value & ) > pred )
2021-06-04 17:17:41 +02:00
{
const ValueFlow : : Value * ret = nullptr ;
for ( const ValueFlow : : Value & v : values ) {
if ( pred ( v ) ) {
if ( ! ret | | ret - > isInconclusive ( ) | | ( ret - > condition & & ! v . isInconclusive ( ) ) )
ret = & v ;
if ( ! ret - > isInconclusive ( ) & & ! ret - > condition )
break ;
}
}
if ( settings & & ret ) {
if ( ret - > isInconclusive ( ) & & ! settings - > certainty . isEnabled ( Certainty : : inconclusive ) )
return nullptr ;
if ( ret - > condition & & ! settings - > severity . isEnabled ( Severity : : warning ) )
return nullptr ;
}
return ret ;
}
2021-08-16 09:19:07 +02:00
static std : : vector < ValueFlow : : Value > isOutOfBoundsImpl ( const ValueFlow : : Value & size ,
const Token * indexTok ,
bool condition )
{
if ( ! indexTok )
return { } ;
const ValueFlow : : Value * indexValue = indexTok - > getMaxValue ( condition , size . path ) ;
if ( ! indexValue )
return { } ;
if ( indexValue - > intvalue > = size . intvalue )
return { * indexValue } ;
if ( ! condition )
return { } ;
// TODO: Use a better way to decide if the variable in unconstrained
if ( ! indexTok - > variable ( ) | | ! indexTok - > variable ( ) - > isArgument ( ) )
return { } ;
2021-09-06 07:15:24 +02:00
if ( std : : any_of ( indexTok - > values ( ) . begin ( ) , indexTok - > values ( ) . end ( ) , [ & ] ( const ValueFlow : : Value & v ) {
return v . isSymbolicValue ( ) & & v . isPossible ( ) & & v . bound = = ValueFlow : : Value : : Bound : : Upper ;
} ) )
return { } ;
2021-08-16 09:19:07 +02:00
if ( indexValue - > bound ! = ValueFlow : : Value : : Bound : : Lower )
return { } ;
if ( size . bound = = ValueFlow : : Value : : Bound : : Lower )
return { } ;
ValueFlow : : Value value = inferCondition ( " >= " , indexTok , indexValue - > intvalue ) ;
if ( ! value . isKnown ( ) )
return { } ;
if ( value . intvalue = = 0 )
return { } ;
value . intvalue = size . intvalue ;
value . bound = ValueFlow : : Value : : Bound : : Lower ;
return { value } ;
}
std : : vector < ValueFlow : : Value > ValueFlow : : isOutOfBounds ( const Value & size , const Token * indexTok , bool possible )
{
2021-12-04 08:54:04 +01:00
ValueFlow : : Value inBoundsValue = inferCondition ( " < " , indexTok , size . intvalue ) ;
if ( inBoundsValue . isKnown ( ) & & inBoundsValue . intvalue ! = 0 )
return { } ;
2021-08-16 09:19:07 +02:00
std : : vector < ValueFlow : : Value > result = isOutOfBoundsImpl ( size , indexTok , false ) ;
if ( ! result . empty ( ) )
return result ;
if ( ! possible )
return result ;
return isOutOfBoundsImpl ( size , indexTok , true ) ;
}