Fixed #10547 (False positive: constVariable, buffer written by stream read)

This commit is contained in:
Daniel Marjamäki 2021-10-18 19:38:53 +02:00
parent c49d246303
commit 921f5c1818
2 changed files with 13 additions and 1 deletions

View File

@ -1525,10 +1525,15 @@ void CheckOther::checkConstPointer()
if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(true) || p->scope()->function->hasVirtualSpecifier())
continue;
}
if (nonConstPointers.find(p) == nonConstPointers.end())
if (nonConstPointers.find(p) == nonConstPointers.end()) {
const Token *start = (p->isArgument()) ? p->scope()->bodyStart : p->nameToken()->next();
const int indirect = p->isArray() ? p->dimensions().size() : 1;
if (isVariableChanged(start, p->scope()->bodyEnd, indirect, p->declarationId(), false, mSettings, mTokenizer->isCPP()))
continue;
constVariableError(p, nullptr);
}
}
}
void CheckOther::constVariableError(const Variable *var, const Function *function)
{
const std::string vartype((var && var->isArgument()) ? "Parameter" : "Variable");

View File

@ -2764,6 +2764,13 @@ private:
" memcpy(data.buf, &a, sizeof(a));\n"
"}");
ASSERT_EQUALS("", errout.str());
// #10547
check("void foo(std::istream &istr) {\n"
" unsigned char x[2];\n"
" istr >> x[0];\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void switchRedundantAssignmentTest() {