* Fix #9931 FP zerodiv (daca@home) * Format * Parentheses * clang-tidy
This commit is contained in:
parent
4a64e20592
commit
6a0c254d1f
|
@ -1206,7 +1206,7 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
|
||||||
|
|
||||||
const Token *tok2 = tok->tokAt(2);
|
const Token *tok2 = tok->tokAt(2);
|
||||||
// skip over tokens to find variable or type
|
// skip over tokens to find variable or type
|
||||||
while (Token::Match(tok2, "%name% ::|.|[")) {
|
while (tok2 && !tok2->isStandardType() && Token::Match(tok2, "%name% ::|.|[")) {
|
||||||
if (tok2->next()->str() == "[")
|
if (tok2->next()->str() == "[")
|
||||||
tok2 = tok2->linkAt(1)->next();
|
tok2 = tok2->linkAt(1)->next();
|
||||||
else
|
else
|
||||||
|
@ -1323,7 +1323,24 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
|
||||||
}
|
}
|
||||||
} else if (!tok2->type()) {
|
} else if (!tok2->type()) {
|
||||||
const ValueType& vt = ValueType::parseDecl(tok2, settings);
|
const ValueType& vt = ValueType::parseDecl(tok2, settings);
|
||||||
const size_t sz = ValueFlow::getSizeOf(vt, settings);
|
size_t sz = ValueFlow::getSizeOf(vt, settings);
|
||||||
|
const Token* brac = tok2->astParent();
|
||||||
|
while (Token::simpleMatch(brac, "[")) {
|
||||||
|
const Token* num = brac->astOperand2();
|
||||||
|
if (num && ((num->isNumber() && MathLib::isInt(num->str())) || num->tokType() == Token::eChar)) {
|
||||||
|
try {
|
||||||
|
MathLib::biguint dim = MathLib::toULongNumber(num->str());
|
||||||
|
sz *= dim;
|
||||||
|
brac = brac->astParent();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
catch (const std::exception& /*e*/) {
|
||||||
|
// Bad integer literal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sz = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (sz > 0) {
|
if (sz > 0) {
|
||||||
ValueFlow::Value value(sz);
|
ValueFlow::Value value(sz);
|
||||||
if (!tok2->isTemplateArg() && settings->platformType != cppcheck::Platform::Unspecified)
|
if (!tok2->isTemplateArg() && settings->platformType != cppcheck::Platform::Unspecified)
|
||||||
|
|
|
@ -66,6 +66,7 @@ private:
|
||||||
TEST_CASE(zeroDiv14); // #1169
|
TEST_CASE(zeroDiv14); // #1169
|
||||||
TEST_CASE(zeroDiv15); // #8319
|
TEST_CASE(zeroDiv15); // #8319
|
||||||
TEST_CASE(zeroDiv16); // #11158
|
TEST_CASE(zeroDiv16); // #11158
|
||||||
|
TEST_CASE(zeroDiv17); // #9931
|
||||||
|
|
||||||
TEST_CASE(zeroDivCond); // division by zero / useless condition
|
TEST_CASE(zeroDivCond); // division by zero / useless condition
|
||||||
|
|
||||||
|
@ -637,6 +638,15 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void zeroDiv17() { // #9931
|
||||||
|
check("int f(int len) {\n"
|
||||||
|
" int sz = sizeof(void*[255]) / 255;\n"
|
||||||
|
" int x = len % sz;\n"
|
||||||
|
" return x;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
}
|
||||||
|
|
||||||
void zeroDivCond() {
|
void zeroDivCond() {
|
||||||
check("void f(unsigned int x) {\n"
|
check("void f(unsigned int x) {\n"
|
||||||
" int y = 17 / x;\n"
|
" int y = 17 / x;\n"
|
||||||
|
|
|
@ -1343,6 +1343,11 @@ private:
|
||||||
values = tokenValues(code,"( arr )");
|
values = tokenValues(code,"( arr )");
|
||||||
ASSERT_EQUALS(1U, values.size());
|
ASSERT_EQUALS(1U, values.size());
|
||||||
ASSERT_EQUALS(10 * sizeof(std::uint16_t), values.back().intvalue);
|
ASSERT_EQUALS(10 * sizeof(std::uint16_t), values.back().intvalue);
|
||||||
|
|
||||||
|
code = "int sz = sizeof(int32_t[10][20]);";
|
||||||
|
values = tokenValues(code, "=");
|
||||||
|
ASSERT_EQUALS(1U, values.size());
|
||||||
|
ASSERT_EQUALS(sizeof(std::int32_t) * 10 * 20, values.back().intvalue);
|
||||||
}
|
}
|
||||||
|
|
||||||
void valueFlowErrorPath() {
|
void valueFlowErrorPath() {
|
||||||
|
|
Loading…
Reference in New Issue