[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
Wed Nov 24 14:55:54 PST 2021


ldionne updated this revision to Diff 389624.
ldionne marked 2 inline comments as done.
ldionne added a comment.

Apply reviewer feedback. Thanks!


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
@@ -331,7 +331,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.389624.patch
Type: text/x-patch
Size: 1679 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211124/27e6bd10/attachment-0001.bin>


More information about the libcxx-commits mailing list