[libcxx] r194151 - Fix an off-by-one error in basic_string::__grow_by, where it would incorrectly throw length_error (instead of bad_alloc) when attempting to resize the string to 'max_size()'. Add tests for resizing to max_size +/-1

Marshall Clow mclow.lists at gmail.com
Wed Nov 6 06:24:38 PST 2013


Author: marshall
Date: Wed Nov  6 08:24:38 2013
New Revision: 194151

URL: http://llvm.org/viewvc/llvm-project?rev=194151&view=rev
Log:
Fix an off-by-one error in basic_string::__grow_by, where it would incorrectly throw length_error (instead of bad_alloc) when attempting to resize the string to 'max_size()'. Add tests for resizing to max_size +/-1

Modified:
    libcxx/trunk/include/string
    libcxx/trunk/test/strings/basic.string/string.capacity/max_size.pass.cpp

Modified: libcxx/trunk/include/string
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/string?rev=194151&r1=194150&r2=194151&view=diff
==============================================================================
--- libcxx/trunk/include/string (original)
+++ libcxx/trunk/include/string Wed Nov  6 08:24:38 2013
@@ -2181,7 +2181,7 @@ basic_string<_CharT, _Traits, _Allocator
                                                      size_type __n_copy,  size_type __n_del,     size_type __n_add)
 {
     size_type __ms = max_size();
-    if (__delta_cap > __ms - __old_cap - 1)
+    if (__delta_cap > __ms - __old_cap)
         this->__throw_length_error();
     pointer __old_p = __get_pointer();
     size_type __cap = __old_cap < __ms / 2 - __alignment ?

Modified: libcxx/trunk/test/strings/basic.string/string.capacity/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/strings/basic.string/string.capacity/max_size.pass.cpp?rev=194151&r1=194150&r2=194151&view=diff
==============================================================================
--- libcxx/trunk/test/strings/basic.string/string.capacity/max_size.pass.cpp (original)
+++ libcxx/trunk/test/strings/basic.string/string.capacity/max_size.pass.cpp Wed Nov  6 08:24:38 2013
@@ -18,15 +18,45 @@
 
 template <class S>
 void
-test(const S& s)
+test1(const S& s)
 {
-    assert(s.max_size() >= s.size());
-    {
-    S s2;
-    try { s2.resize(s2.max_size() - 1, 'x'); }
+    S s2(s);
+    const size_t sz = s2.max_size() - 1;
+    try { s2.resize(sz, 'x'); }
+    catch ( const std::bad_alloc & ) { return ; }
+    assert ( s2.size() ==  sz );
+}
+
+template <class S>
+void
+test2(const S& s)
+{
+    S s2(s);
+    const size_t sz = s2.max_size();
+    try { s2.resize(sz, 'x'); }
     catch ( const std::bad_alloc & ) { return ; }
+    assert ( s.size() ==  sz );
+}
+
+template <class S>
+void
+test3(const S& s)
+{
+    S s2(s);
+    const size_t sz = s2.max_size() + 1;
+    try { s2.resize(sz, 'x'); }
+    catch ( const std::length_error & ) { return ; }
     assert ( false );
-    }
+}
+
+template <class S>
+void
+test(const S& s)
+{
+    assert(s.max_size() >= s.size());
+    test1(s);
+    test2(s);
+    test3(s);
 }
 
 int main()





More information about the cfe-commits mailing list