Menu

False possitive of return of local reference

Tal
2024-12-02
2024-12-03
  • Tal

    Tal - 2024-12-02

    The following code yields false positive in the upstream cppcheck:

    #include <iostream>
    
    struct ostream_holder {
          explicit ostream_holder(std::ostream& os)  : m_os(os) {}
          ostream_holder(const ostream_holder&) = default;
    
          std::ostream& m_os;
    };
    
    template <typename T>
    std::ostream& operator<<(ostream_holder os_holder, const T& value)
    {
        os_holder << value;
        return os_holder.m_os;
    }
    

    The returned error:

    example.cpp:14:21: error: Reference to local variable returned. [returnReference]
        return os_holder.m_os;
    

    Even though this code is valid, and the "m_os" is not a local reference.

     
  • CHR

    CHR - 2024-12-03

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/13373

     
  • Tal

    Tal - 2024-12-03

    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.