]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR libstdc++/8230 (Buggy allocator behaviour)
authorBenjamin Kosnik <bkoz@redhat.com>
Mon, 2 Dec 2002 20:01:54 +0000 (20:01 +0000)
committerBenjamin Kosnik <bkoz@gcc.gnu.org>
Mon, 2 Dec 2002 20:01:54 +0000 (20:01 +0000)
2002-12-02  Benjamin Kosnik  <bkoz@redhat.com>

        PR libstdc++/8230
        * include/bits/stl_vector.h (vector::reserve): Throw length_error if
        requested size is bigger than max_size().
        * include/bits/stl_bvector.h (vector<bool>::reserve): Same.
        * testsuite/23_containers/vector_capacity.cc (test02): Add.

From-SVN: r59734

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/stl_bvector.h
libstdc++-v3/include/bits/stl_vector.h
libstdc++-v3/testsuite/23_containers/vector_capacity.cc

index 2ab6cc66a52d823f9bc1458a6c455cd5434344a3..f4793516ba73329602d8a4e53b5357e389dd5b1c 100644 (file)
@@ -1,3 +1,11 @@
+2002-12-02  Benjamin Kosnik  <bkoz@redhat.com>
+
+        PR libstdc++/8230
+        * include/bits/stl_vector.h (vector::reserve): Throw length_error if
+        requested size is bigger than max_size().
+        * include/bits/stl_bvector.h (vector<bool>::reserve): Same.
+        * testsuite/23_containers/vector_capacity.cc (test02): Add.
+
 2002-12-02  Benjamin Kosnik  <bkoz@redhat.com>
 
        * configure.in (libtool_VERSION): Update to 5:2:0.
index e48ad7d63fdafd636ab1ca67916304ee3011ffe7..ce6df71b67f8b3c1885a561d1b17dc91f420b17a 100644 (file)
@@ -611,7 +611,9 @@ template <typename _Alloc>
     }    
   
     void reserve(size_type __n) {
-      if (capacity() < __n) {
+      if (__n > this->max_size())
+       __throw_length_error("vector::reserve");
+      if (this->capacity() < __n) {
         _Bit_type * __q = _M_bit_alloc(__n);
         _M_finish = copy(begin(), end(), iterator(__q, 0));
         _M_deallocate();
index 5e2ea54d9135dbbee79033a61de108f5a42cc93d..ef3b1c26e74cd05be3098ece41eae83e668043d4 100644 (file)
@@ -391,7 +391,9 @@ public:
    *  reallocation of memory and copy of vector data.
   */
   void reserve(size_type __n) {
-    if (capacity() < __n) {
+    if (__n > this->max_size())
+      __throw_length_error("vector::reserve");
+    if (this->capacity() < __n) {
       const size_type __old_size = size();
       pointer __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish);
       _Destroy(_M_start, _M_finish);
index a56f2ef40c4d88b966cd82ed51097cdde83d5af1..e73b15a3246876741053bd57cb616f6f761aff07 100644 (file)
@@ -1,7 +1,7 @@
 // 1999-05-07
 // bkoz 
 
-// Copyright (C) 1999 Free Software Foundation, Inc.
+// Copyright (C) 1999, 2002 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -22,6 +22,7 @@
 // 23.2.4.2 vector capacity
 
 #include <vector>
+#include <stdexcept>
 #include <testsuite_hooks.h>
 
 template<typename T>
@@ -29,9 +30,8 @@ template<typename T>
 
 struct B { };
 
-bool test01()
+void test01()
 {
-
   // non POD types
   bool test = true;
   std::vector< A<B> > vec01;
@@ -51,17 +51,57 @@ bool test01()
   vec01.resize(sz01);
   sz02 = vec01.size();
   VERIFY( sz01 == sz02 );
+}
 
-#ifdef DEBUG_ASSERT
-  assert(test);
-#endif
-  
-  return test;
+// libstdc++/8230
+void test02()
+{
+  bool test = true;
+
+  {
+    std::vector<int>  array;
+    const std::size_t size = array.max_size();
+    try 
+      {
+       array.reserve(size);
+      } 
+    catch (const std::length_error& error) 
+      {
+       test &= false;
+      }
+    catch (const std::bad_alloc& error)
+      {
+       test &= true;
+      }
+    catch (...)
+      {
+       test &= false;
+      }
+    VERIFY( test );
+  }
+
+  {
+    std::vector<int>  array;
+    const std::size_t size = array.max_size() + 1;
+    try 
+      {
+       array.reserve(size);
+      } 
+    catch (const std::length_error& error) 
+      {
+       test &= true;
+      }
+    catch (...)
+      {
+       test &= false;
+      }
+    VERIFY( test );
+  }
 }
 
 int main()
 {
   test01();
-
+  test02();
   return 0;
 }