Menu

how improve memleak of suppress

James Wang
2016-10-20
2023-02-07
  • James Wang

    James Wang - 2016-10-20
            new_context = (struct teei_context *)tz_malloc(sizeof(struct teei_context), GFP_KERNEL);
           ......
            // cppcheck-suppress memleak
            return 0;
    

    cppcheck report below error

    ××.c:1562]: (error) Memory leak: new_context at "return 0" code line which is the end of the function.
    So I have to add "// cppcheck-suppress memleak" before "return 0;" to spress the error.

    However, I notice if developer write another memleak, cppcheck will ignore it.

    Is there another method to imrove this?

     
  • Daniel Marjamäki

    why do you suppress it? is the warning wrong? if the warning is wrong please report a ticket so we can fix it.

     
  • James Wang

    James Wang - 2016-10-24

    We malloc a memory block and add it to a list in one function and release it in another function. So we do not think this need be fixed. However, we just want to suppress it. Any idea?

     
  • Daniel Marjamäki

    We malloc a memory block and add it to a list in one function and release it in another function.

    If you don't have a real leak then Cppcheck should not warn. So, I would like to fix Cppcheck so it does not warn.

    There is probably some detail in your code that makes Cppcheck confused.

    Can you please show a minimal code example that reproduce such wrong warning. I recommend that you take your real code then remove #includes and functions that are not directly related to this leak.. and then recheck the file to see that Cppcheck still shows the warning. If warning is not shown then some #include or function you removed IS somehow related. Then maybe you can try to remove unrelated statements etc.

    Then a ticket should be reported in our issue tracker (http://trac.cppcheck.net) so we can fix the problem.

     
  • Reini Urban

    Reini Urban - 2023-02-07

    E.g. this produces a wrong memleak warning on str.

        {
          size_t len = strlen (*(char**)value);
          char *str = (char *)malloc (len+1);
          memcpy (str, *(char**)value, len+1);
          // we copy just the pointer, not the string
          memcpy (old, &str, sizeof (char*)); // size of ptr
          // cppcheck-suppress memleak
        }
    
     

    Last edit: Reini Urban 2023-02-07

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.