Skip to content

Commit 6234e9d

Browse files
committed
Fixed #10483 (FP constParameter with array member and memcpy)
1 parent 05acc13 commit 6234e9d

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/checkother.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,26 @@ void CheckOther::checkConstVariable()
14461446
if (castToNonConst)
14471447
continue;
14481448
}
1449+
// Do not warn if struct data is changed
1450+
{
1451+
bool changeStructData = false;
1452+
for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) {
1453+
if (tok->variable() == var && Token::Match(tok, "%var% .")) {
1454+
const Token *parent = tok;
1455+
while (Token::simpleMatch(parent->astParent(), ".") && parent == parent->astParent()->astOperand1())
1456+
parent = parent->astParent();
1457+
if (parent->valueType() &&
1458+
parent->valueType()->pointer > 0 &&
1459+
parent->valueType()->constness == 0 &&
1460+
isVariableChanged(parent, 1, mSettings, mTokenizer->isCPP())) {
1461+
changeStructData = true;
1462+
break;
1463+
}
1464+
}
1465+
}
1466+
if (changeStructData)
1467+
continue;
1468+
}
14491469

14501470
constVariableError(var, function);
14511471
}

test/testother.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,6 +2757,13 @@ class TestOther : public TestFixture {
27572757
check("class Base { virtual void dostuff(int *p) = 0; };\n" // #10397
27582758
"class Derived: public Base { int x; void dostuff(int *p) override { x = *p; } };");
27592759
ASSERT_EQUALS("", errout.str());
2760+
2761+
check("struct Data { char buf[128]; };\n" // #10483
2762+
"void encrypt(Data& data) {\n"
2763+
" const char a[] = \"asfasd\";\n"
2764+
" memcpy(data.buf, &a, sizeof(a));\n"
2765+
"}");
2766+
ASSERT_EQUALS("", errout.str());
27602767
}
27612768

27622769
void switchRedundantAssignmentTest() {

0 commit comments

Comments
 (0)