Fixed #7175 (False positive performance warning (buffer overwritten before its old content has been used))

This commit is contained in:
Daniel Marjamäki 2016-06-13 15:46:43 +02:00
parent 1f858e955f
commit d1f06ff47c
2 changed files with 30 additions and 0 deletions

View File

@ -573,6 +573,19 @@ void CheckOther::checkRedundantAssignment()
if (it == memAssignments.end()) if (it == memAssignments.end())
memAssignments[param1->varId()] = tok; memAssignments[param1->varId()] = tok;
else { else {
bool read = false;
for (const Token *tok2 = tok->linkAt(1); tok2 != writtenArgumentsEnd; tok2 = tok2->previous()) {
if (tok2->varId() == param1->varId()) {
// TODO: is this a read? maybe it's a write
read = true;
break;
}
}
if (read) {
memAssignments[param1->varId()] = tok;
continue;
}
if (printWarning && scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok)) if (printWarning && scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok))
redundantCopyInSwitchError(it->second, tok, param1->str()); redundantCopyInSwitchError(it->second, tok, param1->str());
else if (printPerformance) else if (printPerformance)

View File

@ -5389,6 +5389,23 @@ private:
" return i;\n" " return i;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #7175 - read+write
check("void f() {\n"
" char buf[100];\n"
" strcpy(buf, x);\n"
" strcpy(buf, dostuff(buf));\n" // <- read + write
" strcpy(buf, x);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" char buf[100];\n"
" strcpy(buf, x);\n"
" strcpy(buf, dostuff(buf));\n"
" strcpy(buf, x);\n"
"}");
TODO_ASSERT_EQUALS("error", "", errout.str());
} }
void varFuncNullUB() { // #4482 void varFuncNullUB() { // #4482