jimmiebergmann / mini-yaml

Single header YAML 1.0 C++11 serializer/deserializer.
MIT License
218 stars 41 forks source link

reference member is initialized to a temporary that doesn't persist after the constructor exits #2

Closed paleozogt closed 5 years ago

paleozogt commented 5 years ago

Older compilers seem to let this slide, but newer compilers (for example clang v8) rightly complain about this sort of thing:

std::pair<const std::string &, Node &> Iterator::operator *()
{
    switch(m_Type)
    {
    case SequenceType:
        return {"", *(static_cast<SequenceIteratorImp*>(m_pImp)->m_Iterator->second)};
        //      ^---- reference member 'first' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object
        break;
    ...  

That string literal is going to make a temporary std::string, which will destruct when the function returns. The problem occurs at Yaml.cpp:720 and Yaml.cpp:871.

jimmiebergmann commented 5 years ago

Fixed in https://github.com/jimmiebergmann/mini-yaml/commit/22d3dcf5684a11f9c0508c1ad8b3282a1d888319