Idea Detail view

 
 
 

Value tracking arrives at incorrect conclusion when incrementing variables in loop conditions

 

Consider the following program:

 

   #include <stdlib.h>
   #include <stdint.h>
    
   #define MAX_VAL (6)
   
   int main()
   {
       uint32_t val = 0;
    
       do
       {
       }
       while(++val != MAX_VAL);
    
       if(MAX_VAL == val)
       {
         exit(0);   
       }
    
       return 0;
   }

 

This program is guaranteed to exit before reaching the ‘return’, however lint claims it is guaranteed to never execute the body of the the `if` statement:

 

   info 774: boolean condition for 'if' always evaluates to 'false' (involving variable 'val')
   supplemental 831: binary operator yields 0 (line 15)
   supplemental 831: increment yields 1 (line 13)
   supplemental 831: initialization yields 0 (line 8)
   supplemental 831: integral conversion yields 0 (line 8)
   supplemental 831: integral conversion yields 6 (line 15)
   supplemental 893: expanded from macro 'MAX_VAL' (line 4)

 

I suspect Lint is confusing `val++` for `++val` which would make the warning correct.

 

Pending

1 Votes