Commit 43d3c7a4 by Derek Mauro Committed by Copybara-Service

InlinedVector: Correct the computation of max_size()

Corrects the computation of max_size(), so that it accounts for the
size of the objects.

PiperOrigin-RevId: 471343778
Change-Id: I68e222cefaa0295b8d8c38d00308a29df4165e81
parent 6a262fda
...@@ -275,8 +275,9 @@ class InlinedVector { ...@@ -275,8 +275,9 @@ class InlinedVector {
size_type max_size() const noexcept { size_type max_size() const noexcept {
// One bit of the size storage is used to indicate whether the inlined // One bit of the size storage is used to indicate whether the inlined
// vector contains allocated memory. As a result, the maximum size that the // vector contains allocated memory. As a result, the maximum size that the
// inlined vector can express is half of the max for `size_type`. // inlined vector can express is half of the max for
return (std::numeric_limits<size_type>::max)() / 2; // AllocatorTraits<A>::max_size();
return AllocatorTraits<A>::max_size(storage_.GetAllocator()) / 2;
} }
// `InlinedVector::capacity()` // `InlinedVector::capacity()`
......
...@@ -255,6 +255,7 @@ TEST(IntVec, Hardened) { ...@@ -255,6 +255,7 @@ TEST(IntVec, Hardened) {
#if !defined(NDEBUG) || ABSL_OPTION_HARDENED #if !defined(NDEBUG) || ABSL_OPTION_HARDENED
EXPECT_DEATH_IF_SUPPORTED(v[10], ""); EXPECT_DEATH_IF_SUPPORTED(v[10], "");
EXPECT_DEATH_IF_SUPPORTED(v[-1], ""); EXPECT_DEATH_IF_SUPPORTED(v[-1], "");
EXPECT_DEATH_IF_SUPPORTED(v.resize(v.max_size() + 1), "");
#endif #endif
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment