Latest cppcheck (2.7) complains about a redundant null pointer check. And it's actually correct. However, the redundancy is hidden inside a macro call. It is not unusual to have a general purpose macro (with additional safety checks) and there is little that can be done to avoid this redundancy when such macros are involved. Please consider the following stripped down C code snippet I extracted from a real world program: $ cat redundant.c #define SAFE_NAME(s) ((s) && (s)->name) ? (s)->name : "" struct...
Not sure if this has been reported by before, but there's another false positive in cppcheck 2.5 when run with --enable=portability. The test case is: #include <stdio.h> int main(int argc, const char* argv[]) { argv++; if (argc>0) printf("%s\n", (argv-1)[0]); return 0; } Code compiles fine and is perfectly valid C: # gcc foo3.c -o foo3 && ./foo3 ./foo3 ...but cppcheck complains: # cppcheck --enable=portability foo3.c Checking foo3.c ... foo3.c:4:37: portability: Undefined behaviour, pointer arithmetic...
Here's another variant of possibly the same (?) issue: int main(int ac, char** av) { int strict = ac; if (strict && av) { if (strict == 1) { return 1; } } return 0; } again: code compiles fine and proves that cppcheck is reporting a false positive here: # cppcheck --enable=all foo2.c Checking foo2.c ... foo2.c:4:13: style: Condition 'strict==1' is always true [knownConditionTrueFalse] if (strict == 1) { ^ foo2.c:3:9: note: Assuming condition 'strict' is true if (strict && av) { ^ foo2.c:4:13: note:...
Here's another variant of possibly the same (?) issue: int main(int ac, char** av) { int strict = ac; if (strict && av) { if (strict == 1) { return 1; } } return 0; } again: code compiles fine and proves that cppcheck is reporting a false positive here: # cppcheck --enable=all foo2.c Checking foo2.c ... foo2.c:4:13: style: Condition 'strict==1' is always true [knownConditionTrueFalse] if (strict == 1) { ^ foo2.c:3:9: note: Assuming condition 'strict' is true if (strict && av) { ^ foo2.c:4:13: note:...
Here's another variant of possibly the same (?) issue: int main(int ac, char** av) { int strict = ac; if (strict && av) { if (strict == 1) { return 1; } } return 0; } again: code compiles fine and proves that cppcheck is reporting a false positive here: # cppcheck --enable=all foo.c Checking foo.c ... foo.c:4:13: style: Condition 'strict==1' is always true [knownConditionTrueFalse] if (strict == 1) { ^ foo.c:3:9: note: Assuming condition 'strict' is true if (strict && av) { ^ foo.c:4:13: note: Condition...
Here's another variant of possibly the same (?) issue: int main(int ac, char** av) { int strict = ac; if (strict && av) { if (strict == 1) { return 1; } } return 0; } again: code compiles fine and proves that cppcheck is reporting a false positive here: # cppcheck --enable=all foo.c Checking foo.c ... foo.c:4:13: style: Condition 'strict==1' is always true [knownConditionTrueFalse] if (strict == 1) { ^ foo.c:3:9: note: Assuming condition 'strict' is true if (strict && av) { ^ foo.c:4:13: note: Condition...
Using the latest cppcheck 2.5 I came across a new false positive I would like to share. With option --enable=all it is reporting knownConditionTrueFalse, where obviously incorrect as my simplified test case proves: struct S { void* obj; }; static int func(struct S a, struct S b) { if (a.obj && b.obj) { if (a.obj != b.obj) { /* according to cppcheck, we never get here ... */ return 0; } return 1; } return 1; } int main(int ac, char** av) { struct S a; struct S b; a.obj = (void*)1; b.obj = (void*)2;...
Using the latest cppcheck 2.5 I came across a new false positive I would like to share. With option --enable=all it is reporting knownConditionTrueFalse, where obviously incorrect as my simplified test case proves: struct S { void* obj; }; static int func(struct S a, struct S b) { if (a.obj && b.obj) { if (a.obj != b.obj) { /* according to cppcheck, we never get here ... */ return 0; } return 1; } return 1; } int main(int ac, char** av) { struct S a; struct S b; a.obj = (void*)1; b.obj = (void*)2;...