Fix another FP divideSizeof (#4009)

* Fix FP divideSizeof

* Format

* Undo

* simpleMatch

* Fix another FP divideSizeof
This commit is contained in:
chrchr-github 2022-04-13 12:24:35 +02:00 committed by GitHub
parent 32978933b5
commit ea65fe2b63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 16 deletions

View File

@ -378,22 +378,16 @@ void CheckSizeof::suspiciousSizeofCalculation()
const Token* lPar = tok->astParent(); const Token* lPar = tok->astParent();
if (lPar && lPar->str() == "(") { if (lPar && lPar->str() == "(") {
const Token* const rPar = lPar->link(); const Token* const rPar = lPar->link();
const Token* varTok = rPar->previous(); const Token* varTok = lPar->astOperand2();
while (varTok && varTok->str() == "]") int derefCount = 0;
varTok = varTok->link()->previous(); while (Token::Match(varTok, "[|*")) {
++derefCount;
varTok = varTok->astOperand1();
}
if (lPar->astParent() && lPar->astParent()->str() == "/") { if (lPar->astParent() && lPar->astParent()->str() == "/") {
const Variable* var = varTok ? varTok->variable() : nullptr; const Variable* var = varTok ? varTok->variable() : nullptr;
if (var && var->isPointer() && !var->isArray() && !(lPar->astOperand2() && lPar->astOperand2()->str() == "*")) if (var && var->isPointer() && !var->isArray() && !(var->valueType() && var->valueType()->pointer <= derefCount))
divideSizeofError(tok); divideSizeofError(tok);
else if (varTok && varTok->str() == "*") {
const Token* arrTok = lPar->astParent()->astOperand1();
arrTok = arrTok ? arrTok->astOperand2() : nullptr;
while (Token::simpleMatch(arrTok, "."))
arrTok = arrTok->astOperand2();
var = arrTok ? arrTok->variable() : nullptr;
if (var && var->isPointer() && !var->isArray())
divideSizeofError(tok);
}
} }
else if (Token::simpleMatch(rPar, ") * sizeof") && rPar->next()->astOperand1() == tok->next()) else if (Token::simpleMatch(rPar, ") * sizeof") && rPar->next()->astOperand1() == tok->next())
multiplySizeofError(tok); multiplySizeofError(tok);

View File

@ -440,11 +440,10 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("int f(const S& s) {\n" check("int f(int** p) {\n"
" int** p;\n"
" return sizeof(p[0]) / 4;\n" " return sizeof(p[0]) / 4;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Division of result of sizeof() on pointer type.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Division of result of sizeof() on pointer type.\n", errout.str());
check("struct S {\n" check("struct S {\n"
" unsigned char* s;\n" " unsigned char* s;\n"
@ -466,6 +465,17 @@ private:
" for (int i = 0; i != sizeof(s->t[0].c) / sizeof(char*); i++) {}\n" " for (int i = 0; i != sizeof(s->t[0].c) / sizeof(char*); i++) {}\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void f(int32_t* buf, size_t len) {\n"
" for (int i = 0; i < len / sizeof(buf[0]); i++) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int32_t*** buf, size_t len) {\n"
" for (int i = 0; i < len / sizeof(**buf[0]); i++) {}\n"
" for (int i = 0; i < len / sizeof(*buf[0][0]); i++) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void checkPointerSizeof() { void checkPointerSizeof() {