Uploaded image for project: 'C++ Standard Library'
  1. C++ Standard Library
  2. STDCXX-1037

Bug in vector::swap() with unequal allocators

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 4.1.3, 4.1.4, 4.2.0, 4.2.1
    • 4.2.2
    • 23. Containers
    • None
    • All

    • Patch Available
    • Incorrect Behavior

    Description

      The test below fails with assertion:

      Assertion failed: 1 == v1.size() && 2 == v1.front(), file test.cpp, line 32
      
      This application has requested the Runtime to terminate it in an unusual way.
      Please contact the application's support team for more information.
      
      test.cpp
      #include <cassert>
      #include <memory>
      #include <vector>
      
      template <typename T>
      class Alloc : public std::allocator<T>
      {
      public:
          Alloc(int i) : i_(i) { }
      
          int i_;
      };
      
      template <typename T>
      bool operator==(const Alloc<T>& a1, const Alloc<T>& a2)
      {
          return a1.i_ == a2.i_;
      }
      
      int main ()
      {
          std::vector<int, Alloc<int> > v1(Alloc<int>(1));
          v1.push_back(1);
          assert(1 == v1.size() && 1 == v1.front());
      
          std::vector<int, Alloc<int> > v2(Alloc<int>(2));
          v2.push_back(2);
          assert(1 == v2.size() && 2 == v2.front());
      
          v1.swap(v2);
      
          assert(1 == v1.size() && 2 == v1.front());
          assert(1 == v2.size() && 1 == v2.front());
      
          return 0;
      }
      

      The bug was introduced in r355174

      The proposed patch:

      vector.cc.diff
      Index: vector.cc
      ===================================================================
      --- vector.cc	(revision 800774)
      +++ vector.cc	(working copy)
      @@ -133,6 +133,7 @@
           _RWSTD_ASSERT (__tmp.get_allocator () == __other.get_allocator ());
       
           __tmp.assign (begin (), end ());
      +    assign (__other.begin (), __other.end ());
           __other.swap (__tmp);
       }
      

      Attachments

        Activity

          People

            farid Farid Zaripov
            farid Farid Zaripov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 1h Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1h 1m
                1h 1m