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
2 changes: 1 addition & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4131,7 +4131,7 @@ static std::list<ValueFlow::Value> truncateValues(std::list<ValueFlow::Value> va
}

if (value.isIntValue() && sz > 0 && sz < sizeof(MathLib::biguint))
value.intvalue = ValueFlow::truncateIntValue(value.intvalue, sz, dst->sign);
value.intvalue = ValueFlow::truncateIntValue(value.intvalue, sz, dst, settings.platform);
}
return values;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/vf_analyzers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ struct ValueFlowAnalyzer : Analyzer {
if (dst) {
const size_t sz = dst->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
if (sz > 0 && sz < sizeof(MathLib::biguint)) {
MathLib::bigint newvalue = ValueFlow::truncateIntValue(value->intvalue, sz, dst->sign);
MathLib::bigint newvalue = ValueFlow::truncateIntValue(value->intvalue, sz, dst, settings.platform);

/* Handle overflow/underflow for value bounds */
if (value->bound != ValueFlow::Value::Bound::Point) {
Expand Down
4 changes: 2 additions & 2 deletions lib/vf_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ namespace ValueFlow
return true;
}

MathLib::bigint truncateIntValue(MathLib::bigint value, size_t value_size, const ValueType::Sign dst_sign)
MathLib::bigint truncateIntValue(MathLib::bigint value, size_t value_size, const ValueType* dst, const Platform& platform)
{
if (value_size == 0)
return value;

const MathLib::biguint unsignedMaxValue = std::numeric_limits<MathLib::biguint>::max() >> ((sizeof(unsignedMaxValue) - value_size) * 8);
const MathLib::biguint signBit = 1ULL << ((value_size * 8) - 1);
value &= unsignedMaxValue;
if (dst_sign == ValueType::Sign::SIGNED && (value & signBit))
if ((dst->sign == ValueType::Sign::SIGNED || (dst->sign == ValueType::Sign::UNKNOWN_SIGN && dst->type == ValueType::Type::CHAR && platform.defaultSign == 's')) && (value & signBit))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

When platform.defaultSign == '\0' (--platform=unspecified), plain char still goes through unsigned truncation. So char c = -1; if (~c == -256) {} still reports Condition '~c==-256' is always true, and char c = 0; c--; if (c == -1) {} reports "always false". When signedness is unknown, a value that sets the sign bit has no single known result.

Also, the cause seems to be that parsedecl (symboldatabase.cpp:7751) leaves char variables with UNKNOWN_SIGN, while symboldatabase.cpp:6929/8337 already apply mDefaultSignedness. Applying it in parsedecl would fix this for every reader of sign, not only these truncation call sites. It would also change the UNKNOWN_SIGN checks at symboldatabase.cpp:1934 and :6020.

value |= ~unsignedMaxValue;

return value;
Expand Down
2 changes: 1 addition & 1 deletion lib/vf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace ValueFlow
{
bool getMinMaxValues(const ValueType* vt, const Platform& platform, MathLib::bigint& minValue, MathLib::bigint& maxValue);

MathLib::bigint truncateIntValue(MathLib::bigint value, size_t value_size, ValueType::Sign dst_sign);
MathLib::bigint truncateIntValue(MathLib::bigint value, size_t value_size, const ValueType* dst, const Platform& platform);

Token * valueFlowSetConstantValue(Token *tok, const Settings &settings);

Expand Down
4 changes: 2 additions & 2 deletions lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ namespace ValueFlow
const ValueType *dst = tok->valueType();
if (dst) {
const size_t sz = dst->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
MathLib::bigint newvalue = ValueFlow::truncateIntValue(v.intvalue + 1, sz, dst->sign);
MathLib::bigint newvalue = ValueFlow::truncateIntValue(v.intvalue + 1, sz, dst, settings.platform);
if (v.bound != ValueFlow::Value::Bound::Point) {
if (newvalue < v.intvalue) {
v.invertBound();
Expand Down Expand Up @@ -695,7 +695,7 @@ namespace ValueFlow
const ValueType *dst = tok->valueType();
if (dst) {
const size_t sz = dst->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
MathLib::bigint newvalue = ValueFlow::truncateIntValue(v.intvalue - 1, sz, dst->sign);
MathLib::bigint newvalue = ValueFlow::truncateIntValue(v.intvalue - 1, sz, dst, settings.platform);
if (v.bound != ValueFlow::Value::Bound::Point) {
if (newvalue > v.intvalue) {
v.invertBound();
Expand Down
7 changes: 7 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,13 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(22, values.back().intvalue);
}

code = "void f() {\n" // #15032
" char c = -1;\n"
" if (~c == -256) {}\n"
"}\n";
const int expected = std::numeric_limits<char>::is_signed ? 0 : -256;
ASSERT_EQUALS(true, testKnownValueOfTok(code, "~", expected));

// #10251 starship operator
code = "struct X {};\n"
"auto operator<=>(const X & a, const X & b) -> decltype(1 <=> 2) {\n"
Expand Down
Loading