Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3228 (false positive: possible null pointer dereference)

  • Loading branch information...
commit 63937f592e11edbd2299525e24633821fb7131f8 1 parent 7035d4c
Daniel Marjamäki authored October 30, 2011
9  lib/checknullpointer.cpp
@@ -436,6 +436,8 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
436 436
             continue;
437 437
         }
438 438
 
  439
+        bool inconclusive = false;
  440
+
439 441
         /**
440 442
          * @todo There are lots of false negatives here. A dereference
441 443
          *  is only investigated if a few specific conditions are met.
@@ -444,6 +446,11 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
444 446
         // dereference in assignment
445 447
         if (Token::Match(tok1, "[;{}] %var% . %var%")) {
446 448
             tok1 = tok1->next();
  449
+            if (tok1->strAt(3) == "(") {
  450
+                if (!_settings->inconclusive)
  451
+                    continue;
  452
+                inconclusive = true;
  453
+            }
447 454
         }
448 455
 
449 456
         // dereference in assignment
@@ -559,7 +566,7 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
559 566
             else if (Token::Match(tok2, "if ( !| %varid% )|&&", varid1)) {
560 567
                 // Is this variable a pointer?
561 568
                 if (isPointer(varid1))
562  
-                    nullPointerError(tok1, varname, tok2->linenr());
  569
+                    nullPointerError(tok1, varname, tok2->linenr(), inconclusive);
563 570
                 break;
564 571
             }
565 572
         }
19  test/testnullpointer.cpp
@@ -257,13 +257,6 @@ class TestNullPointer : public TestFixture {
257 257
         ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 3\n", errout.str());
258 258
 
259 259
         check("void foo(ABC *abc) {\n"
260  
-              "    abc->do_something();\n"
261  
-              "    if (abc)\n"
262  
-              "        ;\n"
263  
-              "}\n");
264  
-        ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 3\n", errout.str());
265  
-
266  
-        check("void foo(ABC *abc) {\n"
267 260
               "    if (abc->a == 3) {\n"
268 261
               "        return;\n"
269 262
               "    }\n"
@@ -432,6 +425,18 @@ class TestNullPointer : public TestFixture {
432 425
               "    if (abc) {}\n"
433 426
               "}");
434 427
         ASSERT_EQUALS("", errout.str());
  428
+
  429
+        // #3228 - calling function with null object
  430
+        {
  431
+            const char code[] = "void f(Fred *fred) {\n"
  432
+                                "    fred->x();\n"
  433
+                                "    if (fred) { }\n"
  434
+                                "}";
  435
+            check(code);
  436
+            ASSERT_EQUALS("", errout.str());
  437
+            check(code, true);
  438
+            ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 3\n", errout.str());
  439
+        }
435 440
     }
436 441
 
437 442
     // Dereferencing a pointer and then checking if it is null

0 notes on commit 63937f5

Please sign in to comment.
Something went wrong with that request. Please try again.