Menu

cppcheck does not detect dlopen/dlclose

2022-02-16
2022-02-19
  • Kleber Santos

    Kleber Santos - 2022-02-16

    Hi everyone,

    I'm a P.hD student and I detected that the code below has a weakness, but it was not detected by cppcheck. I tried to fix it by changing the cppcheck but I couldn't until now, could you guys fix this or give me some steps to achieve this?

    int init(void){   
        #ifndef WIN32
            void *abc;
            static const char *possible[] = {
                "test1.so",
                "test2.so"
            };
            unsigned i;
            for (i=0; possible[i]; i++) {
                abc = dlopen(possible[i], RTLD_LAZY);
                if (abc) {
                    LOG(1, "[+] found : %s\n", possible[i]);
                    break;
                } else {
                    LOG(1, "[-] failed : %s\n", possible[i]);
                }
            }
        #endif
        return 0;
    }
    
     
  • Daniel Marjamäki

    Thanks! It seems it's not about dlopen. We don't detect such leak in loops at all. Cppcheck still doesn't detect the leak if you replace "dlopen" with "malloc" in your code.

    I have created https://trac.cppcheck.net/ticket/10815

    I wonder if you would like to look into that. It is old code so I don't remember a lot about it. But hope you can understand how it works.

     

    Last edit: Daniel Marjamäki 2022-02-17
  • Kleber Santos

    Kleber Santos - 2022-02-18

    Thanks for the feedback!

     
  • Kleber Santos

    Kleber Santos - 2022-02-19

    Hi @danielmarjamaki,

    I observed that when we are dealing with member of struct cppcheck detects the memory leak even inside the loop. Maybe this information will help

    struct Vector {
        double *data;
        size_t size;
    };
    
    struct Vector *newVector (size_t sz) {
        struct Vector *vec = malloc (sizeof (struct Vector));
        while(condition){
            vec->data = malloc (sz * sizeof (double));
        }
    }
    

    output: test.c:11:1: error: Memory leak: vec.data [memleak]

    Regards

     

    Last edit: Kleber Santos 2022-02-19

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.