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

std::list::insert() violates strong exception safety requirements

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.1.3
    • 4.2.0
    • 23. Containers
    • None
    • All

    Description

      The list::insert() method do not satisfy strong exception safety requirements (23.2.3.3 Remark 1).

      Details are here:
      http://mail-archives.apache.org/mod_mbox/incubator-stdcxx-dev/200701.mbox/%3c45C0C705.8040606@kyiv.vdiweb.com%3e

      test.cpp:
      ------------------------------
      #include <list>
      #include <cassert>

      static int throw_inx = -1;

      class ListItem
      {
      public:
      static int count_;

      void test ()

      { if (throw_inx == count_) throw count_; ++count_; }

      ListItem ()

      { test (); }

      ListItem (const ListItem&) { test (); }

      ~ListItem ()

      { --count_; }

      };

      int ListItem::count_ = 0;

      int main (int argc, char* argv[])
      {
      typedef std::list<ListItem> List;
      ListItem items [20];
      List lst (1);

      bool thrown = false;
      throw_inx = 10;

      List::iterator it = lst.begin ();
      ListItem & ref = *it;

      try

      { ListItem::count_ = 0; lst.insert (it, 20, items [0]); }

      catch (...)

      { thrown = true; }

      assert (thrown);
      assert (it == lst.begin ());
      assert (&ref == &*lst.begin ());
      assert (1 == lst.size ());
      assert (0 == ListItem::count_);

      try { ListItem::count_ = 0; lst.insert (it, items, items + 20); } catch (...) { thrown = true; }

      assert (thrown);
      assert (it == lst.begin ());
      assert (&ref == &*lst.begin ());
      assert (1 == lst.size ());
      assert (0 == ListItem::count_);

      return 0;
      }

      The test output:
      ------------------------------
      test: /usr/src/tests/test.cpp:48: int main(int, char**): Assertion `it == lst.begin ()' failed.
      Aborted.

      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: