Menu

False alarm on doublefree

2022-04-19
2022-04-26
  • Dotan Barak

    Dotan Barak - 2022-04-19

    Hi.

    I'm using cppcheck-2.7 and i noticed that there is a false alarm on double free,
    i get the following error message:
    /home/db/example.c:31:2: error: Memory pointed to by 'ptr' is freed twice. [doubleFree]

    On the following piece of code:

    #include <stdlib.h>
    
    struct buf_t {
            void *ptr;
    };
    
    int alloc_buf(struct buf_t *buf)
    {
            buf->ptr = malloc(10);
            if (buf->ptr == NULL)
                    return 1;
    
            return 0;
    }
    
    int main()
    {
            struct buf_t buf;
    
            if (alloc_buf(&buf))
                    exit(1);
    
            free(buf.ptr);
    
            if (alloc_buf(&buf))
                    exit(1);
    
            free(buf.ptr);
    
            return 0;
    }
    

    Maybe it is worth mentioning that for the following piece code there isn't any warning at all (i.e. using the struct causes this false alarm):

    #include <stdlib.h>
    
    int alloc_buf(void **buf)
    {
            *buf = malloc(10);
            if (*buf == NULL)
                    return 1;
    
            return 0;
    }
    
    int main()
    {
            void *buf;
    
            if (alloc_buf(&buf))
                    exit(1);
    
            free(buf);
    
            if (alloc_buf(&buf))
                    exit(1);
    
            free(buf);
    
            return 0;
    }
    
     

    Last edit: Dotan Barak 2022-04-24
  • CHR

    CHR - 2022-04-26

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

     

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.