Skip to content

Commit 37fd60e

Browse files
committed
Fixed cppcheck-opensource#7293 (Use of uninitialized pointer not detected (worked in 1.71))
1 parent 6b685f5 commit 37fd60e

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

lib/valueflow.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,16 +2849,18 @@ static void valueFlowUninit(TokenList *tokenlist, SymbolDatabase * /*symbolDatab
28492849
continue;
28502850
const Token *vardecl = tok->next();
28512851
bool stdtype = false;
2852+
bool pointer = false;
28522853
while (Token::Match(vardecl, "%name%|::|*") && vardecl->varId() == 0) {
28532854
stdtype |= vardecl->isStandardType();
2855+
pointer |= vardecl->str() == "*";
28542856
vardecl = vardecl->next();
28552857
}
2858+
if (!tokenlist->isC() && !stdtype && !pointer)
2859+
continue;
28562860
if (!Token::Match(vardecl, "%var% ;"))
28572861
continue;
28582862
if (Token::Match(vardecl, "%varid% ; %varid% =", vardecl->varId()))
28592863
continue;
2860-
if (!tokenlist->isC() && !stdtype)
2861-
continue;
28622864
const Variable *var = vardecl->variable();
28632865
if (!var || var->nameToken() != vardecl)
28642866
continue;

test/testnullpointer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ class TestNullPointer : public TestFixture {
441441

442442
// #2641 - local pointer, function call
443443
check("void f() {\n"
444-
" ABC *abc;\n"
444+
" ABC *abc = abc1;\n"
445445
" abc->a = 0;\n"
446446
" do_stuff();\n"
447447
" if (abc) { }\n"

test/testvalueflow.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,16 +2377,22 @@ class TestValueFlow : public TestFixture {
23772377
}
23782378

23792379
void valueFlowUninit() {
2380+
const char* code;
23802381
std::list<ValueFlow::Value> values;
23812382

2382-
const char* code = "void f() {\n"
2383-
" int x;\n"
2384-
" switch (x) {}\n"
2385-
"}";
2386-
2383+
code = "void f() {\n"
2384+
" int x;\n"
2385+
" switch (x) {}\n"
2386+
"}";
23872387
values = tokenValues(code, "x )");
2388-
ASSERT_EQUALS(1U, values.size());
2389-
ASSERT_EQUALS(ValueFlow::Value::UNINIT, values.empty() ? 0 : values.front().valueType);
2388+
ASSERT_EQUALS(true, values.size()==1U && values.front().isUninitValue());
2389+
2390+
code = "void f() {\n"
2391+
" C *c;\n"
2392+
" if (c->x() == 4) {}\n"
2393+
"}";
2394+
values = tokenValues(code, "c .");
2395+
ASSERT_EQUALS(true, values.size()==1U && values.front().isUninitValue());
23902396
}
23912397
};
23922398

0 commit comments

Comments
 (0)