From 6234e9dddadcd9043e3cf5eedd616ec387362934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 15 Sep 2021 21:25:01 +0200 Subject: [PATCH] Fixed #10483 (FP constParameter with array member and memcpy) --- lib/checkother.cpp | 20 ++++++++++++++++++++ test/testother.cpp | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f4fb2a923..e77d35ed6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1446,6 +1446,26 @@ void CheckOther::checkConstVariable() if (castToNonConst) 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); } diff --git a/test/testother.cpp b/test/testother.cpp index 44406a9c3..4b14e7cc0 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2757,6 +2757,13 @@ private: check("class Base { virtual void dostuff(int *p) = 0; };\n" // #10397 "class Derived: public Base { int x; void dostuff(int *p) override { x = *p; } };"); 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() {