Skip to content

Commit d93cf96

Browse files
committed
Fixed cppcheck-opensource#6769 (false positive: Uninitialized struct member: epoch.integer)
1 parent 63b7700 commit d93cf96

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,12 @@ bool CheckUninitVar::isMemberVariableAssignment(const Token *tok, const std::str
10501050
return true;
10511051
} else if (tok->strAt(1) == "=")
10521052
return true;
1053-
else if (tok->strAt(-1) == "&") {
1053+
else if (Token::Match(tok, "%var% . %name% (")) {
1054+
const Token *ftok = tok->tokAt(2);
1055+
if (!ftok->function() || !ftok->function()->isConst())
1056+
// TODO: Try to determine if membervar is assigned in method
1057+
return true;
1058+
} else if (tok->strAt(-1) == "&") {
10541059
if (Token::Match(tok->tokAt(-2), "[(,] & %name%")) {
10551060
// locate start parentheses in function call..
10561061
unsigned int argumentNumber = 0;

test/testuninitvar.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,13 +3101,15 @@ class TestUninitVar : public TestFixture {
31013101
ASSERT_EQUALS("", errout.str());
31023102

31033103
{
3104-
checkUninitVar("void f(void) {\n"
3104+
checkUninitVar("struct AB { char a[10]; };\n"
3105+
"void f(void) {\n"
31053106
" struct AB ab;\n"
31063107
" strcpy(ab.a, STR);\n"
31073108
"}\n", "test.c");
31083109
ASSERT_EQUALS("", errout.str());
31093110

3110-
checkUninitVar("void f(void) {\n"
3111+
checkUninitVar("struct AB { char a[10]; };\n"
3112+
"void f(void) {\n"
31113113
" struct AB ab;\n"
31123114
" strcpy(x, ab.a);\n"
31133115
"}\n", "test.c");
@@ -3130,6 +3132,33 @@ class TestUninitVar : public TestFixture {
31303132
"}\n", "test.c");
31313133
ASSERT_EQUALS("", errout.str());
31323134

3135+
{
3136+
// #6769 - calling method that might assign struct members
3137+
checkUninitVar("struct AB { int a; int b; void set(); };\n"
3138+
"void f(void) {\n"
3139+
" struct AB ab;\n"
3140+
" ab.set();\n"
3141+
" x = ab;\n"
3142+
"}\n");
3143+
ASSERT_EQUALS("", errout.str());
3144+
3145+
checkUninitVar("struct AB { int a; int get() const; };\n"
3146+
"void f(void) {\n"
3147+
" struct AB ab;\n"
3148+
" ab.get();\n"
3149+
" x = ab;\n"
3150+
"}\n");
3151+
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
3152+
3153+
checkUninitVar("struct AB { int a; void dostuff() {} };\n"
3154+
"void f(void) {\n"
3155+
" struct AB ab;\n"
3156+
" ab.dostuff();\n"
3157+
" x = ab;\n"
3158+
"}\n");
3159+
TODO_ASSERT_EQUALS("error", "", errout.str());
3160+
}
3161+
31333162
checkUninitVar("struct AB { int a; struct { int b; int c; } s; };\n"
31343163
"void do_something(const struct AB ab);\n"
31353164
"void f(void) {\n"

0 commit comments

Comments
 (0)