Skip to content

[libc++] Fix constructing bitset from non-null-terminated arrays #143691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions libcxx/include/bitset
Original file line number Diff line number Diff line change
Expand Up @@ -645,16 +645,13 @@ public:
template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
const _CharT* __str,
# if _LIBCPP_STD_VER >= 26
typename basic_string_view<_CharT>::size_type __n = basic_string_view<_CharT>::npos,
# else
typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
# endif
size_t __n = basic_string<_CharT>::npos,
_CharT __zero = _CharT('0'),
_CharT __one = _CharT('1')) {

size_t __rlen = std::min(__n, char_traits<_CharT>::length(__str));
__init_from_string_view(basic_string_view<_CharT>(__str, __rlen), __zero, __one);
if (__n == basic_string<_CharT>::npos)
__init_from_string_view(basic_string_view<_CharT>(__str), __zero, __one);
else
__init_from_string_view(basic_string_view<_CharT>(__str, __n), __zero, __one);
}
# if _LIBCPP_STD_VER >= 26
template <class _CharT, class _Traits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,35 @@ TEST_CONSTEXPR_CXX23 void test_char_pointer_ctor()
for (std::size_t i = 10; i < v.size(); ++i)
assert(v[i] == false);
}
// Verify that this constructor doesn't read over the given bound.
// See https://github.com/llvm/llvm-project/issues/143684
{
const char not_null_terminated[] = {'1', '0', '1', '0', '1', '0', '1', '0', '1', '0'};
std::bitset<N> v(not_null_terminated, 10);
std::size_t M = std::min<std::size_t>(v.size(), 10);
for (std::size_t i = 0; i < M; ++i)
assert(v[i] == (not_null_terminated[M - 1 - i] == '1'));
for (std::size_t i = 10; i < v.size(); ++i)
assert(!v[i]);
}
{
const char not_null_terminated[] = {'1', 'a', '1', 'a', '1', 'a', '1', 'a', '1', 'a'};
std::bitset<N> v(not_null_terminated, 10, 'a');
std::size_t M = std::min<std::size_t>(v.size(), 10);
for (std::size_t i = 0; i < M; ++i)
assert(v[i] == (not_null_terminated[M - 1 - i] == '1'));
for (std::size_t i = 10; i < v.size(); ++i)
assert(!v[i]);
}
{
const char not_null_terminated[] = {'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a'};
std::bitset<N> v(not_null_terminated, 10, 'a', 'b');
std::size_t M = std::min<std::size_t>(v.size(), 10);
for (std::size_t i = 0; i < M; ++i)
assert(v[i] == (not_null_terminated[M - 1 - i] == 'b'));
for (std::size_t i = 10; i < v.size(); ++i)
assert(!v[i]);
}
}

TEST_CONSTEXPR_CXX23 bool test() {
Expand Down
Loading