Skip to content

Commit 55493d0

Browse files
Fix #13812 value potentially truncated in ValueFlowAnalyzer::evaluteInt() (cppcheck-opensource#8482)
1 parent 4d12b1d commit 55493d0

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

lib/vf_analyzers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ struct ValueFlowAnalyzer : Analyzer {
676676
std::vector<MathLib::bigint> evaluateInt(const Token* tok, F getProgramMemory) const
677677
{
678678
if (const ValueFlow::Value* v = tok->getKnownValue(ValueFlow::Value::ValueType::INT))
679-
return {static_cast<int>(v->intvalue)};
679+
return {v->intvalue};
680680
std::vector<MathLib::bigint> result;
681681
ProgramMemory pm = getProgramMemory();
682682
if (Token::Match(tok, "&&|%oror%")) {
@@ -717,7 +717,7 @@ struct ValueFlowAnalyzer : Analyzer {
717717
ProgramMemory pm = pms.get(tok, ctx, getProgramState());
718718
MathLib::bigint out = 0;
719719
if (pm.getContainerEmptyValue(tok->exprId(), out))
720-
return {static_cast<int>(out)};
720+
return {out};
721721
return {};
722722
}
723723
return {};

test/testvalueflow.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class TestValueFlow : public TestFixture {
320320
}
321321

322322
#define testValueOfX(...) testValueOfX_(__FILE__, __LINE__, __VA_ARGS__)
323-
bool testValueOfX_(const char* file, int line, const char code[], unsigned int linenr, int value, const Settings *s = nullptr) {
323+
bool testValueOfX_(const char* file, int line, const char code[], unsigned int linenr, MathLib::bigint value, const Settings *s = nullptr) {
324324
const Settings *settings1 = s ? s : &settings;
325325

326326
// Tokenize..
@@ -3973,6 +3973,14 @@ class TestValueFlow : public TestFixture {
39733973
" return x;\n"
39743974
"}";
39753975
ASSERT_EQUALS(true, testValueOfX(code, 4U, 246));
3976+
3977+
code = "int64_t f() {\n"
3978+
" const int64_t val = 1LL << 33;\n"
3979+
" int64_t x = val;\n"
3980+
" x += val;\n"
3981+
" return x;\n"
3982+
"}";
3983+
ASSERT_EQUALS(true, testValueOfX(code, 5U, 1LL << 34));
39763984
}
39773985

39783986
void valueFlowForwardCorrelatedVariables() {

0 commit comments

Comments
 (0)