Menu

Leaks in loops

2019-03-06
2021-02-03
  • Neil Matthews

    Neil Matthews - 2019-03-06

    cppcheck --force --enable=all --inconclusive -DLOOP=2 for_loop_hides_leak.c

    doesn't detect the memory leak (assignment to the same pointer):

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        char *ptr;
    
        for(i = 0;i < LOOP;i++)
        {
            ptr = malloc(16);
            printf("ptr = %p\n", (void *)ptr);
        }
    
        free(ptr);
    
        return 0;
    }
    

    I was wondering in general whether checking previously "loop unrolled" loops for (0, 1, and 2 times round - or user configurable) might be one way to proceed?

    Neil

     
  • CHR

    CHR - 2021-01-26

    This FN is still not detected in v2.3, unless the loop is unrolled.

     
  • CHR

    CHR - 2021-02-03

    There already is a related ticket: https://trac.cppcheck.net/ticket/10050

     

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.