[libcxx-commits] [libcxx] 6c06253 - [libc++] Fix erroneous internal capacity evaluation in vector<bool> (#120577)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 9 01:46:08 PST 2025


Author: Peng Liu
Date: 2025-01-09T17:46:04+08:00
New Revision: 6c06253b8557102c52e252f9025ea3d393603324

URL: https://github.com/llvm/llvm-project/commit/6c06253b8557102c52e252f9025ea3d393603324
DIFF: https://github.com/llvm/llvm-project/commit/6c06253b8557102c52e252f9025ea3d393603324.diff

LOG: [libc++] Fix erroneous internal capacity evaluation in vector<bool> (#120577)

This PR fixes the erroneous internal capacity evaluation in
`vector<bool>`, which caused a subsequent SIGSEGV error when calling
`flip()` on `vector<bool>`. By fixing the internal capacity evaluation,
the SIGSEGV is automatically resolved.

Added: 
    

Modified: 
    libcxx/include/__vector/vector_bool.h
    libcxx/test/std/containers/sequences/vector.bool/flip.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 6c6605fb3bd0b8..2b721e00058bc6 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 > 0 ? (__n - 1) / __bits_per_word + 1 : size_type(0);
   }
 
 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 
diff erent allocators
   test_vector_flip(3, std::allocator<bool>());
   test_vector_flip(3, min_allocator<bool>());


        


More information about the libcxx-commits mailing list