Menu

false positive nullPointerRedundantCheck

tsteven
2023-04-14
2023-04-15
  • tsteven

    tsteven - 2023-04-14

    cppcheck 2.10.3 seems to have a problem when combining gcc style
    function attributes and the c++ attribute noreturn.  It seems like the
    existence of the gcc style attribute prevents the recognition of c++
    attributes like [[noreturn]].

    $ ~/local/bin/cppcheck --enable=all test2.cc
    Checking test2.cc ...
    test2.cc:21:18: warning: Either the condition 'ptr==nullptr' is
    redundant or there is possible null pointer dereference: ptr.
    [nullPointerRedundantCheck]
      printf("%d\n", ptr->a);
                     ^
    test2.cc:18:11: note: Assuming that condition 'ptr==nullptr' is not
    redundant
      if (ptr == nullptr) {
              ^
    test2.cc:21:18: note: Null pointer dereference
      printf("%d\n", ptr->a);
                     ^
    nofile:0:0: information: Cppcheck cannot find all the include files (use
    --check-config for details) [missingIncludeSystem]

    Here is test2.cc:

    1 #include <stdio.h>
      2 // When attribute exists but doesn't include noreturn we
    get a false positive nullPointerRedundantCheck
      3 [[noreturn]] void fatal(const char, ...) attribute
    ((format (printf, 1, 2)));
      4 // When attribute exists and includes noreturn it works.
      5 //[[noreturn]] void fatal(const char
    , ...) attribute
    ((format (printf, 1, 2), noreturn));
      6 // Without attribute it works.
      7 //[[noreturn]] void fatal(const char, ...);
      8
      9 struct meohmy_t {
     10   int a{9};
     11 };
     12
     13 meohmy_t
    ffind() {return new meohmy_t;};
     14
     15 int main()
     16 {
     17   auto* ptr = ffind();
     18   if (ptr == nullptr) {
     19     fatal("%s", "bye\n");
     20   }
     21   printf("%d\n", ptr->a);
     22 }
     23</stdio.h>

    Here is test2a.cc:

    1 #include <stdlib.h>
      2 [[noreturn]] void fatal(const char*, ...)
      3 {
      4   exit(1);
      5 };</stdlib.h>

     
  • CHR

    CHR - 2023-04-15

    This seems to be fixed in head. I get a warning for the code below, but not for the other prototypes:

    //[[noreturn]] void fatal(const char*, ...) attribute ((format (printf, 1, 2)));
    //[[noreturn]] void fatal(const char*, ...) attribute ((format (printf, 1, 2), noreturn));
    //[[noreturn]] void fatal(const char*, ...);
    void fatal(const char*, ...);
    struct S { int a; };
    void f(S* p) {
       if (p == nullptr)
         fatal("%s", "bye\n");
       printf("%d\n", p->a);
    }
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.