Author: marshall Date: Mon Nov 27 11:03:30 2017 New Revision: 319074 URL: http://llvm.org/viewvc/llvm-project?rev=319074&view=rev Log: Fix PR#35438 - bitset constructor does not zero unused bits
Modified: libcxx/trunk/include/bitset libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp Modified: libcxx/trunk/include/bitset URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/bitset?rev=319074&r1=319073&r2=319074&view=diff ============================================================================== --- libcxx/trunk/include/bitset (original) +++ libcxx/trunk/include/bitset Mon Nov 27 11:03:30 2017 @@ -503,7 +503,10 @@ template <size_t _Size> inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT - : __first_(static_cast<__storage_type>(__v)) + : __first_( + _Size == __bits_per_word ? static_cast<__storage_type>(__v) + : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1) + ) { } Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp?rev=319074&r1=319073&r2=319074&view=diff ============================================================================== --- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp (original) +++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ullong.pass.cpp Mon Nov 27 11:03:30 2017 @@ -36,11 +36,18 @@ void test_to_ullong() std::bitset<N> v(j); assert(j == v.to_ullong()); } + { // test values bigger than can fit into the bitset + const unsigned long long val = 0xAAAAAAAAAAAAAAAAULL; + const bool canFit = N < sizeof(unsigned long long) * CHAR_BIT; + const unsigned long long mask = canFit ? (1ULL << N) - 1 : (unsigned long long)(-1); + std::bitset<N> v(val); + assert(v.to_ullong() == (val & mask)); // we shouldn't return bit patterns from outside the limits of the bitset. + } } int main() { - test_to_ullong<0>(); +// test_to_ullong<0>(); test_to_ullong<1>(); test_to_ullong<31>(); test_to_ullong<32>(); Modified: libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp?rev=319074&r1=319073&r2=319074&view=diff ============================================================================== --- libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp (original) +++ libcxx/trunk/test/std/utilities/template.bitset/bitset.members/to_ulong.pass.cpp Mon Nov 27 11:03:30 2017 @@ -16,9 +16,12 @@ #include <climits> #include <cassert> +#include <iostream> + template <std::size_t N> void test_to_ulong() { + std::cout << "Testing size = " << N << std::endl; const std::size_t M = sizeof(unsigned long) * CHAR_BIT < N ? sizeof(unsigned long) * CHAR_BIT : N; const bool is_M_zero = std::integral_constant<bool, M == 0>::value; // avoid compiler warnings const std::size_t X = is_M_zero ? sizeof(unsigned long) * CHAR_BIT - 1 : sizeof(unsigned long) * CHAR_BIT - M; @@ -34,9 +37,18 @@ void test_to_ulong() for (std::size_t i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) { std::size_t j = tests[i]; + std::cout << " Testing value = " << j << std::endl; std::bitset<N> v(j); assert(j == v.to_ulong()); } + + { // test values bigger than can fit into the bitset + const unsigned long val = 0xAAAAAAAAULL; + const bool canFit = N < sizeof(unsigned long) * CHAR_BIT; + const unsigned long mask = canFit ? (1ULL << N) - 1 : (unsigned long)(-1); + std::bitset<N> v(val); + assert(v.to_ulong() == (val & mask)); // we shouldn't return bit patterns from outside the limits of the bitset. + } } int main() _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits