[libcxx-commits] [libcxx] [libc++] Fix erroneous internal capacity evaluation in vector<bool> (PR #120577)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jan 7 06:03:06 PST 2025
https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/120577
>From 4de35d19563c8b6e5634e6b4802d13a983364ad0 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Thu, 19 Dec 2024 08:36:10 -0500
Subject: [PATCH 1/2] Fix erroneous internal capacity evaluation
---
libcxx/include/__vector/vector_bool.h | 2 +-
.../test/std/containers/sequences/vector.bool/flip.pass.cpp | 5 +++++
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 8658745b8a8f9e..ba0a14123583a3 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -115,7 +115,7 @@ class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator> {
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type
__external_cap_to_internal(size_type __n) _NOEXCEPT {
- return (__n - 1) / __bits_per_word + 1;
+ return (__n + __bits_per_word - 1) / __bits_per_word;
}
public:
diff --git a/libcxx/test/std/containers/sequences/vector.bool/flip.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/flip.pass.cpp
index f8f575cdc0e219..fb0de061bd04c3 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/flip.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/flip.pass.cpp
@@ -32,6 +32,11 @@ TEST_CONSTEXPR_CXX20 void test_vector_flip(std::size_t n, Allocator a) {
}
TEST_CONSTEXPR_CXX20 bool tests() {
+ // Test empty vectors
+ test_vector_flip(0, std::allocator<bool>());
+ test_vector_flip(0, min_allocator<bool>());
+ test_vector_flip(0, test_allocator<bool>(5));
+
// Test small vectors with different allocators
test_vector_flip(3, std::allocator<bool>());
test_vector_flip(3, min_allocator<bool>());
>From 756e7a2cde067ddbe01e686987ab80ca3ac0db3b Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Tue, 7 Jan 2025 09:02:32 -0500
Subject: [PATCH 2/2] Fix wrap-around behavior in unsigned arithmetic towards
both directions
---
libcxx/include/__vector/vector_bool.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index ba0a14123583a3..4b5117a918ff72 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -115,7 +115,7 @@ class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator> {
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type
__external_cap_to_internal(size_type __n) _NOEXCEPT {
- return (__n + __bits_per_word - 1) / __bits_per_word;
+ return __n > 0 ? (__n - 1) / __bits_per_word + 1 : size_type(0);
}
public:
More information about the libcxx-commits
mailing list