Skip to content

Commit 661784a

Browse files
committed
Fixed cppcheck-opensource#5255 (False positive (error) Uninitialized variable: ptr2 - calling a static member function)
1 parent e2fa6a2 commit 661784a

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/checkuninitvar.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@ class UninitVar : public ExecutionPath {
514514

515515
if (Token::Match(tok.previous(), "[;{}] %var% [=[.]")) {
516516
if (tok.next()->str() == ".") {
517+
if (Token::Match(&tok, "%var% . %var% (")) {
518+
const Function *function = tok.tokAt(2)->function();
519+
if (function && function->isStatic)
520+
return &tok;
521+
}
517522
if (use_dead_pointer(checks, &tok)) {
518523
return &tok;
519524
}
@@ -1770,7 +1775,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
17701775
}
17711776

17721777
bool unknown = false;
1773-
if (pointer && (CheckNullPointer::isPointerDeRef(vartok, unknown) || Token::Match(vartok, "%var% ."))) {
1778+
if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
17741779
// pointer is allocated - dereferencing it is ok.
17751780
if (alloc)
17761781
return false;
@@ -1785,6 +1790,11 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
17851790
return true;
17861791
}
17871792

1793+
if (pointer && Token::Match(vartok, "%var% . %var% (")) {
1794+
const Function *function = vartok->tokAt(2)->function();
1795+
return (!function || !function->isStatic);
1796+
}
1797+
17881798
if (cpp && Token::Match(vartok->next(), "<<|>>")) {
17891799
// Is this calculation done in rhs?
17901800
const Token *tok = vartok;

test/testuninitvar.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ class TestUninitVar : public TestFixture {
356356
"}");
357357
ASSERT_EQUALS("", errout.str());
358358

359+
// #5255
360+
checkUninitVar("struct Element {\n"
361+
" static void abcd() {}\n"
362+
"};\n"
363+
"void a() {\n"
364+
" Element *e;\n"
365+
" e->abcd();\n"
366+
"}");
367+
ASSERT_EQUALS("", errout.str());
368+
359369
// Handling >> and <<
360370
{
361371
checkUninitVar("int a() {\n"
@@ -3075,6 +3085,14 @@ class TestUninitVar : public TestFixture {
30753085
"}");
30763086
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: ptr3\n", errout.str());
30773087

3088+
checkUninitVar2("class Element {\n"
3089+
" static void f() { }\n"
3090+
"};\n"
3091+
"void test() {\n"
3092+
" Element *element; element->f();\n"
3093+
"}");
3094+
ASSERT_EQUALS("", errout.str());
3095+
30783096
checkUninitVar2("void f() {\n" // #4911 - bad simplification => don't crash
30793097
" int a;\n"
30803098
" do { } a=do_something(); while (a);\n"

0 commit comments

Comments
 (0)