[Bug libstdc++/66017] Undefined behaviour in std::set<long long>

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu May 21 10:27:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66017

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I think this would solve it (I'm still trying to build a clang that will allow
me to reproduce the error):


--- a/libstdc++-v3/include/bits/stl_tree.h
+++ b/libstdc++-v3/include/bits/stl_tree.h
@@ -869,25 +869,32 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       iterator
       begin() _GLIBCXX_NOEXCEPT
       { 
+       if (_M_impl._M_header._M_left == &_M_impl._M_header)
+         return end();
        return iterator(static_cast<_Link_type>
                        (this->_M_impl._M_header._M_left));
       }

       const_iterator
       begin() const _GLIBCXX_NOEXCEPT
-      { 
+      {
+       if (_M_impl._M_header._M_left == &_M_impl._M_header)
+         return end();
        return const_iterator(static_cast<_Const_Link_type>
                              (this->_M_impl._M_header._M_left));
       }

       iterator
       end() _GLIBCXX_NOEXCEPT
-      { return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
+      {
+       return iterator(reinterpret_cast<_Link_type>
+                       (&this->_M_impl._M_header));
+      }

       const_iterator
       end() const _GLIBCXX_NOEXCEPT
       { 
-       return const_iterator(static_cast<_Const_Link_type>
+       return const_iterator(reinterpret_cast<_Const_Link_type>
                              (&this->_M_impl._M_header));
       }


When the tree is empty begin() performs an invalid cast too, but I don't like
the branch this introduces.



More information about the Gcc-bugs mailing list