[libcxx] r221717 - Replaced checking in string_view::remove_suffix/remove_prefix by _LIBCPP_ASSERT, since this is technically undefined behavior. Fixes PR#21496

Marshall Clow mclow.lists at gmail.com
Tue Nov 11 14:07:10 PST 2014


Author: marshall
Date: Tue Nov 11 16:07:10 2014
New Revision: 221717

URL: http://llvm.org/viewvc/llvm-project?rev=221717&view=rev
Log:
Replaced checking in string_view::remove_suffix/remove_prefix by _LIBCPP_ASSERT, since this is technically undefined behavior. Fixes PR#21496

Modified:
    libcxx/trunk/include/experimental/string_view
    libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_prefix.pass.cpp
    libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_suffix.pass.cpp

Modified: libcxx/trunk/include/experimental/string_view
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/experimental/string_view?rev=221717&r1=221716&r2=221717&view=diff
==============================================================================
--- libcxx/trunk/include/experimental/string_view (original)
+++ libcxx/trunk/include/experimental/string_view Tue Nov 11 16:07:10 2014
@@ -313,8 +313,7 @@ _LIBCPP_BEGIN_NAMESPACE_LFTS
         _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
         void remove_prefix(size_type __n) _NOEXCEPT
         {
-            if (__n > __size)
-                __n = __size;
+            _LIBCPP_ASSERT(n <= size(), "remove_prefix() can't remove more than size()");
             __data += __n;
             __size -= __n;
         }
@@ -322,8 +321,7 @@ _LIBCPP_BEGIN_NAMESPACE_LFTS
         _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
         void remove_suffix(size_type __n) _NOEXCEPT
         {
-            if (__n > __size)
-                __n = __size;
+            _LIBCPP_ASSERT(n <= size(), "remove_suffix() can't remove more than size()");
             __size -= __n;
         }
 

Modified: libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_prefix.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_prefix.pass.cpp?rev=221717&r1=221716&r2=221717&view=diff
==============================================================================
--- libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_prefix.pass.cpp (original)
+++ libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_prefix.pass.cpp Tue Nov 11 16:07:10 2014
@@ -28,13 +28,11 @@ void test ( const CharT *s, size_t len )
         sv1.remove_prefix ( 1 );
         assert ( sv1.size() == (len - 1));
         assert ( sv1.data() == (s + 1));
+        sv1.remove_prefix ( len - 1 );
     }
     
-    sv1.remove_prefix ( len - 1 );
     assert ( sv1.size() == 0 );
-
-    SV sv2 ( s );
-    sv2.remove_prefix ( len << 1 );
+    sv1.remove_prefix ( 0 );
     assert ( sv1.size() == 0 ); 
     }
 }
@@ -72,7 +70,6 @@ int main () {
     static_assert ( test_ce ( 5, 0 ) == 5, "" );
     static_assert ( test_ce ( 5, 1 ) == 4, "" );
     static_assert ( test_ce ( 5, 5 ) == 0, "" );
-    static_assert ( test_ce ( 5, 9 ) == 0, "" );
     static_assert ( test_ce ( 9, 3 ) == 6, "" );
     }
 #endif

Modified: libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_suffix.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_suffix.pass.cpp?rev=221717&r1=221716&r2=221717&view=diff
==============================================================================
--- libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_suffix.pass.cpp (original)
+++ libcxx/trunk/test/experimental/string.view/string.view.modifiers/remove_suffix.pass.cpp Tue Nov 11 16:07:10 2014
@@ -27,13 +27,11 @@ void test ( const CharT *s, size_t len )
         sv1.remove_suffix ( 1 );
         assert ( sv1.size() == (len - 1));
         assert ( sv1.data() == s);
+        sv1.remove_suffix ( len - 1 );
         }
         
-    sv1.remove_suffix ( len - 1 );
     assert ( sv1.size() == 0 );
-
-    SV sv2 ( s );
-    sv2.remove_suffix ( len << 1 );
+    sv1.remove_suffix ( 0 );
     assert ( sv1.size() == 0 ); 
     }
 
@@ -72,7 +70,6 @@ int main () {
     static_assert ( test_ce ( 5, 0 ) == 5, "" );
     static_assert ( test_ce ( 5, 1 ) == 4, "" );
     static_assert ( test_ce ( 5, 5 ) == 0, "" );
-    static_assert ( test_ce ( 5, 9 ) == 0, "" );
     static_assert ( test_ce ( 9, 3 ) == 6, "" );
     }
 #endif





More information about the cfe-commits mailing list