Ticket #6680: Members can be initialized by operator>>.

This commit is contained in:
Simon Martin 2015-05-10 14:27:15 +02:00
parent 77c5381612
commit da302b3912
2 changed files with 22 additions and 0 deletions

View File

@ -1886,6 +1886,8 @@ bool CheckUninitVar::isMemberVariableAssignment(const Token *tok, const std::str
return true; return true;
else if (Token::Match(tok->tokAt(-2), "[(,=] &")) else if (Token::Match(tok->tokAt(-2), "[(,=] &"))
return true; return true;
else if (Token::Match(tok->tokAt(-2), "%name% >>") && Token::Match(tok->tokAt(3), ";|>>")) // #6680
return true;
else if ((tok->previous() && tok->previous()->isConstOp()) || Token::Match(tok->previous(), "[|=")) else if ((tok->previous() && tok->previous()->isConstOp()) || Token::Match(tok->previous(), "[|="))
; // member variable usage ; // member variable usage
else if (tok->tokAt(3)->isConstOp()) else if (tok->tokAt(3)->isConstOp())

View File

@ -70,6 +70,7 @@ private:
TEST_CASE(uninitvar9); // ticket #6424 TEST_CASE(uninitvar9); // ticket #6424
TEST_CASE(uninitvar_unconditionalTry); TEST_CASE(uninitvar_unconditionalTry);
TEST_CASE(uninitvar_funcptr); // #6404 TEST_CASE(uninitvar_funcptr); // #6404
TEST_CASE(uninitvar_operator); // #6680
TEST_CASE(syntax_error); // Ticket #5073 TEST_CASE(syntax_error); // Ticket #5073
@ -2869,6 +2870,25 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: Factory\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: Factory\n", errout.str());
} }
void uninitvar_operator() { // Ticket #6680
checkUninitVar2("struct Source { Source& operator>>(int& i) { i = 0; return *this; } };\n"
"struct Sink { int v; };\n"
"Source foo;\n"
"void uninit() {\n"
" Sink s;\n"
" int n = 1 >> s.v;\n" // Not initialized
"};\n"
"void notUninit() {\n"
" Sink s1;\n"
" foo >> s1.v;\n" // Initialized by operator>>
" Sink s2;\n"
" int n;\n"
" foo >> s2.v >> n;\n" // Initialized by operator>>
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized struct member: s.v\n", errout.str());
}
// Handling of function calls // Handling of function calls
void uninitvar2_func() { void uninitvar2_func() {
// non-pointer variable // non-pointer variable