Fixed #10483 (FP constParameter with array member and memcpy)

This commit is contained in:
Daniel Marjamäki 2021-09-15 21:25:01 +02:00
parent 05acc13582
commit 6234e9ddda
2 changed files with 27 additions and 0 deletions

View File

@ -1446,6 +1446,26 @@ void CheckOther::checkConstVariable()
if (castToNonConst) if (castToNonConst)
continue; continue;
} }
// Do not warn if struct data is changed
{
bool changeStructData = false;
for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) {
if (tok->variable() == var && Token::Match(tok, "%var% .")) {
const Token *parent = tok;
while (Token::simpleMatch(parent->astParent(), ".") && parent == parent->astParent()->astOperand1())
parent = parent->astParent();
if (parent->valueType() &&
parent->valueType()->pointer > 0 &&
parent->valueType()->constness == 0 &&
isVariableChanged(parent, 1, mSettings, mTokenizer->isCPP())) {
changeStructData = true;
break;
}
}
}
if (changeStructData)
continue;
}
constVariableError(var, function); constVariableError(var, function);
} }

View File

@ -2757,6 +2757,13 @@ private:
check("class Base { virtual void dostuff(int *p) = 0; };\n" // #10397 check("class Base { virtual void dostuff(int *p) = 0; };\n" // #10397
"class Derived: public Base { int x; void dostuff(int *p) override { x = *p; } };"); "class Derived: public Base { int x; void dostuff(int *p) override { x = *p; } };");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("struct Data { char buf[128]; };\n" // #10483
"void encrypt(Data& data) {\n"
" const char a[] = \"asfasd\";\n"
" memcpy(data.buf, &a, sizeof(a));\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void switchRedundantAssignmentTest() { void switchRedundantAssignmentTest() {