2019-09-17 21:00:59 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
* Copyright ( C ) 2007 - 2019 Cppcheck team .
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "exprengine.h"
2019-09-19 20:18:39 +02:00
# include "astutils.h"
2019-09-17 21:00:59 +02:00
# include "settings.h"
# include "symboldatabase.h"
# include "tokenize.h"
2019-10-02 17:59:04 +02:00
# include <cstdlib>
2019-09-22 21:14:20 +02:00
# include <limits>
2019-09-17 21:00:59 +02:00
# include <memory>
# include <iostream>
2019-10-02 17:59:04 +02:00
# ifdef USE_Z3
# include <z3++.h>
# endif
2019-09-17 21:00:59 +02:00
std : : string ExprEngine : : str ( int128_t value )
{
std : : ostringstream ostr ;
2019-09-17 22:28:36 +02:00
# ifdef __GNUC__
2019-09-17 21:00:59 +02:00
if ( value = = ( int ) value ) {
ostr < < ( int ) value ;
return ostr . str ( ) ;
}
if ( value < 0 ) {
ostr < < " - " ;
value = - value ;
}
uint64_t high = value > > 64 ;
uint64_t low = value ;
if ( high > 0 )
ostr < < " h " < < std : : hex < < high < < " l " ;
ostr < < std : : hex < < low ;
2019-09-17 22:28:36 +02:00
# else
ostr < < value ;
# endif
2019-09-17 21:00:59 +02:00
return ostr . str ( ) ;
}
static ExprEngine : : ValuePtr getValueRangeFromValueType ( const std : : string & name , const ValueType * vt , const cppcheck : : Platform & platform ) ;
namespace {
2019-09-19 20:18:39 +02:00
class TrackExecution {
public :
2019-09-20 07:04:58 +02:00
TrackExecution ( ) : mDataIndex ( 0 ) { }
2019-09-19 20:18:39 +02:00
std : : map < const Token * , std : : vector < std : : string > > map ;
int getNewDataIndex ( ) {
2019-09-20 07:04:58 +02:00
return mDataIndex + + ;
2019-09-19 20:18:39 +02:00
}
2019-09-26 20:48:33 +02:00
void symbolRange ( const Token * tok , ExprEngine : : ValuePtr value ) {
2019-09-26 21:43:29 +02:00
if ( ! tok | | ! value )
2019-09-19 20:18:39 +02:00
return ;
2019-09-26 20:48:33 +02:00
const std : : string & symbolicExpression = value - > getSymbolicExpression ( ) ;
if ( symbolicExpression [ 0 ] ! = ' $ ' )
return ;
2019-09-27 08:17:46 +02:00
if ( mSymbols . find ( symbolicExpression ) ! = mSymbols . end ( ) )
2019-09-26 20:48:33 +02:00
return ;
2019-09-27 08:17:46 +02:00
mSymbols . insert ( symbolicExpression ) ;
2019-09-26 20:48:33 +02:00
map [ tok ] . push_back ( symbolicExpression + " = " + value - > getRange ( ) ) ;
2019-09-19 20:18:39 +02:00
}
void state ( const Token * tok , const std : : string & s ) {
map [ tok ] . push_back ( s ) ;
}
2019-09-29 15:00:54 +02:00
void print ( std : : ostream & out ) {
2019-09-19 20:18:39 +02:00
std : : set < std : : pair < int , int > > locations ;
for ( auto it : map ) {
2019-09-20 07:04:58 +02:00
locations . insert ( std : : pair < int , int > ( it . first - > linenr ( ) , it . first - > column ( ) ) ) ;
2019-09-19 20:18:39 +02:00
}
for ( const std : : pair < int , int > & loc : locations ) {
int lineNumber = loc . first ;
int column = loc . second ;
for ( auto & it : map ) {
const Token * tok = it . first ;
if ( lineNumber ! = tok - > linenr ( ) )
continue ;
2019-09-26 21:00:36 +02:00
if ( column ! = tok - > column ( ) )
continue ;
2019-09-19 20:18:39 +02:00
const std : : vector < std : : string > & dumps = it . second ;
for ( const std : : string & dump : dumps )
2019-09-29 15:00:54 +02:00
out < < lineNumber < < " : " < < column < < " : " < < dump < < " \n " ;
2019-09-19 20:18:39 +02:00
}
}
}
private :
2019-09-20 07:04:58 +02:00
int mDataIndex ;
2019-09-27 08:17:46 +02:00
std : : set < std : : string > mSymbols ;
2019-09-19 20:18:39 +02:00
} ;
2019-09-29 15:00:54 +02:00
class Data : public ExprEngine : : DataBase {
2019-09-17 21:00:59 +02:00
public :
2019-09-19 20:18:39 +02:00
Data ( int * symbolValueIndex , const Tokenizer * tokenizer , const Settings * settings , const std : : vector < ExprEngine : : Callback > & callbacks , TrackExecution * trackExecution )
2019-09-29 15:00:54 +02:00
: DataBase ( settings )
, symbolValueIndex ( symbolValueIndex )
2019-09-17 21:00:59 +02:00
, tokenizer ( tokenizer )
2019-09-19 20:18:39 +02:00
, callbacks ( callbacks )
, mTrackExecution ( trackExecution )
2019-09-20 06:12:20 +02:00
, mDataIndex ( trackExecution - > getNewDataIndex ( ) ) { }
2019-09-17 21:00:59 +02:00
typedef std : : map < nonneg int , std : : shared_ptr < ExprEngine : : Value > > Memory ;
Memory memory ;
2019-09-19 20:18:39 +02:00
int * const symbolValueIndex ;
const Tokenizer * const tokenizer ;
2019-09-17 21:00:59 +02:00
const std : : vector < ExprEngine : : Callback > & callbacks ;
2019-10-03 08:48:05 +02:00
std : : vector < ExprEngine : : ValuePtr > constraints ;
2019-09-17 21:00:59 +02:00
2019-09-26 20:48:33 +02:00
void assignValue ( const Token * tok , unsigned int varId , ExprEngine : : ValuePtr value ) {
mTrackExecution - > symbolRange ( tok , value ) ;
2019-09-27 13:12:16 +02:00
if ( value ) {
if ( auto arr = std : : dynamic_pointer_cast < ExprEngine : : ArrayValue > ( value ) ) {
mTrackExecution - > symbolRange ( tok , arr - > size ) ;
for ( const auto & indexAndValue : arr - > data )
mTrackExecution - > symbolRange ( tok , indexAndValue . value ) ;
2019-09-27 13:30:09 +02:00
} else if ( auto s = std : : dynamic_pointer_cast < ExprEngine : : StructValue > ( value ) ) {
for ( const auto & m : s - > member )
mTrackExecution - > symbolRange ( tok , m . second ) ;
2019-09-27 13:12:16 +02:00
}
}
2019-09-26 20:48:33 +02:00
memory [ varId ] = value ;
}
2019-09-26 21:35:29 +02:00
void assignStructMember ( const Token * tok , ExprEngine : : StructValue * structVal , const std : : string & memberName , ExprEngine : : ValuePtr value ) {
mTrackExecution - > symbolRange ( tok , value ) ;
structVal - > member [ memberName ] = value ;
}
2019-09-29 15:00:54 +02:00
std : : string getNewSymbolName ( ) override {
2019-09-17 21:00:59 +02:00
return " $ " + std : : to_string ( + + ( * symbolValueIndex ) ) ;
}
std : : shared_ptr < ExprEngine : : ArrayValue > getArrayValue ( const Token * tok ) {
const Memory : : iterator it = memory . find ( tok - > varId ( ) ) ;
if ( it ! = memory . end ( ) )
return std : : dynamic_pointer_cast < ExprEngine : : ArrayValue > ( it - > second ) ;
2019-09-21 14:17:16 +02:00
if ( tok - > varId ( ) = = 0 )
return std : : shared_ptr < ExprEngine : : ArrayValue > ( ) ;
2019-09-29 15:00:54 +02:00
auto val = std : : make_shared < ExprEngine : : ArrayValue > ( this , tok - > variable ( ) ) ;
assignValue ( tok , tok - > varId ( ) , val ) ;
2019-09-21 14:17:16 +02:00
return val ;
2019-09-17 21:00:59 +02:00
}
2019-09-19 20:18:39 +02:00
ExprEngine : : ValuePtr getValue ( unsigned int varId , const ValueType * valueType , const Token * tok ) {
2019-09-17 21:00:59 +02:00
const Memory : : const_iterator it = memory . find ( varId ) ;
if ( it ! = memory . end ( ) )
return it - > second ;
if ( ! valueType )
return ExprEngine : : ValuePtr ( ) ;
ExprEngine : : ValuePtr value = getValueRangeFromValueType ( getNewSymbolName ( ) , valueType , * settings ) ;
2019-09-19 20:18:39 +02:00
if ( value ) {
2019-09-26 20:48:33 +02:00
assignValue ( tok , varId , value ) ;
2019-09-19 20:18:39 +02:00
}
2019-09-17 21:00:59 +02:00
return value ;
}
2019-09-19 20:18:39 +02:00
void trackProgramState ( const Token * tok ) {
if ( memory . empty ( ) )
return ;
const SymbolDatabase * const symbolDatabase = tokenizer - > getSymbolDatabase ( ) ;
std : : ostringstream s ;
2019-09-27 21:03:47 +02:00
s < < " { " ; // << mDataIndex << ":";
2019-09-19 20:18:39 +02:00
for ( auto mem : memory ) {
ExprEngine : : ValuePtr value = mem . second ;
2019-09-27 14:36:33 +02:00
const Variable * var = symbolDatabase - > getVariableFromVarId ( mem . first ) ;
if ( ! var )
continue ;
s < < " " < < var - > name ( ) < < " = " ;
2019-09-20 21:27:51 +02:00
if ( ! value )
s < < " (null) " ;
2019-09-26 10:07:31 +02:00
else if ( value - > name [ 0 ] = = ' $ ' & & value - > getSymbolicExpression ( ) ! = value - > name )
2019-09-26 10:03:58 +02:00
s < < " ( " < < value - > name < < " , " < < value - > getSymbolicExpression ( ) < < " ) " ;
2019-09-19 20:18:39 +02:00
else
s < < value - > name ;
}
s < < " } " ;
mTrackExecution - > state ( tok , s . str ( ) ) ;
}
2019-10-02 21:47:00 +02:00
ExprEngine : : ValuePtr notValue ( ExprEngine : : ValuePtr v ) {
auto b = std : : dynamic_pointer_cast < ExprEngine : : BinOpResult > ( v ) ;
if ( b ) {
std : : string binop ;
if ( b - > binop = = " == " )
binop = " != " ;
else if ( b - > binop = = " != " )
binop = " == " ;
else if ( b - > binop = = " >= " )
binop = " < " ;
else if ( b - > binop = = " <= " )
binop = " > " ;
else if ( b - > binop = = " > " )
binop = " <= " ;
else if ( b - > binop = = " < " )
binop = " >= " ;
if ( ! binop . empty ( ) )
return std : : make_shared < ExprEngine : : BinOpResult > ( binop , b - > op1 , b - > op2 ) ;
}
auto zero = std : : make_shared < ExprEngine : : IntRange > ( " 0 " , 0 , 0 ) ;
return std : : make_shared < ExprEngine : : BinOpResult > ( " == " , v , zero ) ;
}
2019-10-03 08:48:05 +02:00
void addConstraint ( ExprEngine : : ValuePtr condValue , bool trueCond ) {
2019-10-06 14:47:34 +02:00
if ( ! condValue )
return ;
2019-10-02 21:47:00 +02:00
if ( trueCond )
2019-10-03 08:48:05 +02:00
constraints . push_back ( condValue ) ;
2019-10-02 21:47:00 +02:00
else
2019-10-03 08:48:05 +02:00
constraints . push_back ( notValue ( condValue ) ) ;
2019-10-02 21:47:00 +02:00
}
2019-09-19 20:18:39 +02:00
private :
TrackExecution * const mTrackExecution ;
2019-09-20 06:12:20 +02:00
const int mDataIndex ;
2019-09-17 21:00:59 +02:00
} ;
}
2019-09-25 18:33:21 +02:00
static ExprEngine : : ValuePtr simplifyValue ( ExprEngine : : ValuePtr origValue )
{
auto b = std : : dynamic_pointer_cast < ExprEngine : : BinOpResult > ( origValue ) ;
if ( ! b )
return origValue ;
if ( ! b - > op1 | | ! b - > op2 )
return origValue ;
auto intRange1 = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( b - > op1 ) ;
2019-10-02 17:59:04 +02:00
auto intRange2 = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( b - > op2 ) ;
2019-09-25 18:33:21 +02:00
if ( intRange1 & & intRange2 & & intRange1 - > minValue = = intRange1 - > maxValue & & intRange2 - > minValue = = intRange2 - > maxValue ) {
const std : : string & binop = b - > binop ;
int128_t v ;
if ( binop = = " + " )
v = intRange1 - > minValue + intRange2 - > minValue ;
else if ( binop = = " - " )
v = intRange1 - > minValue - intRange2 - > minValue ;
else if ( binop = = " * " )
v = intRange1 - > minValue * intRange2 - > minValue ;
else if ( binop = = " / " & & intRange2 - > minValue ! = 0 )
v = intRange1 - > minValue / intRange2 - > minValue ;
else if ( binop = = " % " & & intRange2 - > minValue ! = 0 )
v = intRange1 - > minValue % intRange2 - > minValue ;
else
return origValue ;
return std : : make_shared < ExprEngine : : IntRange > ( ExprEngine : : str ( v ) , v , v ) ;
}
return origValue ;
}
2019-09-28 16:16:36 +02:00
static ExprEngine : : ValuePtr translateUninitValueToRange ( ExprEngine : : ValuePtr value , const : : ValueType * valueType , Data & data )
{
if ( ! value )
return value ;
if ( value - > type = = ExprEngine : : ValueType : : UninitValue ) {
auto rangeValue = getValueRangeFromValueType ( data . getNewSymbolName ( ) , valueType , * data . settings ) ;
if ( rangeValue )
return rangeValue ;
}
if ( auto conditionalValue = std : : dynamic_pointer_cast < ExprEngine : : ConditionalValue > ( value ) ) {
if ( conditionalValue - > values . size ( ) = = 1 & & conditionalValue - > values [ 0 ] . second & & conditionalValue - > values [ 0 ] . second - > type = = ExprEngine : : ValueType : : UninitValue ) {
auto rangeValue = getValueRangeFromValueType ( data . getNewSymbolName ( ) , valueType , * data . settings ) ;
if ( rangeValue )
return rangeValue ;
}
}
return value ;
}
2019-09-25 18:33:21 +02:00
2019-09-28 19:28:12 +02:00
static int128_t truncateInt ( int128_t value , int bits , char sign )
{
value = value & ( ( ( int128_t ) 1 < < bits ) - 1 ) ;
// Sign extension
if ( sign = = ' s ' & & value & ( 1ULL < < ( bits - 1 ) ) )
value | = ~ ( ( ( int128_t ) 1 < < bits ) - 1 ) ;
return value ;
}
2019-09-25 18:33:21 +02:00
ExprEngine : : ArrayValue : : ArrayValue ( const std : : string & name , ExprEngine : : ValuePtr size , ExprEngine : : ValuePtr value )
2019-09-26 10:03:58 +02:00
: Value ( name , ExprEngine : : ValueType : : ArrayValue )
2019-09-25 18:33:21 +02:00
, size ( size )
{
assign ( ExprEngine : : ValuePtr ( ) , value ) ;
}
2019-09-29 15:00:54 +02:00
ExprEngine : : ArrayValue : : ArrayValue ( DataBase * data , const Variable * var )
: Value ( data - > getNewSymbolName ( ) , ExprEngine : : ValueType : : ArrayValue )
2019-09-25 18:33:21 +02:00
{
if ( var ) {
int sz = 1 ;
for ( const auto & dim : var - > dimensions ( ) ) {
if ( ! dim . known ) {
sz = - 1 ;
break ;
}
sz * = dim . num ;
}
if ( sz > = 1 )
size = std : : make_shared < ExprEngine : : IntRange > ( std : : to_string ( sz ) , sz , sz ) ;
}
2019-09-29 15:00:54 +02:00
ValuePtr val ;
2019-09-29 17:32:26 +02:00
if ( var & & ! var - > isGlobal ( ) & & ! var - > isStatic ( ) )
2019-09-29 15:00:54 +02:00
val = std : : make_shared < ExprEngine : : UninitValue > ( ) ;
2019-09-29 17:32:26 +02:00
else if ( var & & var - > valueType ( ) ) {
: : ValueType vt ( * var - > valueType ( ) ) ;
vt . pointer = 0 ;
val = getValueRangeFromValueType ( data - > getNewSymbolName ( ) , & vt , * data - > settings ) ;
2019-09-29 15:00:54 +02:00
}
assign ( ExprEngine : : ValuePtr ( ) , val ) ;
2019-09-25 18:33:21 +02:00
}
2019-09-17 21:00:59 +02:00
void ExprEngine : : ArrayValue : : assign ( ExprEngine : : ValuePtr index , ExprEngine : : ValuePtr value )
{
2019-09-25 18:33:21 +02:00
if ( ! index )
data . clear ( ) ;
if ( value ) {
ExprEngine : : ArrayValue : : IndexAndValue indexAndValue = { index , value } ;
data . push_back ( indexAndValue ) ;
2019-09-17 21:00:59 +02:00
}
}
2019-09-24 22:22:16 +02:00
void ExprEngine : : ArrayValue : : clear ( )
{
2019-09-25 18:33:21 +02:00
data . clear ( ) ;
ExprEngine : : ArrayValue : : IndexAndValue indexAndValue = {
ExprEngine : : ValuePtr ( ) , std : : make_shared < ExprEngine : : IntRange > ( " 0 " , 0 , 0 )
} ;
data . push_back ( indexAndValue ) ;
}
static bool isEqual ( ExprEngine : : ValuePtr v1 , ExprEngine : : ValuePtr v2 )
{
if ( ! v1 | | ! v2 )
return ! v1 & & ! v2 ;
2019-09-26 10:03:58 +02:00
return v1 - > name = = v2 - > name ;
2019-09-24 22:22:16 +02:00
}
2019-09-25 18:33:21 +02:00
static bool isNonOverlapping ( ExprEngine : : ValuePtr v1 , ExprEngine : : ValuePtr v2 )
2019-09-17 21:00:59 +02:00
{
2019-09-25 18:33:21 +02:00
if ( ! v1 | | ! v2 )
return false ; // Don't know!
auto intRange1 = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( v1 ) ;
auto intRange2 = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( v2 ) ;
if ( intRange1 & & intRange2 & & ( intRange1 - > minValue > intRange2 - > maxValue | | intRange1 - > maxValue < intRange2 - > maxValue ) )
return true ;
return false ;
}
2019-09-26 10:03:58 +02:00
ExprEngine : : ConditionalValue : : Vector ExprEngine : : ArrayValue : : read ( ExprEngine : : ValuePtr index ) const
2019-09-25 18:33:21 +02:00
{
2019-09-26 10:03:58 +02:00
ExprEngine : : ConditionalValue : : Vector ret ;
2019-10-04 17:58:00 +02:00
if ( ! index )
return ret ;
2019-09-25 18:33:21 +02:00
for ( const auto indexAndValue : data ) {
if ( isEqual ( index , indexAndValue . index ) )
ret . clear ( ) ;
if ( isNonOverlapping ( index , indexAndValue . index ) )
continue ;
2019-09-26 10:03:58 +02:00
// Array contains string literal data...
if ( ! indexAndValue . index & & indexAndValue . value - > type = = ExprEngine : : ValueType : : StringLiteralValue ) {
2019-09-25 18:33:21 +02:00
auto stringLiteral = std : : dynamic_pointer_cast < ExprEngine : : StringLiteralValue > ( indexAndValue . value ) ;
if ( ! stringLiteral ) {
2019-09-26 10:03:58 +02:00
ret . push_back ( std : : pair < ValuePtr , ValuePtr > ( indexAndValue . index , std : : make_shared < ExprEngine : : IntRange > ( " " , - 128 , 128 ) ) ) ;
2019-09-25 18:33:21 +02:00
continue ;
}
if ( auto i = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( index ) ) {
if ( stringLiteral & & i - > minValue > = 0 & & i - > minValue = = i - > maxValue ) {
int c = 0 ;
if ( i - > minValue < stringLiteral - > size ( ) )
c = stringLiteral - > string [ i - > minValue ] ;
2019-09-26 10:03:58 +02:00
ret . push_back ( std : : pair < ValuePtr , ValuePtr > ( indexAndValue . index , std : : make_shared < ExprEngine : : IntRange > ( std : : to_string ( c ) , c , c ) ) ) ;
2019-09-25 18:33:21 +02:00
continue ;
}
}
int cmin = 0 , cmax = 0 ;
for ( char c : stringLiteral - > string ) {
if ( c < cmin )
cmin = c ;
else if ( c > cmax )
cmax = c ;
}
2019-09-26 10:03:58 +02:00
ret . push_back ( std : : pair < ValuePtr , ValuePtr > ( indexAndValue . index , std : : make_shared < ExprEngine : : IntRange > ( " " , cmin , cmax ) ) ) ;
2019-09-25 18:33:21 +02:00
continue ;
}
2019-09-29 21:19:21 +02:00
// Rename IntRange
if ( auto i = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( indexAndValue . value ) ) {
ret . push_back ( std : : pair < ValuePtr , ValuePtr > ( indexAndValue . index , std : : make_shared < ExprEngine : : IntRange > ( indexAndValue . value - > name + " : " + index - > name , i - > minValue , i - > maxValue ) ) ) ;
continue ;
}
2019-09-26 10:03:58 +02:00
ret . push_back ( std : : pair < ValuePtr , ValuePtr > ( indexAndValue . index , indexAndValue . value ) ) ;
2019-09-17 21:00:59 +02:00
}
2019-09-26 10:03:58 +02:00
if ( ret . size ( ) = = 1 )
ret [ 0 ] . first = ExprEngine : : ValuePtr ( ) ;
else if ( ret . size ( ) = = 2 & & ! ret [ 0 ] . first ) {
ret [ 0 ] . first = std : : make_shared < ExprEngine : : BinOpResult > ( " != " , index , ret [ 1 ] . first ) ;
ret [ 1 ] . first = std : : make_shared < ExprEngine : : BinOpResult > ( " == " , index , ret [ 1 ] . first ) ;
} else {
// FIXME!!
ret . clear ( ) ;
}
2019-09-25 18:33:21 +02:00
return ret ;
2019-09-17 21:00:59 +02:00
}
2019-09-26 10:03:58 +02:00
std : : string ExprEngine : : ConditionalValue : : getSymbolicExpression ( ) const
{
std : : ostringstream ostr ;
ostr < < " { " ;
bool first = true ;
for ( auto condvalue : values ) {
ValuePtr cond = condvalue . first ;
ValuePtr value = condvalue . second ;
if ( ! first )
ostr < < " , " ;
first = false ;
ostr < < " { "
< < ( cond ? cond - > getSymbolicExpression ( ) : std : : string ( " (null) " ) )
< < " , "
< < value - > getSymbolicExpression ( )
< < " } " ;
}
ostr < < " } " ;
return ostr . str ( ) ;
}
std : : string ExprEngine : : ArrayValue : : getSymbolicExpression ( ) const
2019-09-21 14:17:16 +02:00
{
2019-09-25 18:33:21 +02:00
std : : ostringstream ostr ;
ostr < < " size= " < < ( size ? size - > name : std : : string ( " (null) " ) ) ;
for ( const auto indexAndValue : data ) {
ostr < < " ,[ "
< < ( ! indexAndValue . index ? std : : string ( " : " ) : indexAndValue . index - > name )
< < " ]= "
< < indexAndValue . value - > name ;
2019-09-21 14:17:16 +02:00
}
2019-09-25 18:33:21 +02:00
return ostr . str ( ) ;
2019-09-21 14:17:16 +02:00
}
2019-09-26 19:39:12 +02:00
std : : string ExprEngine : : StructValue : : getSymbolicExpression ( ) const
{
std : : ostringstream ostr ;
ostr < < " { " ;
bool first = true ;
for ( const auto & m : member ) {
const std : : string & memberName = m . first ;
auto memberValue = m . second ;
if ( ! first )
ostr < < " , " ;
first = false ;
ostr < < memberName < < " = " < < ( memberValue ? memberValue - > getSymbolicExpression ( ) : std : : string ( " (null) " ) ) ;
}
ostr < < " } " ;
return ostr . str ( ) ;
}
2019-09-22 10:56:49 +02:00
std : : string ExprEngine : : PointerValue : : getRange ( ) const
{
std : : string r ;
if ( data )
2019-09-26 10:03:58 +02:00
r = " -> " + data - > getSymbolicExpression ( ) ;
2019-09-22 10:56:49 +02:00
if ( null )
r + = std : : string ( r . empty ( ) ? " " : " , " ) + " null " ;
if ( uninitData )
r + = std : : string ( r . empty ( ) ? " " : " , " ) + " ->? " ;
return r ;
}
2019-10-02 17:59:04 +02:00
std : : string ExprEngine : : IntegerTruncation : : getSymbolicExpression ( ) const
2019-09-17 21:00:59 +02:00
{
2019-10-02 17:59:04 +02:00
return sign + std : : to_string ( bits ) + " ( " + inputValue - > getSymbolicExpression ( ) + " ) " ;
2019-09-17 21:00:59 +02:00
}
2019-10-02 17:59:04 +02:00
# ifdef USE_Z3
2019-10-06 14:47:34 +02:00
2019-10-02 17:59:04 +02:00
struct ExprData {
2019-10-05 16:33:40 +02:00
typedef std : : map < std : : string , z3 : : expr > ValueExpr ;
2019-10-02 17:59:04 +02:00
typedef std : : vector < z3 : : expr > AssertionList ;
2019-09-17 21:00:59 +02:00
2019-10-05 16:33:40 +02:00
z3 : : context context ;
2019-10-02 17:59:04 +02:00
ValueExpr valueExpr ;
AssertionList assertionList ;
2019-09-17 21:00:59 +02:00
2019-10-02 17:59:04 +02:00
void addAssertions ( z3 : : solver & solver ) const {
for ( auto assertExpr : assertionList )
solver . add ( assertExpr ) ;
2019-09-17 21:00:59 +02:00
}
2019-10-06 14:47:34 +02:00
z3 : : expr getExpr ( const ExprEngine : : BinOpResult * b ) {
auto op1 = getExpr ( b - > op1 ) ;
auto op2 = getExpr ( b - > op2 ) ;
if ( b - > binop = = " + " )
return op1 + op2 ;
if ( b - > binop = = " - " )
return op1 - op2 ;
if ( b - > binop = = " * " )
return op1 * op2 ;
if ( b - > binop = = " / " )
return op1 / op2 ;
if ( b - > binop = = " % " )
return op1 % op2 ;
if ( b - > binop = = " == " )
return op1 = = op2 ;
if ( b - > binop = = " != " )
return op1 ! = op2 ;
if ( b - > binop = = " >= " )
return op1 > = op2 ;
if ( b - > binop = = " <= " )
return op1 < = op2 ;
if ( b - > binop = = " > " )
return op1 > op2 ;
if ( b - > binop = = " < " )
return op1 < op2 ;
if ( b - > binop = = " && " )
return op1 & & op2 ;
if ( b - > binop = = " || " )
return op1 | | op2 ;
throw std : : runtime_error ( " Internal error: Unhandled operator " ) ;
}
2019-09-23 20:27:13 +02:00
2019-10-06 14:47:34 +02:00
z3 : : expr getExpr ( ExprEngine : : ValuePtr v ) {
if ( auto intRange = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( v ) ) {
if ( intRange - > name [ 0 ] ! = ' $ ' )
return context . int_val ( int64_t ( intRange - > minValue ) ) ;
auto it = valueExpr . find ( v - > name ) ;
if ( it ! = valueExpr . end ( ) )
return it - > second ;
auto e = context . int_const ( v - > name . c_str ( ) ) ;
valueExpr . emplace ( v - > name , e ) ;
if ( intRange - > maxValue < = INT_MAX )
assertionList . push_back ( e < = int ( intRange - > maxValue ) ) ;
if ( intRange - > minValue > = INT_MIN )
assertionList . push_back ( e > = int ( intRange - > minValue ) ) ;
return e ;
}
2019-09-17 21:00:59 +02:00
2019-10-06 14:47:34 +02:00
if ( auto b = std : : dynamic_pointer_cast < ExprEngine : : BinOpResult > ( v ) ) {
return getExpr ( b . get ( ) ) ;
}
2019-09-22 21:14:20 +02:00
2019-10-06 14:47:34 +02:00
if ( auto c = std : : dynamic_pointer_cast < ExprEngine : : ConditionalValue > ( v ) ) {
if ( c - > values . size ( ) = = 1 )
return getExpr ( c - > values [ 0 ] . second ) ;
2019-09-22 21:14:20 +02:00
2019-10-06 14:47:34 +02:00
return z3 : : ite ( getExpr ( c - > values [ 1 ] . first ) ,
getExpr ( c - > values [ 1 ] . second ) ,
getExpr ( c - > values [ 0 ] . second ) ) ;
}
2019-10-03 20:16:06 +02:00
2019-10-06 17:43:30 +02:00
if ( auto integerTruncation = std : : dynamic_pointer_cast < ExprEngine : : IntegerTruncation > ( v ) ) {
return getExpr ( integerTruncation - > inputValue ) ;
//return getExpr(integerTruncation->inputValue) & ((1 << integerTruncation->bits) - 1);
}
2019-10-06 14:47:34 +02:00
if ( v - > type = = ExprEngine : : ValueType : : UninitValue )
return context . int_val ( 0 ) ;
2019-10-03 20:16:06 +02:00
2019-10-06 14:47:34 +02:00
throw std : : runtime_error ( " Internal error: Unhandled value type " ) ;
}
2019-10-03 20:16:06 +02:00
2019-10-06 14:47:34 +02:00
z3 : : expr getConstraintExpr ( ExprEngine : : ValuePtr v ) {
if ( v - > type = = ExprEngine : : ValueType : : IntRange )
return ( getExpr ( v ) ! = 0 ) ;
return getExpr ( v ) ;
}
} ;
2019-10-02 17:59:04 +02:00
# endif
2019-10-05 16:33:40 +02:00
bool ExprEngine : : IntRange : : isIntValueInRange ( DataBase * dataBase , int value ) const
{
if ( value < minValue | | value > maxValue )
return false ;
const Data * data = dynamic_cast < Data * > ( dataBase ) ;
if ( data - > constraints . empty ( ) )
return true ;
# ifdef USE_Z3
// Check the value against the constraints
ExprData exprData ;
z3 : : solver solver ( exprData . context ) ;
2019-10-06 18:26:40 +02:00
try {
z3 : : expr e = exprData . context . int_const ( name . c_str ( ) ) ;
exprData . valueExpr . emplace ( name , e ) ;
for ( auto constraint : dynamic_cast < const Data * > ( dataBase ) - > constraints )
solver . add ( exprData . getConstraintExpr ( constraint ) ) ;
solver . add ( e = = value ) ;
return solver . check ( ) = = z3 : : sat ;
} catch ( const z3 : : exception & exception ) {
//std::cout << exception << std::endl;
return true ; // Safe option is to return true
}
2019-10-05 16:33:40 +02:00
# else
// The value may or may not be in range
return false ;
# endif
}
2019-10-02 21:47:00 +02:00
bool ExprEngine : : BinOpResult : : isIntValueInRange ( ExprEngine : : DataBase * dataBase , int value ) const
2019-09-17 21:00:59 +02:00
{
2019-10-02 17:59:04 +02:00
# ifdef USE_Z3
ExprData exprData ;
2019-10-05 16:33:40 +02:00
z3 : : solver solver ( exprData . context ) ;
2019-10-06 14:47:34 +02:00
z3 : : expr e = exprData . getExpr ( this ) ;
2019-10-02 21:47:00 +02:00
exprData . addAssertions ( solver ) ;
2019-10-03 08:48:05 +02:00
for ( auto constraint : dynamic_cast < const Data * > ( dataBase ) - > constraints )
2019-10-06 14:47:34 +02:00
solver . add ( exprData . getConstraintExpr ( constraint ) ) ;
2019-10-02 21:47:00 +02:00
solver . add ( e = = value ) ;
return solver . check ( ) = = z3 : : sat ;
2019-10-02 17:59:04 +02:00
# else
2019-10-02 21:47:00 +02:00
( void ) dataBase ;
2019-10-02 17:59:04 +02:00
( void ) value ;
return false ;
# endif
2019-09-17 21:00:59 +02:00
}
2019-10-02 21:47:00 +02:00
std : : string ExprEngine : : BinOpResult : : getExpr ( ExprEngine : : DataBase * dataBase ) const
2019-09-17 21:00:59 +02:00
{
2019-10-02 17:59:04 +02:00
# ifdef USE_Z3
ExprData exprData ;
2019-10-05 16:33:40 +02:00
z3 : : solver solver ( exprData . context ) ;
2019-10-06 14:47:34 +02:00
z3 : : expr e = exprData . getExpr ( this ) ;
2019-10-02 21:47:00 +02:00
exprData . addAssertions ( solver ) ;
2019-10-03 08:48:05 +02:00
for ( auto constraint : dynamic_cast < const Data * > ( dataBase ) - > constraints )
2019-10-06 14:47:34 +02:00
solver . add ( exprData . getConstraintExpr ( constraint ) ) ;
2019-10-02 21:47:00 +02:00
solver . add ( e ) ;
2019-10-02 17:59:04 +02:00
std : : ostringstream os ;
2019-10-02 21:47:00 +02:00
os < < solver ;
2019-10-02 17:59:04 +02:00
return os . str ( ) ;
# else
2019-10-02 21:47:00 +02:00
( void ) dataBase ;
2019-10-02 17:59:04 +02:00
return " " ;
# endif
2019-09-17 21:00:59 +02:00
}
2019-10-02 17:59:04 +02:00
2019-09-17 21:00:59 +02:00
// Todo: This is taken from ValueFlow and modified.. we should reuse it
2019-09-23 20:27:13 +02:00
static int getIntBitsFromValueType ( const ValueType * vt , const cppcheck : : Platform & platform )
2019-09-17 21:00:59 +02:00
{
2019-09-23 20:27:13 +02:00
if ( ! vt )
return 0 ;
2019-09-17 21:00:59 +02:00
switch ( vt - > type ) {
case ValueType : : Type : : BOOL :
2019-09-23 20:27:13 +02:00
return 1 ;
2019-09-17 21:00:59 +02:00
case ValueType : : Type : : CHAR :
2019-09-23 20:27:13 +02:00
return platform . char_bit ;
2019-09-17 21:00:59 +02:00
case ValueType : : Type : : SHORT :
2019-09-23 20:27:13 +02:00
return platform . short_bit ;
2019-09-17 21:00:59 +02:00
case ValueType : : Type : : INT :
2019-09-23 20:27:13 +02:00
return platform . int_bit ;
2019-09-17 21:00:59 +02:00
case ValueType : : Type : : LONG :
2019-09-23 20:27:13 +02:00
return platform . long_bit ;
2019-09-17 21:00:59 +02:00
case ValueType : : Type : : LONGLONG :
2019-09-23 20:27:13 +02:00
return platform . long_long_bit ;
2019-09-17 21:00:59 +02:00
default :
2019-09-23 20:27:13 +02:00
return 0 ;
2019-09-17 21:00:59 +02:00
} ;
2019-09-23 20:27:13 +02:00
}
static ExprEngine : : ValuePtr getValueRangeFromValueType ( const std : : string & name , const ValueType * vt , const cppcheck : : Platform & platform )
{
if ( ! vt | | ! ( vt - > isIntegral ( ) | | vt - > isFloat ( ) ) | | vt - > pointer )
return ExprEngine : : ValuePtr ( ) ;
2019-09-17 21:00:59 +02:00
2019-09-23 20:27:13 +02:00
int bits = getIntBitsFromValueType ( vt , platform ) ;
2019-09-17 21:00:59 +02:00
if ( bits = = 1 ) {
return std : : make_shared < ExprEngine : : IntRange > ( name , 0 , 1 ) ;
2019-09-23 20:27:13 +02:00
} else if ( bits > 1 ) {
2019-09-17 21:00:59 +02:00
if ( vt - > sign = = ValueType : : Sign : : UNSIGNED ) {
return std : : make_shared < ExprEngine : : IntRange > ( name , 0 , ( ( int128_t ) 1 < < bits ) - 1 ) ;
} else {
return std : : make_shared < ExprEngine : : IntRange > ( name , - ( ( int128_t ) 1 < < ( bits - 1 ) ) , ( ( int128_t ) 1 < < ( bits - 1 ) ) - 1 ) ;
}
}
2019-09-23 20:27:13 +02:00
switch ( vt - > type ) {
case ValueType : : Type : : FLOAT :
return std : : make_shared < ExprEngine : : FloatRange > ( name , std : : numeric_limits < float > : : min ( ) , std : : numeric_limits < float > : : max ( ) ) ;
case ValueType : : Type : : DOUBLE :
return std : : make_shared < ExprEngine : : FloatRange > ( name , std : : numeric_limits < double > : : min ( ) , std : : numeric_limits < double > : : max ( ) ) ;
case ValueType : : Type : : LONGDOUBLE :
return std : : make_shared < ExprEngine : : FloatRange > ( name , std : : numeric_limits < long double > : : min ( ) , std : : numeric_limits < long double > : : max ( ) ) ;
default :
return ExprEngine : : ValuePtr ( ) ;
} ;
2019-09-17 21:00:59 +02:00
}
2019-10-02 21:47:00 +02:00
static void call ( const std : : vector < ExprEngine : : Callback > & callbacks , const Token * tok , ExprEngine : : ValuePtr value , Data * dataBase )
2019-09-17 21:00:59 +02:00
{
if ( value ) {
for ( ExprEngine : : Callback f : callbacks ) {
2019-10-02 21:47:00 +02:00
f ( tok , * value , dataBase ) ;
2019-09-17 21:00:59 +02:00
}
}
}
static ExprEngine : : ValuePtr executeExpression ( const Token * tok , Data & data ) ;
static ExprEngine : : ValuePtr executeReturn ( const Token * tok , Data & data )
{
ExprEngine : : ValuePtr retval = executeExpression ( tok - > astOperand1 ( ) , data ) ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , retval , & data ) ;
2019-09-17 21:00:59 +02:00
return retval ;
}
2019-09-23 20:27:13 +02:00
static ExprEngine : : ValuePtr truncateValue ( ExprEngine : : ValuePtr val , const ValueType * valueType , Data & data )
{
2019-09-24 13:27:53 +02:00
if ( ! valueType )
return val ;
2019-09-23 20:27:13 +02:00
if ( valueType - > pointer ! = 0 )
return val ;
if ( ! valueType - > isIntegral ( ) )
return val ; // TODO
int bits = getIntBitsFromValueType ( valueType , * data . settings ) ;
if ( bits = = 0 )
// TODO
return val ;
if ( auto range = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( val ) ) {
if ( range - > minValue = = range - > maxValue ) {
2019-09-28 19:28:12 +02:00
int128_t newValue = truncateInt ( range - > minValue , bits , valueType - > sign = = ValueType : : Sign : : SIGNED ? ' s ' : ' u ' ) ;
2019-09-23 20:27:13 +02:00
if ( newValue = = range - > minValue )
return val ;
return std : : make_shared < ExprEngine : : IntRange > ( ExprEngine : : str ( newValue ) , newValue , newValue ) ;
}
if ( auto typeRange = getValueRangeFromValueType ( " " , valueType , * data . settings ) ) {
auto typeIntRange = std : : dynamic_pointer_cast < ExprEngine : : IntRange > ( typeRange ) ;
if ( typeIntRange ) {
if ( range - > minValue > = typeIntRange - > minValue & & range - > maxValue < = typeIntRange - > maxValue )
return val ;
}
}
return std : : make_shared < ExprEngine : : IntegerTruncation > ( data . getNewSymbolName ( ) , val , bits , valueType - > sign = = ValueType : : Sign : : SIGNED ? ' s ' : ' u ' ) ;
}
// TODO
return val ;
}
2019-09-17 21:00:59 +02:00
static ExprEngine : : ValuePtr executeAssign ( const Token * tok , Data & data )
{
ExprEngine : : ValuePtr rhsValue = executeExpression ( tok - > astOperand2 ( ) , data ) ;
2019-09-24 13:27:53 +02:00
ExprEngine : : ValuePtr assignValue ;
2019-09-21 21:15:51 +02:00
if ( tok - > str ( ) = = " = " )
2019-09-24 13:27:53 +02:00
assignValue = rhsValue ;
2019-09-21 21:15:51 +02:00
else {
// "+=" => "+"
std : : string binop ( tok - > str ( ) ) ;
binop = binop . substr ( 0 , binop . size ( ) - 1 ) ;
ExprEngine : : ValuePtr lhsValue = executeExpression ( tok - > astOperand1 ( ) , data ) ;
2019-09-25 18:33:21 +02:00
assignValue = simplifyValue ( std : : make_shared < ExprEngine : : BinOpResult > ( binop , lhsValue , rhsValue ) ) ;
2019-09-21 21:15:51 +02:00
}
2019-09-17 21:00:59 +02:00
const Token * lhsToken = tok - > astOperand1 ( ) ;
2019-09-24 13:27:53 +02:00
assignValue = truncateValue ( assignValue , lhsToken - > valueType ( ) , data ) ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , assignValue , & data ) ;
2019-09-24 13:27:53 +02:00
2019-09-17 21:00:59 +02:00
if ( lhsToken - > varId ( ) > 0 ) {
2019-09-26 20:48:33 +02:00
data . assignValue ( lhsToken , lhsToken - > varId ( ) , assignValue ) ;
2019-09-17 21:00:59 +02:00
} else if ( lhsToken - > str ( ) = = " [ " ) {
auto arrayValue = data . getArrayValue ( lhsToken - > astOperand1 ( ) ) ;
if ( arrayValue ) {
2019-09-21 11:36:34 +02:00
// Is it array initialization?
const Token * arrayInit = lhsToken - > astOperand1 ( ) ;
if ( arrayInit & & arrayInit - > variable ( ) & & arrayInit - > variable ( ) - > nameToken ( ) = = arrayInit ) {
2019-09-26 10:03:58 +02:00
if ( assignValue - > type = = ExprEngine : : ValueType : : StringLiteralValue )
2019-09-25 18:33:21 +02:00
arrayValue - > assign ( ExprEngine : : ValuePtr ( ) , assignValue ) ;
2019-09-21 11:36:34 +02:00
} else {
auto indexValue = executeExpression ( lhsToken - > astOperand2 ( ) , data ) ;
2019-09-24 13:27:53 +02:00
arrayValue - > assign ( indexValue , assignValue ) ;
2019-09-21 11:36:34 +02:00
}
2019-09-17 21:00:59 +02:00
}
} else if ( lhsToken - > isUnaryOp ( " * " ) ) {
auto pval = executeExpression ( lhsToken - > astOperand1 ( ) , data ) ;
2019-09-26 10:03:58 +02:00
if ( pval & & pval - > type = = ExprEngine : : ValueType : : AddressOfValue ) {
2019-09-17 21:00:59 +02:00
auto val = std : : dynamic_pointer_cast < ExprEngine : : AddressOfValue > ( pval ) ;
if ( val )
2019-09-26 20:48:33 +02:00
data . assignValue ( lhsToken , val - > varId , assignValue ) ;
2019-09-26 10:03:58 +02:00
} else if ( pval & & pval - > type = = ExprEngine : : ValueType : : BinOpResult ) {
2019-09-22 15:58:55 +02:00
auto b = std : : dynamic_pointer_cast < ExprEngine : : BinOpResult > ( pval ) ;
if ( b & & b - > binop = = " + " ) {
std : : shared_ptr < ExprEngine : : ArrayValue > arr ;
ExprEngine : : ValuePtr offset ;
2019-09-26 10:03:58 +02:00
if ( b - > op1 - > type = = ExprEngine : : ValueType : : ArrayValue ) {
2019-09-22 15:58:55 +02:00
arr = std : : dynamic_pointer_cast < ExprEngine : : ArrayValue > ( b - > op1 ) ;
offset = b - > op2 ;
} else {
arr = std : : dynamic_pointer_cast < ExprEngine : : ArrayValue > ( b - > op2 ) ;
offset = b - > op1 ;
}
if ( arr & & offset ) {
2019-09-24 13:27:53 +02:00
arr - > assign ( offset , assignValue ) ;
2019-09-22 15:58:55 +02:00
}
}
2019-09-17 21:00:59 +02:00
}
2019-09-26 21:35:29 +02:00
} else if ( Token : : Match ( lhsToken , " . %name% " ) ) {
auto structVal = executeExpression ( lhsToken - > astOperand1 ( ) , data ) ;
if ( structVal & & structVal - > type = = ExprEngine : : ValueType : : StructValue )
data . assignStructMember ( tok , & * std : : static_pointer_cast < ExprEngine : : StructValue > ( structVal ) , lhsToken - > strAt ( 1 ) , assignValue ) ;
2019-09-17 21:00:59 +02:00
}
2019-09-24 13:27:53 +02:00
return assignValue ;
2019-09-17 21:00:59 +02:00
}
static ExprEngine : : ValuePtr executeFunctionCall ( const Token * tok , Data & data )
{
2019-09-24 22:22:16 +02:00
std : : vector < ExprEngine : : ValuePtr > argValues ;
2019-09-21 19:34:06 +02:00
for ( const Token * argtok : getArguments ( tok ) ) {
auto val = executeExpression ( argtok , data ) ;
2019-09-24 22:22:16 +02:00
argValues . push_back ( val ) ;
2019-09-24 19:53:33 +02:00
if ( ! argtok - > valueType ( ) | | ( argtok - > valueType ( ) - > constness & 1 ) = = 1 )
continue ;
2019-09-21 19:34:06 +02:00
if ( auto arrayValue = std : : dynamic_pointer_cast < ExprEngine : : ArrayValue > ( val ) ) {
ValueType vt ( * argtok - > valueType ( ) ) ;
vt . pointer = 0 ;
auto anyVal = getValueRangeFromValueType ( data . getNewSymbolName ( ) , & vt , * data . settings ) ;
2019-09-25 18:33:21 +02:00
arrayValue - > assign ( ExprEngine : : ValuePtr ( ) , anyVal ) ;
2019-09-24 19:53:33 +02:00
} else if ( auto addressOf = std : : dynamic_pointer_cast < ExprEngine : : AddressOfValue > ( val ) ) {
ValueType vt ( * argtok - > valueType ( ) ) ;
vt . pointer = 0 ;
if ( vt . isIntegral ( ) & & argtok - > valueType ( ) - > pointer = = 1 )
2019-09-26 20:48:33 +02:00
data . assignValue ( argtok , addressOf - > varId , getValueRangeFromValueType ( data . getNewSymbolName ( ) , & vt , * data . settings ) ) ;
2019-09-21 19:34:06 +02:00
}
}
2019-09-24 22:22:16 +02:00
2019-09-28 11:55:06 +02:00
if ( ! tok - > valueType ( ) & & tok - > astParent ( ) )
2019-09-28 10:59:28 +02:00
throw std : : runtime_error ( " Expression ' " + tok - > expressionString ( ) + " ' has unknown type! " ) ;
2019-09-17 21:00:59 +02:00
auto val = getValueRangeFromValueType ( data . getNewSymbolName ( ) , tok - > valueType ( ) , * data . settings ) ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , val , & data ) ;
2019-09-17 21:00:59 +02:00
return val ;
}
static ExprEngine : : ValuePtr executeArrayIndex ( const Token * tok , Data & data )
{
auto arrayValue = data . getArrayValue ( tok - > astOperand1 ( ) ) ;
if ( arrayValue ) {
auto indexValue = executeExpression ( tok - > astOperand2 ( ) , data ) ;
2019-09-26 10:03:58 +02:00
auto conditionalValues = arrayValue - > read ( indexValue ) ;
for ( auto value : conditionalValues )
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , value . second , & data ) ;
2019-09-29 21:19:21 +02:00
if ( conditionalValues . size ( ) = = 1 & & ! conditionalValues [ 0 ] . first )
return conditionalValues [ 0 ] . second ;
2019-09-26 10:03:58 +02:00
return std : : make_shared < ExprEngine : : ConditionalValue > ( data . getNewSymbolName ( ) , conditionalValues ) ;
2019-09-17 21:00:59 +02:00
}
2019-09-28 19:28:12 +02:00
// TODO: Pointer value..
executeExpression ( tok - > astOperand1 ( ) , data ) ;
executeExpression ( tok - > astOperand2 ( ) , data ) ;
2019-09-17 21:00:59 +02:00
return ExprEngine : : ValuePtr ( ) ;
}
2019-09-28 15:40:00 +02:00
static ExprEngine : : ValuePtr executeCast ( const Token * tok , Data & data )
{
const Token * expr = tok - > astOperand2 ( ) ? tok - > astOperand2 ( ) : tok - > astOperand1 ( ) ;
auto val = executeExpression ( expr , data ) ;
if ( expr - > valueType ( ) & & expr - > valueType ( ) - > type = = : : ValueType : : Type : : VOID & & expr - > valueType ( ) - > pointer > 0 ) {
: : ValueType vt ( * tok - > valueType ( ) ) ;
vt . pointer = 0 ;
auto range = getValueRangeFromValueType ( data . getNewSymbolName ( ) , & vt , * data . settings ) ;
if ( tok - > valueType ( ) - > pointer = = 0 )
return range ;
bool uninit = false , null = false ;
if ( val & & val - > type = = ExprEngine : : ValueType : : PointerValue ) {
null = std : : static_pointer_cast < ExprEngine : : PointerValue > ( val ) - > null ;
uninit = std : : static_pointer_cast < ExprEngine : : PointerValue > ( val ) - > uninitData ;
}
return std : : make_shared < ExprEngine : : PointerValue > ( data . getNewSymbolName ( ) , range , null , uninit ) ;
}
if ( val )
// TODO: Cast this..
return val ;
return getValueRangeFromValueType ( data . getNewSymbolName ( ) , tok - > valueType ( ) , * data . settings ) ;
}
2019-09-17 21:00:59 +02:00
static ExprEngine : : ValuePtr executeDot ( const Token * tok , Data & data )
{
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand1 ( ) - > varId ( ) )
return ExprEngine : : ValuePtr ( ) ;
2019-09-19 20:18:39 +02:00
std : : shared_ptr < ExprEngine : : StructValue > structValue = std : : dynamic_pointer_cast < ExprEngine : : StructValue > ( data . getValue ( tok - > astOperand1 ( ) - > varId ( ) , nullptr , nullptr ) ) ;
2019-09-29 08:26:09 +02:00
if ( ! structValue ) {
if ( tok - > originalName ( ) = = " -> " ) {
std : : shared_ptr < ExprEngine : : PointerValue > pointerValue = std : : dynamic_pointer_cast < ExprEngine : : PointerValue > ( data . getValue ( tok - > astOperand1 ( ) - > varId ( ) , nullptr , nullptr ) ) ;
if ( pointerValue ) {
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok - > astOperand1 ( ) , pointerValue , & data ) ;
2019-09-29 08:26:09 +02:00
structValue = std : : dynamic_pointer_cast < ExprEngine : : StructValue > ( pointerValue - > data ) ;
} else {
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok - > astOperand1 ( ) , data . getValue ( tok - > astOperand1 ( ) - > varId ( ) , nullptr , nullptr ) , & data ) ;
2019-09-29 08:26:09 +02:00
}
}
if ( ! structValue )
return ExprEngine : : ValuePtr ( ) ;
}
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok - > astOperand1 ( ) , structValue , & data ) ;
2019-09-17 21:00:59 +02:00
return structValue - > getValueOfMember ( tok - > astOperand2 ( ) - > str ( ) ) ;
}
static ExprEngine : : ValuePtr executeBinaryOp ( const Token * tok , Data & data )
{
ExprEngine : : ValuePtr v1 = executeExpression ( tok - > astOperand1 ( ) , data ) ;
ExprEngine : : ValuePtr v2 = executeExpression ( tok - > astOperand2 ( ) , data ) ;
if ( v1 & & v2 ) {
2019-09-25 18:33:21 +02:00
auto result = simplifyValue ( std : : make_shared < ExprEngine : : BinOpResult > ( tok - > str ( ) , v1 , v2 ) ) ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , result , & data ) ;
2019-09-17 21:00:59 +02:00
return result ;
}
return ExprEngine : : ValuePtr ( ) ;
}
static ExprEngine : : ValuePtr executeAddressOf ( const Token * tok , Data & data )
{
auto addr = std : : make_shared < ExprEngine : : AddressOfValue > ( data . getNewSymbolName ( ) , tok - > astOperand1 ( ) - > varId ( ) ) ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , addr , & data ) ;
2019-09-17 21:00:59 +02:00
return addr ;
}
static ExprEngine : : ValuePtr executeDeref ( const Token * tok , Data & data )
{
ExprEngine : : ValuePtr pval = executeExpression ( tok - > astOperand1 ( ) , data ) ;
if ( pval ) {
auto addressOf = std : : dynamic_pointer_cast < ExprEngine : : AddressOfValue > ( pval ) ;
if ( addressOf ) {
2019-09-19 20:18:39 +02:00
auto val = data . getValue ( addressOf - > varId , tok - > valueType ( ) , tok ) ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , val , & data ) ;
2019-09-17 21:00:59 +02:00
return val ;
}
auto pointer = std : : dynamic_pointer_cast < ExprEngine : : PointerValue > ( pval ) ;
if ( pointer ) {
auto val = pointer - > data ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , val , & data ) ;
2019-09-17 21:00:59 +02:00
return val ;
}
}
return ExprEngine : : ValuePtr ( ) ;
}
static ExprEngine : : ValuePtr executeVariable ( const Token * tok , Data & data )
{
2019-09-19 20:18:39 +02:00
auto val = data . getValue ( tok - > varId ( ) , tok - > valueType ( ) , tok ) ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , val , & data ) ;
2019-09-17 21:00:59 +02:00
return val ;
}
2019-09-23 18:10:06 +02:00
static ExprEngine : : ValuePtr executeKnownMacro ( const Token * tok , Data & data )
{
auto val = std : : make_shared < ExprEngine : : IntRange > ( data . getNewSymbolName ( ) , tok - > getKnownIntValue ( ) , tok - > getKnownIntValue ( ) ) ;
2019-10-02 21:47:00 +02:00
call ( data . callbacks , tok , val , & data ) ;
2019-09-23 18:10:06 +02:00
return val ;
}
2019-09-17 21:00:59 +02:00
static ExprEngine : : ValuePtr executeNumber ( const Token * tok )
{
2019-09-22 21:14:20 +02:00
if ( tok - > valueType ( ) - > isFloat ( ) ) {
long double value = MathLib : : toDoubleNumber ( tok - > str ( ) ) ;
return std : : make_shared < ExprEngine : : FloatRange > ( tok - > str ( ) , value , value ) ;
}
2019-09-17 21:00:59 +02:00
int128_t value = MathLib : : toLongNumber ( tok - > str ( ) ) ;
return std : : make_shared < ExprEngine : : IntRange > ( tok - > str ( ) , value , value ) ;
}
2019-09-21 11:36:34 +02:00
static ExprEngine : : ValuePtr executeStringLiteral ( const Token * tok , Data & data )
{
std : : string s = tok - > str ( ) ;
return std : : make_shared < ExprEngine : : StringLiteralValue > ( data . getNewSymbolName ( ) , s . substr ( 1 , s . size ( ) - 2 ) ) ;
}
2019-09-28 16:16:36 +02:00
static ExprEngine : : ValuePtr executeExpression1 ( const Token * tok , Data & data )
2019-09-17 21:00:59 +02:00
{
if ( tok - > str ( ) = = " return " )
return executeReturn ( tok , data ) ;
2019-09-21 21:15:51 +02:00
if ( tok - > isAssignmentOp ( ) )
// TODO: Handle more operators
2019-09-17 21:00:59 +02:00
return executeAssign ( tok , data ) ;
if ( tok - > astOperand1 ( ) & & tok - > astOperand2 ( ) & & tok - > str ( ) = = " [ " )
return executeArrayIndex ( tok , data ) ;
2019-09-28 15:40:00 +02:00
if ( tok - > str ( ) = = " ( " ) {
if ( ! tok - > isCast ( ) )
return executeFunctionCall ( tok , data ) ;
return executeCast ( tok , data ) ;
}
2019-09-17 21:00:59 +02:00
if ( tok - > str ( ) = = " . " )
return executeDot ( tok , data ) ;
if ( tok - > astOperand1 ( ) & & tok - > astOperand2 ( ) )
return executeBinaryOp ( tok , data ) ;
if ( tok - > isUnaryOp ( " & " ) & & Token : : Match ( tok - > astOperand1 ( ) , " %var% " ) )
return executeAddressOf ( tok , data ) ;
if ( tok - > isUnaryOp ( " * " ) )
return executeDeref ( tok , data ) ;
if ( tok - > varId ( ) )
return executeVariable ( tok , data ) ;
2019-09-23 18:10:06 +02:00
if ( tok - > isName ( ) & & tok - > hasKnownIntValue ( ) )
return executeKnownMacro ( tok , data ) ;
2019-09-24 20:10:51 +02:00
if ( tok - > isNumber ( ) | | tok - > tokType ( ) = = Token : : Type : : eChar )
2019-09-17 21:00:59 +02:00
return executeNumber ( tok ) ;
2019-09-21 11:36:34 +02:00
if ( tok - > tokType ( ) = = Token : : Type : : eString )
return executeStringLiteral ( tok , data ) ;
2019-09-17 21:00:59 +02:00
return ExprEngine : : ValuePtr ( ) ;
}
2019-09-28 16:16:36 +02:00
static ExprEngine : : ValuePtr executeExpression ( const Token * tok , Data & data )
{
return translateUninitValueToRange ( executeExpression1 ( tok , data ) , tok - > valueType ( ) , data ) ;
}
2019-09-26 19:39:12 +02:00
static ExprEngine : : ValuePtr createVariableValue ( const Variable & var , Data & data ) ;
2019-09-17 21:00:59 +02:00
static void execute ( const Token * start , const Token * end , Data & data )
{
for ( const Token * tok = start ; tok ! = end ; tok = tok - > next ( ) ) {
2019-09-21 21:15:51 +02:00
if ( Token : : Match ( tok , " [;{}] " ) )
2019-09-19 20:18:39 +02:00
data . trackProgramState ( tok ) ;
2019-09-29 17:28:12 +02:00
2019-10-06 17:43:30 +02:00
if ( Token : : simpleMatch ( tok , " while ( 0 ) ; " ) ) {
tok = tok - > tokAt ( 4 ) ;
continue ;
}
if ( tok - > str ( ) = = " break " )
return ;
2019-09-29 17:28:12 +02:00
if ( Token : : Match ( tok , " for|while|switch ( " ) )
// TODO this is a bailout
2019-10-04 17:58:00 +02:00
throw std : : runtime_error ( " Unhandled: " + tok - > str ( ) ) ;
2019-09-29 17:28:12 +02:00
// Variable declaration..
2019-09-26 19:39:12 +02:00
if ( tok - > variable ( ) & & tok - > variable ( ) - > nameToken ( ) = = tok ) {
2019-09-27 21:03:47 +02:00
if ( Token : : Match ( tok , " %varid% ; %varid% = " , tok - > varId ( ) ) ) {
2019-09-29 08:26:09 +02:00
// if variable is not used in assignment rhs then we do not need to create a "confusing" variable value..
bool foundInRhs = false ;
visitAstNodes ( tok - > tokAt ( 3 ) - > astOperand2 ( ) , [ & ] ( const Token * rhs ) {
if ( rhs - > varId ( ) = = tok - > varId ( ) ) {
foundInRhs = true ;
return ChildrenToVisit : : done ;
}
return ChildrenToVisit : : op1_and_op2 ;
} ) ;
if ( ! foundInRhs ) {
tok = tok - > tokAt ( 2 ) ;
continue ;
}
2019-09-27 21:03:47 +02:00
}
2019-09-26 19:39:12 +02:00
if ( tok - > variable ( ) - > isArray ( ) ) {
2019-09-29 15:00:54 +02:00
data . assignValue ( tok , tok - > varId ( ) , std : : make_shared < ExprEngine : : ArrayValue > ( & data , tok - > variable ( ) ) ) ;
2019-09-26 19:39:12 +02:00
if ( Token : : Match ( tok , " %name% [ " ) )
tok = tok - > linkAt ( 1 ) ;
} else if ( Token : : Match ( tok , " %var% ; " ) )
2019-09-26 20:48:33 +02:00
data . assignValue ( tok , tok - > varId ( ) , createVariableValue ( * tok - > variable ( ) , data ) ) ;
2019-09-26 19:39:12 +02:00
} else if ( ! tok - > astParent ( ) & & ( tok - > astOperand1 ( ) | | tok - > astOperand2 ( ) ) )
2019-09-17 21:00:59 +02:00
executeExpression ( tok , data ) ;
2019-09-26 19:39:12 +02:00
else if ( Token : : simpleMatch ( tok , " if ( " ) ) {
2019-09-17 21:00:59 +02:00
const Token * cond = tok - > next ( ) - > astOperand2 ( ) ;
2019-10-02 21:47:00 +02:00
const ExprEngine : : ValuePtr condValue = executeExpression ( cond , data ) ;
Data ifData ( data ) ;
Data elseData ( data ) ;
2019-10-03 08:48:05 +02:00
ifData . addConstraint ( condValue , true ) ;
elseData . addConstraint ( condValue , false ) ;
2019-10-02 21:47:00 +02:00
2019-09-17 21:00:59 +02:00
const Token * thenStart = tok - > linkAt ( 1 ) - > next ( ) ;
const Token * thenEnd = thenStart - > link ( ) ;
2019-10-04 17:58:00 +02:00
execute ( thenStart - > next ( ) , end , ifData ) ;
2019-09-17 21:00:59 +02:00
if ( Token : : simpleMatch ( thenEnd , " } else { " ) ) {
const Token * elseStart = thenEnd - > tokAt ( 2 ) ;
2019-10-02 21:47:00 +02:00
execute ( elseStart - > next ( ) , end , elseData ) ;
2019-10-06 18:26:40 +02:00
} else {
execute ( thenEnd , end , elseData ) ;
2019-09-17 21:00:59 +02:00
}
return ;
}
if ( Token : : simpleMatch ( tok , " } else { " ) )
tok = tok - > linkAt ( 2 ) ;
}
}
2019-09-29 15:00:54 +02:00
void ExprEngine : : executeAllFunctions ( const Tokenizer * tokenizer , const Settings * settings , const std : : vector < ExprEngine : : Callback > & callbacks , std : : ostream & trace )
2019-09-17 21:00:59 +02:00
{
const SymbolDatabase * symbolDatabase = tokenizer - > getSymbolDatabase ( ) ;
for ( const Scope * functionScope : symbolDatabase - > functionScopes ) {
2019-09-27 14:36:33 +02:00
try {
2019-09-29 15:00:54 +02:00
executeFunction ( functionScope , tokenizer , settings , callbacks , trace ) ;
2019-09-28 10:59:28 +02:00
} catch ( const std : : exception & e ) {
2019-09-27 14:36:33 +02:00
// FIXME.. there should not be exceptions
2019-09-28 10:59:28 +02:00
std : : string functionName = functionScope - > function - > name ( ) ;
2019-09-28 11:03:20 +02:00
std : : cout < < " Verify: Aborted analysis of function ' " < < functionName < < " ': " < < e . what ( ) < < std : : endl ;
2019-09-27 14:36:33 +02:00
}
2019-09-17 21:00:59 +02:00
}
}
2019-09-26 19:39:12 +02:00
static ExprEngine : : ValuePtr createStructVal ( const Scope * structScope , bool uninitData , Data & data )
2019-09-17 21:00:59 +02:00
{
2019-09-20 21:27:51 +02:00
if ( ! structScope )
return ExprEngine : : ValuePtr ( ) ;
2019-09-17 21:00:59 +02:00
std : : shared_ptr < ExprEngine : : StructValue > structValue = std : : make_shared < ExprEngine : : StructValue > ( data . getNewSymbolName ( ) ) ;
2019-09-26 19:39:12 +02:00
auto uninitValue = std : : make_shared < ExprEngine : : UninitValue > ( ) ;
2019-09-17 21:00:59 +02:00
for ( const Variable & member : structScope - > varlist ) {
2019-09-26 19:39:12 +02:00
if ( uninitData ) {
if ( member . isPointer ( ) ) {
structValue - > member [ member . name ( ) ] = uninitValue ;
continue ;
}
if ( member . valueType ( ) & & member . valueType ( ) - > type > = : : ValueType : : Type : : CHAR ) {
structValue - > member [ member . name ( ) ] = uninitValue ;
continue ;
}
}
2019-10-05 18:29:41 +02:00
if ( member . valueType ( ) & & member . valueType ( ) - > isIntegral ( ) ) {
ExprEngine : : ValuePtr memberValue = createVariableValue ( member , data ) ;
if ( memberValue )
structValue - > member [ member . name ( ) ] = memberValue ;
}
2019-09-17 21:00:59 +02:00
}
return structValue ;
}
static ExprEngine : : ValuePtr createVariableValue ( const Variable & var , Data & data )
{
2019-09-22 10:56:49 +02:00
if ( ! var . nameToken ( ) )
return ExprEngine : : ValuePtr ( ) ;
const ValueType * valueType = var . valueType ( ) ;
if ( ! valueType | | valueType - > type = = ValueType : : Type : : UNKNOWN_TYPE )
valueType = var . nameToken ( ) - > valueType ( ) ;
2019-09-29 08:26:09 +02:00
if ( ! valueType | | valueType - > type = = ValueType : : Type : : UNKNOWN_TYPE ) {
// variable with unknown type
if ( var . isLocal ( ) & & var . isPointer ( ) & & ! var . isArray ( ) )
return std : : make_shared < ExprEngine : : UninitValue > ( ) ;
2019-09-17 21:00:59 +02:00
return ExprEngine : : ValuePtr ( ) ;
2019-09-29 08:26:09 +02:00
}
2019-09-22 10:56:49 +02:00
if ( valueType - > pointer > 0 ) {
2019-09-29 08:26:09 +02:00
if ( var . isLocal ( ) )
return std : : make_shared < ExprEngine : : UninitValue > ( ) ;
2019-09-22 10:56:49 +02:00
ValueType vt ( * valueType ) ;
vt . pointer = 0 ;
2019-09-22 21:14:20 +02:00
auto range = getValueRangeFromValueType ( data . getNewSymbolName ( ) , & vt , * data . settings ) ;
return std : : make_shared < ExprEngine : : PointerValue > ( data . getNewSymbolName ( ) , range , true , true ) ;
2019-09-22 10:56:49 +02:00
}
2019-09-27 18:58:23 +02:00
if ( var . isArray ( ) )
2019-09-29 15:00:54 +02:00
return std : : make_shared < ExprEngine : : ArrayValue > ( & data , & var ) ;
2019-09-22 10:56:49 +02:00
if ( valueType - > isIntegral ( ) )
return getValueRangeFromValueType ( data . getNewSymbolName ( ) , valueType , * data . settings ) ;
if ( valueType - > type = = ValueType : : Type : : RECORD )
2019-09-26 19:39:12 +02:00
return createStructVal ( valueType - > typeScope , var . isLocal ( ) & & ! var . isStatic ( ) , data ) ;
2019-09-22 10:56:49 +02:00
if ( valueType - > smartPointerType ) {
2019-09-26 19:39:12 +02:00
auto structValue = createStructVal ( valueType - > smartPointerType - > classScope , var . isLocal ( ) & & ! var . isStatic ( ) , data ) ;
2019-09-22 10:56:49 +02:00
return std : : make_shared < ExprEngine : : PointerValue > ( data . getNewSymbolName ( ) , structValue , true , false ) ;
}
2019-09-27 13:12:16 +02:00
if ( valueType - > container & & valueType - > container - > stdStringLike ) {
auto size = std : : make_shared < ExprEngine : : IntRange > ( data . getNewSymbolName ( ) , 0 , ~ 0ULL ) ;
auto value = std : : make_shared < ExprEngine : : IntRange > ( data . getNewSymbolName ( ) , - 128 , 127 ) ;
return std : : make_shared < ExprEngine : : ArrayValue > ( data . getNewSymbolName ( ) , size , value ) ;
}
2019-09-17 21:00:59 +02:00
return ExprEngine : : ValuePtr ( ) ;
}
2019-09-29 15:00:54 +02:00
void ExprEngine : : executeFunction ( const Scope * functionScope , const Tokenizer * tokenizer , const Settings * settings , const std : : vector < ExprEngine : : Callback > & callbacks , std : : ostream & trace )
2019-09-17 21:00:59 +02:00
{
if ( ! functionScope - > bodyStart )
return ;
const Function * function = functionScope - > function ;
if ( ! function )
return ;
2019-09-21 19:34:06 +02:00
if ( functionScope - > bodyStart - > fileIndex ( ) > 0 )
// TODO.. what about functions in headers?
return ;
2019-09-17 21:00:59 +02:00
int symbolValueIndex = 0 ;
2019-09-19 20:18:39 +02:00
TrackExecution trackExecution ;
Data data ( & symbolValueIndex , tokenizer , settings , callbacks , & trackExecution ) ;
2019-09-17 21:00:59 +02:00
2019-09-26 20:48:33 +02:00
for ( const Variable & arg : function - > argumentList )
data . assignValue ( functionScope - > bodyStart , arg . declarationId ( ) , createVariableValue ( arg , data ) ) ;
2019-09-17 21:00:59 +02:00
execute ( functionScope - > bodyStart , functionScope - > bodyEnd , data ) ;
2019-09-19 20:18:39 +02:00
2019-09-29 15:00:54 +02:00
if ( settings - > debugVerification ) {
2019-09-19 20:18:39 +02:00
// TODO generate better output!!
2019-09-29 15:00:54 +02:00
trackExecution . print ( trace ) ;
2019-09-19 20:18:39 +02:00
}
2019-09-17 21:00:59 +02:00
}
void ExprEngine : : runChecks ( ErrorLogger * errorLogger , const Tokenizer * tokenizer , const Settings * settings )
{
2019-10-02 21:47:00 +02:00
std : : function < void ( const Token * , const ExprEngine : : Value & , ExprEngine : : DataBase * ) > divByZero = [ = ] ( const Token * tok , const ExprEngine : : Value & value , ExprEngine : : DataBase * dataBase ) {
2019-09-27 14:36:33 +02:00
if ( ! Token : : Match ( tok - > astParent ( ) , " [/%] " ) )
2019-09-17 21:00:59 +02:00
return ;
2019-10-02 21:47:00 +02:00
if ( tok - > astParent ( ) - > astOperand2 ( ) = = tok & & value . isIntValueInRange ( dataBase , 0 ) ) {
2019-09-17 21:00:59 +02:00
std : : list < const Token * > callstack { tok - > astParent ( ) } ;
2019-09-28 11:03:20 +02:00
ErrorLogger : : ErrorMessage errmsg ( callstack , & tokenizer - > list , Severity : : SeverityType : : error , " verificationDivByZero " , " There is division, cannot determine that there can't be a division by zero. " , CWE ( 369 ) , false ) ;
2019-09-17 21:00:59 +02:00
errorLogger - > reportErr ( errmsg ) ;
}
} ;
2019-10-02 17:59:04 +02:00
# ifdef VERIFY_INTEGEROVERFLOW
2019-10-02 21:47:00 +02:00
std : : function < void ( const Token * , const ExprEngine : : Value & , ExprEngine : : DataBase * ) > integerOverflow = [ & ] ( const Token * tok , const ExprEngine : : Value & value , ExprEngine : : DataBase * dataBase ) {
2019-09-27 14:20:17 +02:00
if ( ! tok - > isArithmeticalOp ( ) | | ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > isIntegral ( ) | | tok - > valueType ( ) - > pointer > 0 )
return ;
const ExprEngine : : BinOpResult * b = dynamic_cast < const ExprEngine : : BinOpResult * > ( & value ) ;
if ( ! b )
return ;
ExprEngine : : BinOpResult : : IntOrFloatValue minValue , maxValue ;
b - > getRange ( & minValue , & maxValue ) ;
if ( minValue . isFloat ( ) | | maxValue . isFloat ( ) )
return ;
int bits = getIntBitsFromValueType ( tok - > valueType ( ) , * settings ) ;
2019-09-28 06:31:47 +02:00
if ( bits = = 0 )
return ;
2019-09-27 14:20:17 +02:00
if ( tok - > valueType ( ) - > sign = = : : ValueType : : Sign : : SIGNED ) {
int128_t v = ( int128_t ) 1 < < ( bits - 1 ) ;
if ( minValue . intValue > = - v & & maxValue . intValue < v )
return ;
} else {
int128_t v = ( int128_t ) 1 < < bits ;
if ( minValue . intValue > = 0 & & maxValue . intValue < v )
return ;
}
std : : string note ;
if ( tok - > valueType ( ) - > sign = = : : ValueType : : Sign : : UNSIGNED )
note = " Note that unsigned integer overflow is defined and will wrap around. " ;
std : : list < const Token * > callstack { tok } ;
ErrorLogger : : ErrorMessage errmsg ( callstack , & tokenizer - > list , Severity : : SeverityType : : error , " verificationIntegerOverflow " , " Integer overflow, " + tok - > valueType ( ) - > str ( ) + " result. " + note , false ) ;
errorLogger - > reportErr ( errmsg ) ;
} ;
2019-10-02 17:59:04 +02:00
# endif
2019-09-27 14:20:17 +02:00
2019-09-17 21:00:59 +02:00
std : : vector < ExprEngine : : Callback > callbacks ;
callbacks . push_back ( divByZero ) ;
2019-09-27 16:13:23 +02:00
# ifdef VERIFY_INTEGEROVERFLOW
2019-09-27 14:20:17 +02:00
callbacks . push_back ( integerOverflow ) ;
2019-09-27 16:13:23 +02:00
# endif
2019-09-29 15:00:54 +02:00
ExprEngine : : executeAllFunctions ( tokenizer , settings , callbacks , std : : cout ) ;
2019-09-17 21:00:59 +02:00
}