Skip to content

Commit 130d1ab

Browse files
authored
Fix 10210: FN: nullPointerRedundantCheck regression in member function (danmar#3512)
1 parent f1f86db commit 130d1ab

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

lib/astutils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,8 @@ bool isVariablesChanged(const Token* start,
22912291

22922292
bool isThisChanged(const Token* tok, int indirect, const Settings* settings, bool cpp)
22932293
{
2294-
if (Token::Match(tok->previous(), "%name% (")) {
2294+
if ((Token::Match(tok->previous(), "%name% (") && !Token::simpleMatch(tok->astOperand1(), ".")) ||
2295+
Token::Match(tok->tokAt(-3), "this . %name% (")) {
22952296
if (tok->previous()->function()) {
22962297
return (!tok->previous()->function()->isConst());
22972298
} else if (!tok->previous()->isKeyword()) {

lib/valueflow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,8 @@ struct ValueFlowAnalyzer : Analyzer {
23332333
return isThisModified(tok);
23342334

23352335
// bailout: global non-const variables
2336-
if (isGlobal() && Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {")) {
2336+
if (isGlobal() && !dependsOnThis() && Token::Match(tok, "%name% (") &&
2337+
!Token::simpleMatch(tok->linkAt(1), ") {")) {
23372338
if (tok->function()) {
23382339
if (!tok->function()->isConstexpr() && !isConstFunctionCall(tok, getSettings()->library))
23392340
return Action::Invalid;

test/testnullpointer.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class TestNullPointer : public TestFixture {
124124
TEST_CASE(nullpointer82); // #10331
125125
TEST_CASE(nullpointer83); // #9870
126126
TEST_CASE(nullpointer84); // #9873
127+
TEST_CASE(nullpointer85); // #10210
127128
TEST_CASE(nullpointer_addressOf); // address of
128129
TEST_CASE(nullpointerSwitch); // #2626
129130
TEST_CASE(nullpointer_cast); // #4692
@@ -2525,6 +2526,27 @@ class TestNullPointer : public TestFixture {
25252526
errout.str());
25262527
}
25272528

2529+
void nullpointer85() // #10210
2530+
{
2531+
check("struct MyStruct {\n"
2532+
" int GetId() const {\n"
2533+
" int id = 0;\n"
2534+
" int page = m_notebook->GetSelection();\n"
2535+
" if (m_notebook && (m_notebook->GetPageCount() > 0))\n"
2536+
" id = page;\n"
2537+
" return id;\n"
2538+
" }\n"
2539+
" wxNoteBook *m_notebook = nullptr;\n"
2540+
"};\n"
2541+
"int f() {\n"
2542+
" const MyStruct &s = Get();\n"
2543+
" return s.GetId();\n"
2544+
"}\n");
2545+
ASSERT_EQUALS(
2546+
"[test.cpp:5] -> [test.cpp:4]: (warning) Either the condition 'm_notebook' is redundant or there is possible null pointer dereference: m_notebook.\n",
2547+
errout.str());
2548+
}
2549+
25282550
void nullpointer_addressOf() { // address of
25292551
check("void f() {\n"
25302552
" struct X *x = 0;\n"

0 commit comments

Comments
 (0)