diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 0a9840fa1..91124b709 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -612,6 +612,7 @@ void CheckOther::checkRedundantAssignment() std::map varAssignments; std::map memAssignments; + std::set initialized; const Token* writtenArgumentsEnd = 0; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { @@ -672,10 +673,12 @@ void CheckOther::checkRedundantAssignment() const Token* param1 = tok->tokAt(2); writtenArgumentsEnd = param1->next(); if (param1->varId() && param1->strAt(1) == "," && !Token::Match(tok, "strcat|strncat|wcscat|wcsncat")) { - std::map::iterator it = memAssignments.find(param1->varId()); - if (it == memAssignments.end()) + if (tok->str() == "memset" && initialized.find(param1->varId()) == initialized.end() && param1->variable() && param1->variable()->isLocal() && param1->variable()->isArray()) + initialized.insert(param1->varId()); + else if (memAssignments.find(param1->varId()) == memAssignments.end()) memAssignments[param1->varId()] = tok; else { + const std::map::iterator it = memAssignments.find(param1->varId()); if (scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok) && warning) redundantCopyInSwitchError(it->second, tok, param1->str()); else if (performance) diff --git a/test/testother.cpp b/test/testother.cpp index fb8cb77a2..d82891b1d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5643,8 +5643,7 @@ private: " memcpy(a, b, 5);\n" " memmove(a, b, 5);\n" "}"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (performance) Buffer 'a' is being written before its old content has been used.\n" - "[test.cpp:3] -> [test.cpp:5]: (performance) Buffer 'a' is being written before its old content has been used.\n" + ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (performance) Buffer 'a' is being written before its old content has been used.\n" "[test.cpp:3]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memset()' with 'sizeof(*a)'?\n" "[test.cpp:4]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memcpy()' with 'sizeof(*a)'?\n" "[test.cpp:5]: (warning, inconclusive) Array 'a' is filled incompletely. Did you forget to multiply the size given to 'memmove()' with 'sizeof(*a)'?\n", errout.str()); @@ -5949,6 +5948,14 @@ private: " memset(a, 0, size);\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #4455 - initialization of local buffer + check("void f(void) {" + " char buf[10];\n" + " memset(buf, 0, 10);\n" + " strcpy(buf, string);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void varFuncNullUB() { // #4482