Fix #12150 FP uninitialized member array, initialized in range for loop (#5627)

This commit is contained in:
chrchr-github 2023-11-06 16:15:47 +01:00 committed by GitHub
parent fc8c244675
commit de2cfb05b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -1063,6 +1063,11 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
assignVar(usage, ftok->next()->varId());
} else if (Token::Match(ftok, "* this . %name% =")) {
assignVar(usage, ftok->tokAt(3)->varId());
} else if (astIsRangeBasedForDecl(ftok)) {
if (const Variable* rangeVar = ftok->astParent()->astOperand1()->variable()) {
if (rangeVar->isReference() && !rangeVar->isConst())
assignVar(usage, ftok->varId());
}
}
// The functions 'clear' and 'Clear' are supposed to initialize variable.

View File

@ -193,6 +193,7 @@ private:
TEST_CASE(uninitVarArray8);
TEST_CASE(uninitVarArray9); // ticket #6957, #6959
TEST_CASE(uninitVarArray10);
TEST_CASE(uninitVarArray11);
TEST_CASE(uninitVarArray2D);
TEST_CASE(uninitVarArray3D);
TEST_CASE(uninitVarCpp11Init1);
@ -3180,6 +3181,20 @@ private:
errout.str());
}
void uninitVarArray11() {
check("class C {\n" // #12150
"private:\n"
" int buf[10];\n"
"public:\n"
" C() {\n"
" for (int& i : buf)\n"
" i = 0;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void uninitVarArray2D() {
check("class John\n"
"{\n"