Menu

Issue Report: False Positive: Using reference to dangling temporary.CppCheck(danglingTempReference)

Lonchik
2022-03-21
2022-03-22
  • Lonchik

    Lonchik - 2022-03-21

    Sample code snippet

    // A type alias definition in some include.
    using UniqueBuffer = std::tuple<std::unique_ptr<uint8_t[]>, size_t>;
    // class member.
    std::queue<UniqueBuffer> _queue;
    
    // somewhere in a class member function...
            auto& headerbuf = _queue.emplace();
            std::get<0>(headerbuf) = std::unique_ptr<uint8_t[]>(new uint8_t[headersize]);
            std::get<1>(headerbuf) = headersize;
    

    cppcheck highlights last two lines with error Using reference to dangling temporary.CppCheck(danglingTempReference).

    Note that if I change the code line from auto& headerbuf = _queue.emplace(); to simply auto headerbuf = _queue.emplace(); (i.e. type instead of type reference), cppcheck stops flagging, but it's incorrect b/c now it is writing to a copy of memory, rather than into queue's entry's memory space.

    Seems that cppcheck fails to recognize that std::tuple<...>& that's returned from std::queue::emplace() is not actually a temporary, but rather a reference to internal member space.

    Env: using VSCode extension jbenden.c-cpp-flylint

    $ brew info cppcheck 
    cppcheck: stable 2.7 (bottled), HEAD
    Static analysis of C and C++ code
    https://sourceforge.net/projects/cppcheck/
    /usr/local/Cellar/cppcheck/2.7 (83 files, 8.5MB) *
      Poured from bottle on 2022-02-14 at 14:54:48
    From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/cppcheck.rb
    License: GPL-3.0-or-later
    ==> Dependencies
    Build: cmake , python@3.10 
    Required: pcre , tinyxml2 
    ==> Options
    --HEAD
            Install HEAD version
    ==> Analytics
    install: 2,553 (30 days), 6,817 (90 days), 25,803 (365 days)
    install-on-request: 2,535 (30 days), 6,755 (90 days), 25,579 (365 days)
    build-error: 2 (30 days)
    
     
  • CHR

    CHR - 2022-03-22
     
    👍
    1
  • Lonchik

    Lonchik - 2022-03-22

    Awesome, thanks!

     

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.