Skip to content

Commit de2cfb0

Browse files
Fix #12150 FP uninitialized member array, initialized in range for loop (cppcheck-opensource#5627)
1 parent fc8c244 commit de2cfb0

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

lib/checkclass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,11 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
10631063
assignVar(usage, ftok->next()->varId());
10641064
} else if (Token::Match(ftok, "* this . %name% =")) {
10651065
assignVar(usage, ftok->tokAt(3)->varId());
1066+
} else if (astIsRangeBasedForDecl(ftok)) {
1067+
if (const Variable* rangeVar = ftok->astParent()->astOperand1()->variable()) {
1068+
if (rangeVar->isReference() && !rangeVar->isConst())
1069+
assignVar(usage, ftok->varId());
1070+
}
10661071
}
10671072

10681073
// The functions 'clear' and 'Clear' are supposed to initialize variable.

test/testconstructors.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class TestConstructors : public TestFixture {
193193
TEST_CASE(uninitVarArray8);
194194
TEST_CASE(uninitVarArray9); // ticket #6957, #6959
195195
TEST_CASE(uninitVarArray10);
196+
TEST_CASE(uninitVarArray11);
196197
TEST_CASE(uninitVarArray2D);
197198
TEST_CASE(uninitVarArray3D);
198199
TEST_CASE(uninitVarCpp11Init1);
@@ -3180,6 +3181,20 @@ class TestConstructors : public TestFixture {
31803181
errout.str());
31813182
}
31823183

3184+
void uninitVarArray11() {
3185+
check("class C {\n" // #12150
3186+
"private:\n"
3187+
" int buf[10];\n"
3188+
"public:\n"
3189+
" C() {\n"
3190+
" for (int& i : buf)\n"
3191+
" i = 0;\n"
3192+
" }\n"
3193+
"};\n");
3194+
3195+
ASSERT_EQUALS("", errout.str());
3196+
}
3197+
31833198
void uninitVarArray2D() {
31843199
check("class John\n"
31853200
"{\n"

0 commit comments

Comments
 (0)