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.
@danielmarjamaki should a ticket be opend for this issue too?
Thanks! I have created https://trac.cppcheck.net/ticket/10333
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.
Given this code:
cppcheck reports null pointer dereferences:
Nothing is reported when the second assert is removed.
@danielmarjamaki should a ticket be opend for this issue too?
Thanks! I have created https://trac.cppcheck.net/ticket/10333
The following obvious nonsense does not trigger any warning:
It should warn that the assert will always fail and that the following dereference is wrong.