[libcxx-commits] [libcxx] [libc++] Fix arithmetic underflow in internal capacity evaluation for vector<bool> (PR #120577)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jan 5 18:49:02 PST 2025
https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/120577
>From e1b3a681d3e2535bb2b5c90a66de9c5b4c8c2206 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] 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>());
More information about the libcxx-commits
mailing list