Menu

Make custom memory allocation/deallocation equivalent to malloc/free

Siddharth
2019-01-08
2019-01-18
  • Siddharth

    Siddharth - 2019-01-08

    I have a custom free function "custom_free" that deallocates memory allocated by malloc or by a custom alloc function "custom_alloc".
    When i run cppcheck, it reports "mismatchAllocDealloc" whenever it finds something like this

    str = malloc(100);
    custom_free(str); / Valid - custom_free internally calls free . Still cppcheck generates mismatchAllocDealloc /

    Is there a way to define in the ".cfg" to indicate free and custom_free are equivalent ?

    I tried this
    <memory>
    <alloc>malloc</alloc>
    <dealloc>free</dealloc>
    <dealloc>custom_free</dealloc>
    </memory>

    This works , but then it applies this rule for all paramters to custom_free whereas i want the rule to be applied only for the 3rd parameter. Thats because rest of the parameters are not deallocated by this custom_free function.

    So tried this -

    <memory>
    <alloc>malloc</alloc>
    <dealloc>free</dealloc>
    <dealloc arg="3">custom_free</dealloc>
    </memory>

    But this doesn't work and it generates mismatchAllocDealloc.

    Please note, I do not want to suppress mismatchAllocDealloc considering it may suppress genuine issues.

    Thanks
    Siddharth

     

    Last edit: Siddharth 2019-01-08
  • versat

    versat - 2019-01-15

    Slightly related thread: https://sourceforge.net/p/cppcheck/discussion/general/thread/d9737d5d/
    While it seems possible to specify arg options for alloc and dealloc, Cppcheck does not seem to take these into account.
    @danmar Should we create a special ticket for such issues, requesting arg options to work? I have not found any ticket that already handles it directly.

     
  • Daniel Marjamäki

    Should we create a special ticket for such issues, requesting arg options to work? I have not found any ticket that already handles it directly

    sounds ok to me.

     

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.