Menu

False Positive because if() is not processed correctly?

2018-12-13
2021-01-30
  • Absinthe Sautereau

    I have come across this problem that seems like cppcheck just isn't understanding the flow of the if statement. It is showing that memset is getting a -1 value but that else statement should never be accessed for the case when src is > tgt. Obviously this code is a reduced variation of what is actually going on. But it seems to be the smallest piece of code that demonstrates the behavior.

    #include <stdio.h>
    #include <string.h>
    
    int main (void) {
    
        char tgt[7];
        char src[7+1] = "7777777";
    
        if (sizeof tgt <= sizeof src) { 
            memmove(&tgt, &src, sizeof tgt); 
        } else { 
            memmove(&tgt, &src, sizeof src); 
            memset(&tgt + sizeof src, ' ', sizeof tgt - sizeof src); 
        }
    
        return 0;
    }
    
    main.c;13;error;invalidFunctionArg;Invalid memset() argument nr 3. The value is -1 but the valid values are '0:'. 
    
     
  • CHR

    CHR - 2021-01-27

    I can reproduce this with v2.3.

     
  • CHR

    CHR - 2021-01-30
     

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.