[libcxx-commits] [PATCH] D114395: [libc++] Fix the return value of max_size()

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


This revision was not accepted when it landed; it landed in state "Needs Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2db67e97712e: [libc++] Fix the return value of max_size() (authored by ldionne).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114395/new/

https://reviews.llvm.org/D114395

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


Index: libcxx/test/std/strings/string.view/string.view.capacity/capacity.pass.cpp
===================================================================
--- libcxx/test/std/strings/string.view/string.view.capacity/capacity.pass.cpp
+++ 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 @@
     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>
Index: libcxx/include/string_view
===================================================================
--- libcxx/include/string_view
+++ libcxx/include/string_view
@@ -356,7 +356,7 @@
     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; }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114395.392127.patch
Type: text/x-patch
Size: 1679 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211206/dafb5341/attachment.bin>


More information about the libcxx-commits mailing list