[libcxx-commits] [libcxx] 2db67e9 - [libc++] Fix the return value of max_size()

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 6 10:53:33 PST 2021


Author: Louis Dionne
Date: 2021-12-06T13:53:25-05:00
New Revision: 2db67e97712e257d9784f4c3812432443f72145e

URL: https://github.com/llvm/llvm-project/commit/2db67e97712e257d9784f4c3812432443f72145e
DIFF: https://github.com/llvm/llvm-project/commit/2db67e97712e257d9784f4c3812432443f72145e.diff

LOG: [libc++] Fix the return value of max_size()

I assume nobody ever uses std::string_view::max_size() outside of
testing. However, we should still return a value that is based on
something with a reasonable rationale. Previously, we would forget
to take into account the size of the character type stored in the
string, and this patch takes that into account.

Thanks to @mclow.lists for pointing out this issue.

Differential Revision: https://reviews.llvm.org/D114395

Added: 
    

Modified: 
    libcxx/include/string_view
    libcxx/test/std/strings/string.view/string.view.capacity/capacity.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/string_view b/libcxx/include/string_view
index c40782d1ca8b0..3861ad67ca5de 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -356,7 +356,7 @@ public:
     size_type length()   const _NOEXCEPT { return __size; }
 
     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
-    size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
+    size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max() / sizeof(value_type); }
 
     _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
     bool empty()         const _NOEXCEPT { return __size == 0; }

diff  --git a/libcxx/test/std/strings/string.view/string.view.capacity/capacity.pass.cpp b/libcxx/test/std/strings/string.view/string.view.capacity/capacity.pass.cpp
index 2283ccab6d7c5..0fdbe0e03ed2b 100644
--- a/libcxx/test/std/strings/string.view/string.view.capacity/capacity.pass.cpp
+++ b/libcxx/test/std/strings/string.view/string.view.capacity/capacity.pass.cpp
@@ -16,6 +16,7 @@
 
 #include <string_view>
 #include <cassert>
+#include <limits>
 
 #include "test_macros.h"
 
@@ -42,6 +43,16 @@ void test1 () {
     assert ( sv1.size() == sv1.length());
     assert ( sv1.max_size() > sv1.size());
     }
+
+    // Sanity check max_size() -- a string_view can't store more bytes than a single object
+    // can contain. Any implementation that fails this check is certainly lying.
+    {
+        typedef typename SV::value_type CharT;
+        typedef typename SV::size_type Size;
+        SV sv;
+        assert(sv.max_size() <= std::numeric_limits<Size>::max() / sizeof(CharT));
+        LIBCPP_ASSERT(sv.max_size() == std::numeric_limits<Size>::max() / sizeof(CharT));
+    }
 }
 
 template<typename CharT>


        


More information about the libcxx-commits mailing list