Menu

False Positive uninitdata with compound assignment operators

4 days ago
3 days ago
  • Aaron Danen

    Aaron Danen - 4 days ago

    Hello

    malloc() initializes buf, but not buf[0]. The following is valid because buf is never dereferenced

    void f() {
        char *buf = (char *)malloc(1);
        if (!buf)
            return NULL;
        buf += 1;
        free(buf - 1);
    }
    

    however, cppcheck throws

    examples/buf.c:5:5: error: Memory is allocated but not initialized: buf [uninitdata]
        buf += 1;
        ^
    

    For this similar code, cppcheck correctly throws no errors

    void f() {
        char *buf = (char *)malloc(1);
        if (!buf)
            return NULL;
        buf = buf + 1;
        free(buf - 1);
    }
    

    I think this is because checkuninitvar is has explicit handling for "=". I have a patch to modify checkuninitvar.cpp to handle the other assignment operators like "+=" and "-=". If it is useful I can submit at PR. Thanks!

    -- Aaron

     
  • CHR

    CHR - 3 days ago

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15005

     

Log in to post a comment.