2019-10-30 17:57:46 +01:00
|
|
|
|
|
|
|
#include "programmemory.h"
|
2021-01-11 08:00:13 +01:00
|
|
|
#include "mathlib.h"
|
2019-10-30 17:57:46 +01:00
|
|
|
#include "token.h"
|
|
|
|
#include "astutils.h"
|
|
|
|
#include "symboldatabase.h"
|
2020-05-19 08:53:38 +02:00
|
|
|
#include <algorithm>
|
2019-10-30 17:57:46 +01:00
|
|
|
#include <cassert>
|
2020-12-19 21:54:19 +01:00
|
|
|
#include <limits>
|
2020-09-11 23:03:57 +02:00
|
|
|
#include <memory>
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::setValue(nonneg int varid, const ValueFlow::Value &value)
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
values[varid] = value;
|
|
|
|
}
|
2020-09-11 23:03:57 +02:00
|
|
|
const ValueFlow::Value* ProgramMemory::getValue(nonneg int varid) const
|
2019-10-30 17:58:19 +01:00
|
|
|
{
|
2020-02-19 07:55:04 +01:00
|
|
|
const ProgramMemory::Map::const_iterator it = values.find(varid);
|
2020-09-11 23:03:57 +02:00
|
|
|
const bool found = it != values.end() && !it->second.isImpossible();
|
2019-10-30 17:57:46 +01:00
|
|
|
if (found)
|
2020-09-11 23:03:57 +02:00
|
|
|
return &it->second;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgramMemory::getIntValue(nonneg int varid, MathLib::bigint* result) const
|
|
|
|
{
|
|
|
|
const ValueFlow::Value* value = getValue(varid);
|
|
|
|
if (value && value->isIntValue()) {
|
|
|
|
*result = value->intvalue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::setIntValue(nonneg int varid, MathLib::bigint value)
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
values[varid] = ValueFlow::Value(value);
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
bool ProgramMemory::getTokValue(nonneg int varid, const Token** result) const
|
|
|
|
{
|
2020-09-11 23:03:57 +02:00
|
|
|
const ValueFlow::Value* value = getValue(varid);
|
|
|
|
if (value && value->isTokValue()) {
|
|
|
|
*result = value->tokvalue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
2020-08-11 03:08:49 +02:00
|
|
|
bool ProgramMemory::getContainerSizeValue(nonneg int varid, MathLib::bigint* result) const
|
|
|
|
{
|
2020-09-11 23:03:57 +02:00
|
|
|
const ValueFlow::Value* value = getValue(varid);
|
|
|
|
if (value && value->isContainerSizeValue()) {
|
|
|
|
*result = value->intvalue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-08-11 03:08:49 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
void ProgramMemory::setUnknown(nonneg int varid)
|
|
|
|
{
|
|
|
|
values[varid].valueType = ValueFlow::Value::ValueType::UNINIT;
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
bool ProgramMemory::hasValue(nonneg int varid)
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
return values.find(varid) != values.end();
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::swap(ProgramMemory &pm)
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
values.swap(pm.values);
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::clear()
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
values.clear();
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
bool ProgramMemory::empty() const
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
return values.empty();
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::replace(const ProgramMemory &pm)
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
for (auto&& p:pm.values)
|
|
|
|
values[p.first] = p.second;
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:58:19 +01:00
|
|
|
void ProgramMemory::insert(const ProgramMemory &pm)
|
|
|
|
{
|
2019-10-30 17:57:46 +01:00
|
|
|
for (auto&& p:pm.values)
|
|
|
|
values.insert(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool conditionIsFalse(const Token *condition, const ProgramMemory &programMemory)
|
|
|
|
{
|
|
|
|
if (!condition)
|
|
|
|
return false;
|
|
|
|
if (condition->str() == "&&") {
|
|
|
|
return conditionIsFalse(condition->astOperand1(), programMemory) ||
|
|
|
|
conditionIsFalse(condition->astOperand2(), programMemory);
|
|
|
|
}
|
|
|
|
ProgramMemory progmem(programMemory);
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
|
|
|
execute(condition, &progmem, &result, &error);
|
|
|
|
return !error && result == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool conditionIsTrue(const Token *condition, const ProgramMemory &programMemory)
|
|
|
|
{
|
|
|
|
if (!condition)
|
|
|
|
return false;
|
|
|
|
if (condition->str() == "||") {
|
|
|
|
return conditionIsTrue(condition->astOperand1(), programMemory) ||
|
|
|
|
conditionIsTrue(condition->astOperand2(), programMemory);
|
|
|
|
}
|
|
|
|
ProgramMemory progmem(programMemory);
|
|
|
|
bool error = false;
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
execute(condition, &progmem, &result, &error);
|
|
|
|
return !error && result == 1;
|
|
|
|
}
|
|
|
|
|
2021-01-11 08:00:13 +01:00
|
|
|
void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, const Token* endTok, const Settings* settings, bool then)
|
2019-10-30 17:57:46 +01:00
|
|
|
{
|
|
|
|
if (Token::Match(tok, "==|>=|<=|<|>|!=")) {
|
2021-01-11 08:00:13 +01:00
|
|
|
if (then && !Token::Match(tok, "==|>=|<=|<|>"))
|
2019-10-30 17:57:46 +01:00
|
|
|
return;
|
|
|
|
if (!then && !Token::Match(tok, "<|>|!="))
|
|
|
|
return;
|
|
|
|
ValueFlow::Value truevalue;
|
|
|
|
ValueFlow::Value falsevalue;
|
2021-01-11 08:00:13 +01:00
|
|
|
const Token* vartok = parseCompareInt(tok, truevalue, falsevalue, [&](const Token* t) -> std::vector<MathLib::bigint> {
|
|
|
|
if (t->hasKnownIntValue())
|
|
|
|
return {t->values().front().intvalue};
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
|
|
|
execute(t, &pm, &result, &error);
|
|
|
|
if (!error)
|
|
|
|
return {result};
|
|
|
|
return std::vector<MathLib::bigint>{};
|
|
|
|
});
|
2019-10-30 17:57:46 +01:00
|
|
|
if (!vartok)
|
|
|
|
return;
|
|
|
|
if (vartok->varId() == 0)
|
|
|
|
return;
|
|
|
|
if (!truevalue.isIntValue())
|
|
|
|
return;
|
2020-02-19 07:55:04 +01:00
|
|
|
if (endTok && isVariableChanged(tok->next(), endTok, vartok->varId(), false, settings, true))
|
2019-10-30 17:57:46 +01:00
|
|
|
return;
|
|
|
|
pm.setIntValue(vartok->varId(), then ? truevalue.intvalue : falsevalue.intvalue);
|
|
|
|
} else if (Token::Match(tok, "%var%")) {
|
|
|
|
if (tok->varId() == 0)
|
|
|
|
return;
|
|
|
|
if (then && !astIsPointer(tok) && !astIsBool(tok))
|
|
|
|
return;
|
2020-02-19 07:55:04 +01:00
|
|
|
if (endTok && isVariableChanged(tok->next(), endTok, tok->varId(), false, settings, true))
|
2019-10-30 17:57:46 +01:00
|
|
|
return;
|
|
|
|
pm.setIntValue(tok->varId(), then);
|
|
|
|
} else if (Token::simpleMatch(tok, "!")) {
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, !then);
|
|
|
|
} else if (then && Token::simpleMatch(tok, "&&")) {
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, then);
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand2(), endTok, settings, then);
|
|
|
|
} else if (!then && Token::simpleMatch(tok, "||")) {
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand1(), endTok, settings, then);
|
|
|
|
programMemoryParseCondition(pm, tok->astOperand2(), endTok, settings, then);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fillProgramMemoryFromConditions(ProgramMemory& pm, const Scope* scope, const Token* endTok, const Settings* settings)
|
|
|
|
{
|
|
|
|
if (!scope)
|
|
|
|
return;
|
|
|
|
if (!scope->isLocal())
|
|
|
|
return;
|
|
|
|
assert(scope != scope->nestedIn);
|
|
|
|
fillProgramMemoryFromConditions(pm, scope->nestedIn, endTok, settings);
|
2021-01-11 08:00:13 +01:00
|
|
|
if (scope->type == Scope::eIf || scope->type == Scope::eWhile || scope->type == Scope::eElse || scope->type == Scope::eFor) {
|
|
|
|
const Token* condTok = getCondTokFromEnd(scope->bodyEnd);
|
|
|
|
if (!condTok)
|
2019-10-30 17:57:46 +01:00
|
|
|
return;
|
|
|
|
programMemoryParseCondition(pm, condTok, endTok, settings, scope->type != Scope::eElse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fillProgramMemoryFromConditions(ProgramMemory& pm, const Token* tok, const Settings* settings)
|
|
|
|
{
|
|
|
|
fillProgramMemoryFromConditions(pm, tok->scope(), tok, settings);
|
|
|
|
}
|
|
|
|
|
2020-02-19 07:55:04 +01:00
|
|
|
static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok, const ProgramMemory& state, ProgramMemory::Map vars)
|
2019-10-30 17:57:46 +01:00
|
|
|
{
|
|
|
|
int indentlevel = 0;
|
|
|
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->previous()) {
|
|
|
|
bool setvar = false;
|
|
|
|
if (Token::Match(tok2, "[;{}] %var% = %var% ;")) {
|
|
|
|
for (auto&& p:vars) {
|
|
|
|
if (p.first != tok2->next()->varId())
|
|
|
|
continue;
|
|
|
|
const Token *vartok = tok2->tokAt(3);
|
2020-11-10 16:00:55 +01:00
|
|
|
if (vartok == tok)
|
|
|
|
continue;
|
2019-10-30 17:57:46 +01:00
|
|
|
pm.setValue(vartok->varId(), p.second);
|
|
|
|
setvar = true;
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 07:02:25 +01:00
|
|
|
if (!setvar && (Token::Match(tok2, ";|{|}|%type% %var% =") ||
|
2019-10-30 17:57:46 +01:00
|
|
|
Token::Match(tok2, "[;{}] const| %type% %var% ("))) {
|
|
|
|
const Token *vartok = tok2->next();
|
|
|
|
while (vartok->next()->isName())
|
|
|
|
vartok = vartok->next();
|
|
|
|
if (!pm.hasValue(vartok->varId())) {
|
|
|
|
MathLib::bigint result = 0;
|
|
|
|
bool error = false;
|
|
|
|
execute(vartok->next()->astOperand2(), &pm, &result, &error);
|
|
|
|
if (!error)
|
|
|
|
pm.setIntValue(vartok->varId(), result);
|
2020-02-13 16:27:06 +01:00
|
|
|
else
|
|
|
|
pm.setUnknown(vartok->varId());
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
2020-05-14 14:10:32 +02:00
|
|
|
} else if (!setvar && Token::Match(tok2, "%var% !!=") && isVariableChanged(tok2, 0, nullptr, true)) {
|
|
|
|
const Token *vartok = tok2;
|
|
|
|
if (!pm.hasValue(vartok->varId()))
|
|
|
|
pm.setUnknown(vartok->varId());
|
2019-10-30 17:57:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tok2->str() == "{") {
|
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
|
|
|
--indentlevel;
|
|
|
|
}
|
|
|
|
if (tok2->str() == "}") {
|
|
|
|
const Token *cond = tok2->link();
|
|
|
|
cond = Token::simpleMatch(cond->previous(), ") {") ? cond->linkAt(-1) : nullptr;
|
|
|
|
if (cond && conditionIsFalse(cond->astOperand2(), state))
|
|
|
|
tok2 = cond->previous();
|
|
|
|
else if (cond && conditionIsTrue(cond->astOperand2(), state)) {
|
|
|
|
++indentlevel;
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 16:25:33 +01:00
|
|
|
static void removeModifiedVars(ProgramMemory& pm, const Token* tok, const Token* origin)
|
|
|
|
{
|
|
|
|
for (auto i = pm.values.begin(), last = pm.values.end(); i != last;) {
|
|
|
|
if (isVariableChanged(origin, tok, i->first, false, nullptr, true)) {
|
|
|
|
i = pm.values.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ProgramMemory getInitialProgramState(const Token* tok,
|
2020-01-05 17:00:34 +01:00
|
|
|
const Token* origin,
|
2020-02-19 07:55:04 +01:00
|
|
|
const ProgramMemory::Map& vars = ProgramMemory::Map {})
|
2020-01-05 16:25:33 +01:00
|
|
|
{
|
|
|
|
ProgramMemory pm;
|
|
|
|
if (origin) {
|
|
|
|
fillProgramMemoryFromConditions(pm, origin, nullptr);
|
|
|
|
const ProgramMemory state = pm;
|
|
|
|
fillProgramMemoryFromAssignments(pm, tok, state, vars);
|
|
|
|
removeModifiedVars(pm, tok, origin);
|
|
|
|
}
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
|
2020-02-19 07:55:04 +01:00
|
|
|
void ProgramMemoryState::insert(const ProgramMemory &pm, const Token* origin)
|
|
|
|
{
|
2020-02-21 16:18:41 +01:00
|
|
|
if (origin)
|
|
|
|
for (auto&& p:pm.values)
|
2020-02-19 07:55:04 +01:00
|
|
|
origins.insert(std::make_pair(p.first, origin));
|
|
|
|
state.insert(pm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::replace(const ProgramMemory &pm, const Token* origin)
|
|
|
|
{
|
2020-02-21 16:18:41 +01:00
|
|
|
if (origin)
|
|
|
|
for (auto&& p:pm.values)
|
2020-02-19 07:55:04 +01:00
|
|
|
origins[p.first] = origin;
|
|
|
|
state.replace(pm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& vars)
|
|
|
|
{
|
|
|
|
ProgramMemory pm;
|
|
|
|
fillProgramMemoryFromConditions(pm, tok, nullptr);
|
|
|
|
ProgramMemory local;
|
|
|
|
for (const auto& p:vars) {
|
|
|
|
nonneg int varid = p.first;
|
|
|
|
const ValueFlow::Value &value = p.second;
|
|
|
|
pm.setValue(varid, value);
|
|
|
|
if (value.varId)
|
|
|
|
pm.setIntValue(value.varId, value.varvalue);
|
|
|
|
}
|
|
|
|
local = pm;
|
|
|
|
fillProgramMemoryFromAssignments(pm, tok, local, vars);
|
|
|
|
replace(pm, tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::assume(const Token* tok, bool b)
|
|
|
|
{
|
|
|
|
ProgramMemory pm = state;
|
|
|
|
programMemoryParseCondition(pm, tok, nullptr, nullptr, b);
|
|
|
|
insert(pm, tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramMemoryState::removeModifiedVars(const Token* tok)
|
|
|
|
{
|
|
|
|
for (auto i = state.values.begin(), last = state.values.end(); i != last;) {
|
|
|
|
if (isVariableChanged(origins[i->first], tok, i->first, false, nullptr, true)) {
|
|
|
|
origins.erase(i->first);
|
|
|
|
i = state.values.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramMemory ProgramMemoryState::get(const Token *tok, const ProgramMemory::Map& vars) const
|
|
|
|
{
|
|
|
|
ProgramMemoryState local = *this;
|
|
|
|
local.addState(tok, vars);
|
|
|
|
local.removeModifiedVars(tok);
|
|
|
|
return local.state;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramMemory getProgramMemory(const Token *tok, const ProgramMemory::Map& vars)
|
2020-02-16 16:02:22 +01:00
|
|
|
{
|
|
|
|
ProgramMemory programMemory;
|
2020-02-16 19:58:09 +01:00
|
|
|
for (const auto& p:vars) {
|
2020-02-16 16:02:22 +01:00
|
|
|
const ValueFlow::Value &value = p.second;
|
|
|
|
programMemory.replace(getInitialProgramState(tok, value.tokvalue));
|
|
|
|
programMemory.replace(getInitialProgramState(tok, value.condition));
|
|
|
|
}
|
|
|
|
fillProgramMemoryFromConditions(programMemory, tok, nullptr);
|
|
|
|
ProgramMemory state;
|
2020-02-16 19:58:09 +01:00
|
|
|
for (const auto& p:vars) {
|
2020-02-16 16:02:22 +01:00
|
|
|
nonneg int varid = p.first;
|
|
|
|
const ValueFlow::Value &value = p.second;
|
|
|
|
programMemory.setValue(varid, value);
|
|
|
|
if (value.varId)
|
|
|
|
programMemory.setIntValue(value.varId, value.varvalue);
|
|
|
|
}
|
2020-02-19 07:55:04 +01:00
|
|
|
state = programMemory;
|
2020-02-16 16:02:22 +01:00
|
|
|
fillProgramMemoryFromAssignments(programMemory, tok, state, vars);
|
|
|
|
return programMemory;
|
|
|
|
}
|
|
|
|
|
2019-10-30 17:57:46 +01:00
|
|
|
ProgramMemory getProgramMemory(const Token *tok, nonneg int varid, const ValueFlow::Value &value)
|
|
|
|
{
|
|
|
|
ProgramMemory programMemory;
|
2020-01-05 16:25:33 +01:00
|
|
|
programMemory.replace(getInitialProgramState(tok, value.tokvalue));
|
|
|
|
programMemory.replace(getInitialProgramState(tok, value.condition));
|
2020-02-13 16:27:06 +01:00
|
|
|
fillProgramMemoryFromConditions(programMemory, tok, nullptr);
|
2019-10-30 17:57:46 +01:00
|
|
|
programMemory.setValue(varid, value);
|
|
|
|
if (value.varId)
|
|
|
|
programMemory.setIntValue(value.varId, value.varvalue);
|
|
|
|
const ProgramMemory state = programMemory;
|
|
|
|
fillProgramMemoryFromAssignments(programMemory, tok, state, {{varid, value}});
|
|
|
|
return programMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void execute(const Token *expr,
|
2019-10-30 17:58:19 +01:00
|
|
|
ProgramMemory * const programMemory,
|
|
|
|
MathLib::bigint *result,
|
|
|
|
bool *error)
|
2019-10-30 17:57:46 +01:00
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
*error = true;
|
|
|
|
|
2021-01-13 12:36:26 +01:00
|
|
|
else if (expr->hasKnownIntValue() && !expr->isAssignmentOp()) {
|
2019-10-30 17:57:46 +01:00
|
|
|
*result = expr->values().front().intvalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->isNumber()) {
|
|
|
|
*result = MathLib::toLongNumber(expr->str());
|
|
|
|
if (MathLib::isFloat(expr->str()))
|
|
|
|
*error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->varId() > 0) {
|
|
|
|
if (!programMemory->getIntValue(expr->varId(), result))
|
|
|
|
*error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->isComparisonOp()) {
|
|
|
|
MathLib::bigint result1(0), result2(0);
|
|
|
|
execute(expr->astOperand1(), programMemory, &result1, error);
|
|
|
|
execute(expr->astOperand2(), programMemory, &result2, error);
|
|
|
|
if (expr->str() == "<")
|
|
|
|
*result = result1 < result2;
|
|
|
|
else if (expr->str() == "<=")
|
|
|
|
*result = result1 <= result2;
|
|
|
|
else if (expr->str() == ">")
|
|
|
|
*result = result1 > result2;
|
|
|
|
else if (expr->str() == ">=")
|
|
|
|
*result = result1 >= result2;
|
|
|
|
else if (expr->str() == "==")
|
|
|
|
*result = result1 == result2;
|
|
|
|
else if (expr->str() == "!=")
|
|
|
|
*result = result1 != result2;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->isAssignmentOp()) {
|
|
|
|
execute(expr->astOperand2(), programMemory, result, error);
|
|
|
|
if (!expr->astOperand1() || !expr->astOperand1()->varId())
|
|
|
|
*error = true;
|
|
|
|
if (*error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (expr->str() == "=") {
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), *result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long intValue;
|
|
|
|
if (!programMemory->getIntValue(expr->astOperand1()->varId(), &intValue)) {
|
|
|
|
*error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (expr->str() == "+=")
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), intValue + *result);
|
|
|
|
else if (expr->str() == "-=")
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), intValue - *result);
|
|
|
|
else if (expr->str() == "*=")
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), intValue * *result);
|
|
|
|
else if (expr->str() == "/=" && *result != 0)
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), intValue / *result);
|
|
|
|
else if (expr->str() == "%=" && *result != 0)
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), intValue % *result);
|
|
|
|
else if (expr->str() == "&=")
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), intValue & *result);
|
|
|
|
else if (expr->str() == "|=")
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), intValue | *result);
|
|
|
|
else if (expr->str() == "^=")
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), intValue ^ *result);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Token::Match(expr, "++|--")) {
|
|
|
|
if (!expr->astOperand1() || expr->astOperand1()->varId() == 0U)
|
|
|
|
*error = true;
|
|
|
|
else {
|
|
|
|
long long intValue;
|
|
|
|
if (!programMemory->getIntValue(expr->astOperand1()->varId(), &intValue))
|
|
|
|
*error = true;
|
|
|
|
else {
|
|
|
|
if (intValue == 0 &&
|
|
|
|
expr->str() == "--" &&
|
|
|
|
expr->astOperand1()->variable() &&
|
2020-01-11 14:00:41 +01:00
|
|
|
expr->astOperand1()->variable()->isUnsigned())
|
2019-10-30 17:57:46 +01:00
|
|
|
*error = true; // overflow
|
|
|
|
*result = intValue + (expr->str() == "++" ? 1 : -1);
|
|
|
|
programMemory->setIntValue(expr->astOperand1()->varId(), *result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->isArithmeticalOp() && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
MathLib::bigint result1(0), result2(0);
|
|
|
|
execute(expr->astOperand1(), programMemory, &result1, error);
|
|
|
|
execute(expr->astOperand2(), programMemory, &result2, error);
|
|
|
|
if (expr->str() == "+")
|
|
|
|
*result = result1 + result2;
|
|
|
|
else if (expr->str() == "-")
|
|
|
|
*result = result1 - result2;
|
|
|
|
else if (expr->str() == "*") {
|
|
|
|
if (result2 && (result1 > std::numeric_limits<MathLib::bigint>::max()/result2))
|
|
|
|
*error = true;
|
|
|
|
else
|
|
|
|
*result = result1 * result2;
|
|
|
|
} else if (result2 == 0)
|
|
|
|
*error = true;
|
|
|
|
else if (expr->str() == "/")
|
|
|
|
*result = result1 / result2;
|
|
|
|
else if (expr->str() == "%")
|
|
|
|
*result = result1 % result2;
|
|
|
|
else if (expr->str() == "<<") {
|
|
|
|
if (result2 < 0 || result1 < 0 || result2 >= MathLib::bigint_bits) { // don't perform UB
|
|
|
|
*error= true;
|
|
|
|
} else {
|
|
|
|
*result = result1 << result2;
|
|
|
|
}
|
|
|
|
} else if (expr->str() == ">>") {
|
|
|
|
if (result2 < 0) { // don't perform UB
|
|
|
|
*error=true;
|
|
|
|
} else {
|
|
|
|
*result = result1 >> result2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->str() == "&&") {
|
|
|
|
bool error1 = false;
|
|
|
|
execute(expr->astOperand1(), programMemory, result, &error1);
|
|
|
|
if (!error1 && *result == 0)
|
|
|
|
*result = 0;
|
|
|
|
else {
|
|
|
|
bool error2 = false;
|
|
|
|
execute(expr->astOperand2(), programMemory, result, &error2);
|
2020-12-16 17:25:21 +01:00
|
|
|
if (error1 || error2)
|
2019-10-30 17:57:46 +01:00
|
|
|
*error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->str() == "||") {
|
|
|
|
execute(expr->astOperand1(), programMemory, result, error);
|
2020-12-16 17:25:21 +01:00
|
|
|
if (*result == 1 && *error == false)
|
|
|
|
*result = 1;
|
|
|
|
else if (*result == 0 && *error == false)
|
2019-10-30 17:57:46 +01:00
|
|
|
execute(expr->astOperand2(), programMemory, result, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->str() == "!") {
|
|
|
|
execute(expr->astOperand1(), programMemory, result, error);
|
|
|
|
*result = !(*result);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->str() == "," && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
execute(expr->astOperand1(), programMemory, result, error);
|
|
|
|
execute(expr->astOperand2(), programMemory, result, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (expr->str() == "[" && expr->astOperand1() && expr->astOperand2()) {
|
|
|
|
const Token *tokvalue = nullptr;
|
|
|
|
if (!programMemory->getTokValue(expr->astOperand1()->varId(), &tokvalue)) {
|
|
|
|
auto tokvalue_it = std::find_if(expr->astOperand1()->values().begin(),
|
|
|
|
expr->astOperand1()->values().end(),
|
|
|
|
std::mem_fn(&ValueFlow::Value::isTokValue));
|
|
|
|
if (tokvalue_it == expr->astOperand1()->values().end()) {
|
|
|
|
*error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tokvalue = tokvalue_it->tokvalue;
|
|
|
|
}
|
|
|
|
if (!tokvalue || !tokvalue->isLiteral()) {
|
|
|
|
*error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const std::string strValue = tokvalue->strValue();
|
|
|
|
MathLib::bigint index = 0;
|
|
|
|
execute(expr->astOperand2(), programMemory, &index, error);
|
|
|
|
if (index >= 0 && index < strValue.size())
|
|
|
|
*result = strValue[index];
|
|
|
|
else if (index == strValue.size())
|
|
|
|
*result = 0;
|
|
|
|
else
|
|
|
|
*error = true;
|
2020-08-23 17:17:33 +02:00
|
|
|
} else if (Token::Match(expr->tokAt(-3), "%var% . %name% (")) {
|
2020-08-11 03:08:49 +02:00
|
|
|
const Token* containerTok = expr->tokAt(-3);
|
2020-08-13 17:10:26 +02:00
|
|
|
if (astIsContainer(containerTok)) {
|
2020-08-11 03:08:49 +02:00
|
|
|
Library::Container::Yield yield = containerTok->valueType()->container->getYield(expr->strAt(-1));
|
|
|
|
if (yield == Library::Container::Yield::SIZE) {
|
|
|
|
if (!programMemory->getContainerSizeValue(containerTok->varId(), result))
|
|
|
|
*error = true;
|
|
|
|
} else if (yield == Library::Container::Yield::EMPTY) {
|
|
|
|
MathLib::bigint size = 0;
|
|
|
|
if (!programMemory->getContainerSizeValue(containerTok->varId(), &size))
|
|
|
|
*error = true;
|
|
|
|
*result = (size == 0);
|
|
|
|
} else {
|
|
|
|
*error = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*error = true;
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 17:57:46 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
*error = true;
|
|
|
|
}
|