When analysing some code with cppcheck 2.2 I seem to have found a false positive. It looks like cppcheck does not recognise that the pointer is incremented and so a subsequent check is not the same as a previous one.
The following code shows that the increment actually occurs, the second conditional check of if (*ptr >= max_value) is not the same as the first one.
Any advice would be appreciated.
#include<stdio.h>#include<stdbool.h>boolinc_ptr_test(intmax_value,int*ptr){if(*ptr>=max_value){printf("Returning after initial *ptr >= max_value check\n");returnfalse;}*ptr=*ptr+1;if(*ptr>=max_value){printf("Taken 'if' branch, *ptr==%d, max_value==%d\n",*ptr,max_value);returnfalse;}else{printf("Taken 'else' branch, *ptr==%d, max_value==%d\n",*ptr,max_value);returntrue;}}intcnt=0;int*foo=&cnt;intmain(void){while(inc_ptr_test(10,&cnt));printf("main() : pre increment cnt==%d\n",cnt);*foo=*foo+1;printf("main() : post increment cnt==%d\n",cnt);return0;}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
When analysing some code with cppcheck 2.2 I seem to have found a false positive. It looks like cppcheck does not recognise that the pointer is incremented and so a subsequent check is not the same as a previous one.
The following code shows that the increment actually occurs, the second conditional check of
if (*ptr >= max_value)is not the same as the first one.Any advice would be appreciated.
I should have added the output of the test, this is shown below...
Thanks! I created ticket https://trac.cppcheck.net/ticket/10004