Menu

cppcheck 2.11: autovarInvalidDeallocation FP

2023-06-27
2023-06-27
  • Sylvain Joubert

    Sylvain Joubert - 2023-06-27

    With cppcheck 2.11 I now get some autovarInvalidDeallocation false positives that I did not get with previous versions. At least I think it is false positive since I can't reason why it would be a error and because the error message seems wrongly put together making me think this is an edge case badly handled.

    I can't reduce a small test case unfortunately as I don't understand exactly what trips it. But here is the "bad" message hoping it can help (the (() is particularly suspicious):

    Deallocation of an auto-variable (() results in undefined behaviour. [autovarInvalidDeallocation]
    

    In all my FP, the delete pointer is initialized by taking the address of returned reference (not really a good pattern/practice).
    I could not reduce this to a simple example with valid code. However, here is a broken code that produces the same message (again, I hope this can help):

    void autovar_error()
    {
      Data* pointer = &getData();
      delete pointer;
    }
    
    $ cppcheck test.cpp --enable=style
    Checking test.cpp ...
    test.cpp:10:10: error: Deallocation of an auto-variable (() results in undefined behaviour. [autovarInvalidDeallocation]
      delete pointer;
             ^
    test.cpp:9:19: note: Address of variable taken here.
      Data* pointer = &getData();
                      ^
    test.cpp:10:10: note: Deallocating memory that was not dynamically allocated
      delete pointer;
             ^
    
     
  • CHR

    CHR - 2023-06-27

    Thanks for reporting, fixed by https://github.com/danmar/cppcheck/pull/5203

     

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.