Menu

"Null pointer dereference" reported after assert

2021-06-28
2023-05-31
  • Paul Aitken

    Paul Aitken - 2021-06-28

    Given this code:

    void ptr_put(struct whatever *ptr)
    {
        assert(ptr);
        assert(ptr->refcnt > 0);
    
        if (ptr && --ptr->refcnt == 0)
            ptr_destroy(ptr);
    }
    

    cppcheck reports null pointer dereferences:

    $ cppcheck -q --library=std,gnu,dpdk --enable=unusedFunction,warning /tmp/5a.c
    /tmp/5a.c:4:9: warning: Either the condition 'ptr' is redundant or there is possible null pointer dereference: ptr. [nullPointerRedundantCheck]
     assert(ptr->refcnt > 0);
            ^
    /tmp/5a.c:6:6: note: Assuming that condition 'ptr' is not redundant
     if (ptr && --ptr->refcnt == 0)
         ^
    /tmp/5a.c:4:9: note: Null pointer dereference
     assert(ptr->zif_refcnt > 0);
    

    Nothing is reported when the second assert is removed.

     
  • Paul Aitken

    Paul Aitken - 2021-06-30

    @danielmarjamaki should a ticket be opend for this issue too?

     
  • Daniel Marjamäki

     
  • Christopher Yeleighton

    The following obvious nonsense does not trigger any warning:

    void foo ()  { int *r = 0; assert (r); *r = 0; return r; }
    

    It should warn that the assert will always fail and that the following dereference is wrong.

     

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.