Menu

Resource Leak

Anjali
2020-11-30
2020-12-02
  • Anjali

    Anjali - 2020-11-30

    Hi,

    I have 2 queries in resource leak detection by Cpp check.

    1 Query:

    I have downloaded cpp check source code from this link https://launchpad.net/ubuntu/+source/cppcheck/2.2-1 and kept for build in 18.04 and 16.04 ubuntu Host PCs. I have a c file in which a function check_conf() is smthng like below

         int check_conf()
    {
    
            fp = fopen("file.txt",r+);
            if(fp==NULLL)
            {
                    ret = FAILURE;
            }
            else
            {
                    //SUCCESS case
            }
    
            fclose();
    
            fp = fopen("file.txt","w");
    
            if(fp == NULL)
            {
                    ret = FAILURE;
            }
            else
            {
                    //Success case
            }
    
            --------------------
    }            
    

    I missed the fclose() during implementation time. And during static analysis in ubuntu 18.04 with below command

    ./cppcheck --dump config_api.c --enable=warning,performance,portability --std=c11

    Resource leak is not detected .But in 16.04 it is detected.

    Why it is so when both host PCSs are having cppcheck executable from same base code?

    2 Query:

      void test()
    {
    -------------------------------------
    -------------------------------------
            fp = fopen(conf_file_test,"r");
    
            if(fp == NULL)
            {
    
                    ret = FILE_OPEN_FAILED;
            }
            else
            {
                    while(fgets(conf_buf,sizeof(conf_buf),fp) != NULL)
                    {
                            if(strstr(conf_buf,txt) != NULL)
                            {
                                    (void)memset(conf_buf,0,sizeof(conf_buf));
    
                                    if(fgets(conf_buf,sizeof(conf_buf),fp) != NULL)
                                    {
                                            if(strstr(conf_buf,tx2) != NULL)
                                            {
                                                    entry_exist_flag = ENABLE;
                                            }
                                            else
                                            {
                                                    //Do nothng
                                            }
                                    }
                                    else
                                    {
                                            //Do nothng
                                    }
                            }
                            else
                            {
                                    //Do nothng
                            }
                    }
    
                    fclose(fp); <--------
    
                    if(entry_exist_flag == DISABLE)
                    {
                            /* Entry not found */
    
                            fp = fopen(conf_file_test,"a");
                            ----------------------------------
    
                    }
            }
    }
    

    Here the fclose pointed is causing the confusion. If I comment the while loop and fclose() then resource leak detection happens in ubuntu 18.04. If I dont comment the while loop and comment only the fclose(), no resorce leak detection happens in 18.04. This case I have not checked in 16.04. Since 18.04 is my target host PC.

    Need help to understand this .

     
  • Daniel Marjamäki

    1. That is strange I cannot explain it. Did you forget declaring fp?
    2. you must declare fp otherwise Cppcheck will not detect issues.
     

    Last edit: Daniel Marjamäki 2020-11-30
    • Anjali

      Anjali - 2020-12-02

      Hi Daniel,

      Thank You for the reply.

      I have couple of questions too.

      1. I have a file xyz.c and it has a lot of include files which is given
        as an so file from third party. Whether is it possible to use so
        libraries during the static analysis of xyz.c file?

      2. How can I run cppcheck on multiple files at a time?

      Regards,

      Anjali

      On 30/11/20 11:03 pm, "Daniel Marjamäki" wrote:

      1. That is strange I cannot explain it.
      2. you must declare fp otherwise Cppcheck will not detect issues.

      Resource Leak
      https://sourceforge.net/p/cppcheck/discussion/development/thread/4c9387dd15/?limit=25#bfd9


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/cppcheck/discussion/development/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
      • Daniel Marjamäki

        cppcheck cannot analyze so files. you are supposed to use --library to handle external dependencies.
        use cppcheck folder to check all files in folder (recursively).

         
        • Daniel Marjamäki

          For details how to use cppcheck please see the manual: http://cppcheck.sourceforge.net/manual.pdf

          If you have some feedback on the manual feel free to share it.

           

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.