Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,24 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
upper.bound = ValueFlow::Value::Bound::Lower;
upper.setImpossible();
setTokenValue(tok, std::move(upper), settings);
} else if (tok->isCast() && tok->valueType() && tok->valueType()->isIntegral() && !tok->valueType()->pointer) {
MathLib::bigint minValue;
MathLib::bigint maxValue;
if (!ValueFlow::getMinMaxValues(tok->valueType(), settings.platform, minValue, maxValue))
continue;

if (minValue > std::numeric_limits<MathLib::bigint>::min()) {
ValueFlow::Value lower{minValue - 1};
lower.bound = ValueFlow::Value::Bound::Upper;
lower.setImpossible();
setTokenValue(tok, std::move(lower), settings);
}
if (maxValue < std::numeric_limits<MathLib::bigint>::max()) {
ValueFlow::Value upper{maxValue + 1};
upper.bound = ValueFlow::Value::Bound::Lower;
upper.setImpossible();
setTokenValue(tok, std::move(upper), settings);
}
} else if (astIsUnsigned(tok) && !astIsPointer(tok)) {
std::vector<MathLib::bigint> minvalue = minUnsignedValue(tok);
if (minvalue.empty())
Expand Down
18 changes: 13 additions & 5 deletions lib/vf_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace ValueFlow
if (!vt || !vt->isIntegral() || vt->pointer)
return false;

std::uint8_t bits;
std::size_t bits;
switch (vt->type) {
case ValueType::Type::BOOL:
bits = 1;
Expand All @@ -66,29 +66,37 @@ namespace ValueFlow
case ValueType::Type::LONGLONG:
bits = platform.long_long_bit;
break;
case ValueType::Type::WCHAR_T:
bits = platform.sizeof_wchar_t * platform.char_bit;
break;
default:
return false;
}

if (bits == 0) {
return false;
}
if (bits == 1) {
minValue = 0;
maxValue = 1;
} else if (bits < 62) {
if (vt->sign == ValueType::Sign::UNSIGNED) {
minValue = 0;
maxValue = (1LL << bits) - 1;
} else {
} else if (vt->sign == ValueType::Sign::SIGNED) {
minValue = -(1LL << (bits - 1));
maxValue = (1LL << (bits - 1)) - 1;
}
} else
return false;
} else if (bits == 64) {
if (vt->sign == ValueType::Sign::UNSIGNED) {
minValue = 0;
maxValue = LLONG_MAX; // todo max unsigned value
} else {
} else if (vt->sign == ValueType::Sign::SIGNED) {
minValue = LLONG_MIN;
maxValue = LLONG_MAX;
}
} else
return false;
} else {
return false;
}
Expand Down
73 changes: 73 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6546,6 +6546,79 @@ class TestCondition : public TestFixture {
"[test.cpp:4:13]: (style) Comparing expression of type 'const unsigned int &' against value 4294967295. Condition is always false. [compareValueOutOfTypeRangeError]\n",
errout_str());

check("typedef unsigned int uint32;\n"
"typedef unsigned long long uint64;\n"
"typedef long long sint64;\n"
"void f(uint32 x) {\n"
" uint64 tmp = ((uint64)x) + 1ULL;\n"
" if (tmp > 4294967295ULL)\n"
" tmp = 4294967295ULL;\n"
" if ((((sint64)((uint32)tmp)) - 1LL) < 0LL) {}\n"
" if ((((sint64)((uint32)tmp)) - 1LL) > 4294967295LL) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:9:41]: (style) Condition '(((long long)((unsigned int)tmp))-1LL)>4294967295LL' is always false [knownConditionTrueFalse]\n",
errout_str());

check("typedef unsigned int uint32;\n"
"typedef unsigned long long uint64;\n"
"typedef long long sint64;\n"
"void f(uint64 tmp) {\n"
" if (tmp > 4294967295ULL)\n"
" tmp = 4294967295ULL;\n"
" if (static_cast<sint64>(static_cast<uint32>(tmp)) - 1LL > 4294967295LL) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:7:61]: (style) Condition 'static_cast<long long>(static_cast<unsigned int>(tmp))-1LL>4294967295LL' is always false [knownConditionTrueFalse]\n",
errout_str());

// cast directly around variable: both the range-based and the declared-type
// analysis can prove the condition invariant; diag() must prevent duplicates
check("void f(unsigned char c) {\n"
" if ((unsigned char)c > 255) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:28]: (style) Comparing expression of type 'unsigned char' against value 255. Condition is always false. [compareValueOutOfTypeRangeError]\n",
errout_str());

check("void f(unsigned char c) {\n"
" if ((unsigned char)c == 256) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:29]: (style) Comparing expression of type 'unsigned char' against value 256. Condition is always false. [compareValueOutOfTypeRangeError]\n",
errout_str());

check("void f(unsigned int u) {\n"
" if ((unsigned int)u > 4294967295ULL) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:27]: (style) Comparing expression of type 'unsigned int' against value 4294967295. Condition is always false. [compareValueOutOfTypeRangeError]\n",
errout_str());

check("void f(unsigned short s) {\n"
" if ((unsigned int)s > 4294967295ULL) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:27]: (style) Comparing expression of type 'unsigned int' against value 4294967295. Condition is always false. [compareValueOutOfTypeRangeError]\n",
errout_str());

// wchar_t range is derived through ValueType::getSizeOf()
check("void f(wchar_t c) {\n"
" if ((wchar_t)c > 0x7fffffff) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:20]: (style) Condition '(wchar_t)c>0x7fffffff' is always false [knownConditionTrueFalse]\n",
errout_str());

check("void f(unsigned int x) {\n"
" if (-(signed char)x < -129) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:25]: (style) Condition '-(char)x<-129' is always false [knownConditionTrueFalse]\n",
errout_str());

check("void f(int x) {\n"
" if ((x) > 0) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("", errout_str());

check("void f(unsigned int x) {\n"
" if ((unsigned int)x > 0) {}\n"
"}\n", settingsUnix64);
ASSERT_EQUALS("", errout_str());

check("void f() {\n"
" long long ll = 1024 * 1024 * 1024;\n"
" if (ll * 8 < INT_MAX) {}\n"
Expand Down
16 changes: 16 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9572,6 +9572,22 @@ class TestValueFlow : public TestFixture {
"}\n";
ASSERT_EQUALS(true, testValueOfXImpossible(code, 3U, "a", -1));
ASSERT_EQUALS(true, testValueOfXImpossible(code, 3U, -1));

const Settings settingsUnix64 = settingsBuilder().platform(Platform::Type::Unix64).build();
code = "void f(unsigned long long x) {\n"
" return (unsigned int)x;\n"
"}\n";
SimpleTokenizer tokenizer(settingsUnix64, *this);
ASSERT(tokenizer.tokenize(code));
const Token* returnTok = Token::findmatch(tokenizer.tokens(), "return (");
ASSERT(returnTok && returnTok->next());
const std::list<ValueFlow::Value>& castValues = returnTok->next()->values();
ASSERT(std::any_of(castValues.cbegin(), castValues.cend(), [](const ValueFlow::Value& value) {
return value.isImpossible() && value.intvalue == -1;
}));
ASSERT(std::any_of(castValues.cbegin(), castValues.cend(), [](const ValueFlow::Value& value) {
return value.isImpossible() && value.intvalue == 4294967296;
}));
}

void valueFlowImpossibleIncDec()
Expand Down
Loading