Skip to content

Commit 884a95d

Browse files
committed
Modified check that hasKnownInt is within integer range
1 parent 920f386 commit 884a95d

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

lib/checktype.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -383,23 +383,18 @@ void CheckType::checkLongCast()
383383
const ValueType *type = tok->astOperand1()->valueType();
384384
if (type && checkTypeCombination(*type, *retVt, *mSettings) &&
385385
type->pointer == 0U &&
386-
type->originalTypeName.empty() &&
387-
!tok->astOperand1()->hasKnownIntValue()) {
386+
type->originalTypeName.empty()) {
388387
std::pair<MathLib::bigint, MathLib::bigint> opRange1, opRange2;
389-
if (!getExpressionResultRange(tok->astOperand1()->astOperand1(), *mSettings, opRange1) || !getExpressionResultRange(tok->astOperand1()->astOperand2(), *mSettings, opRange2)) {
388+
if (tok->astOperand1()->hasKnownIntValue()) {
389+
if (!mSettings->platform.isIntValue(tok->astOperand1()->getKnownIntValue()))
390+
ret = tok;
391+
} else if (!getExpressionResultRange(tok->astOperand1()->astOperand1(), *mSettings, opRange1) || !getExpressionResultRange(tok->astOperand1()->astOperand2(), *mSettings, opRange2)) {
390392
ret = tok;
391-
break;
392-
}
393-
394-
if (!mSettings->platform.isIntValue(opRange1.first) || !mSettings->platform.isIntValue(opRange1.second) ||
395-
!mSettings->platform.isIntValue(opRange2.first) || !mSettings->platform.isIntValue(opRange2.second)) {
393+
} else if (!mSettings->platform.isIntValue(opRange1.first) || !mSettings->platform.isIntValue(opRange1.second) ||
394+
!mSettings->platform.isIntValue(opRange2.first) || !mSettings->platform.isIntValue(opRange2.second)) {
396395
ret = tok;
397-
break;
398-
}
399-
400-
if (!isOperationResultWithinIntRange(tok->astOperand1(), *mSettings, &opRange1, &opRange2)) {
396+
} else if (!isOperationResultWithinIntRange(tok->astOperand1(), *mSettings, &opRange1, &opRange2)) {
401397
ret = tok;
402-
break;
403398
}
404399
}
405400
}

test/testtype.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,13 @@ class TestType : public TestFixture {
477477
check(code3b, dinit(CheckOptions, $.settings = &settings));
478478
ASSERT_EQUALS("[test.cpp:2:5]: (style) int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information. [truncLongCastReturn]\n", errout_str());
479479

480+
const char code3c[] = "long f() {\n"
481+
" unsigned int n = 1U << 20;\n"
482+
" return n << 20;\n"
483+
"}\n";
484+
check(code3c, dinit(CheckOptions, $.settings = &settings));
485+
ASSERT_EQUALS("[test.cpp:3:5]: (style) int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information. [truncLongCastReturn]\n", errout_str());
486+
480487
const char code4a[] = "long f(int x) {\n"
481488
" int y = 0x07FFFF;\n"
482489
" return ((x & y) << (12 & x));\n"

0 commit comments

Comments
 (0)