size_t __pos, size_t __n, _CharT __zero, _CharT __one)
{
reset();
- const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
+ const size_t __rlen = std::min(__n, size_t(__len - __pos));
+ const size_t __nbits = std::min(_Nb, __rlen);
+ for (size_t __i = __rlen - __nbits; __i > 0; --__i)
+ {
+ const _CharT __c = __s[__pos + __rlen - __i];
+ if (!_Traits::eq(__c, __zero) && !_Traits::eq(__c, __one))
+ __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
+ }
for (size_t __i = __nbits; __i > 0; --__i)
{
const _CharT __c = __s[__pos + __nbits - __i];
--- /dev/null
+// { dg-do run }
+
+// PR libstdc++/121054 std::bitset<0>("zero") should throw std::invalid_argument
+#include <bitset>
+#include <stdexcept>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ try {
+ std::bitset<0>(std::string("x"));
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>(std::string("0x"));
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<0>(std::string("01"));
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>(std::string("x0"), 1);
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+#if _GLIBCXX_USE_C99_WCHAR
+ try {
+ std::bitset<1>(std::wstring(L"0x"));
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>(std::wstring(L"x0"), 1);
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+#endif
+}
+
+void
+test02()
+{
+#if __cplusplus >= 201103L
+ try {
+ std::bitset<0>("x");
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>("0x", 2);
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>("0x", 1);
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<0>("01");
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+#if _GLIBCXX_USE_C99_WCHAR
+ try {
+ std::bitset<1>(L"0x", 2);
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>(L"0x", 1);
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+#endif
+#endif
+}
+
+void
+test03()
+{
+#if __cpp_lib_bitset >= 202202L
+ try {
+ std::bitset<0>(std::string_view("x"));
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>(std::string_view("0x"));
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<0>(std::string_view("01"));
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>(std::string_view("x0"), 1);
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+#if _GLIBCXX_USE_C99_WCHAR
+ try {
+ std::bitset<1>(std::wstring_view(L"0x"));
+ VERIFY( false );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( true );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+
+ try {
+ std::bitset<1>(std::wstring_view(L"x0"), 1);
+ VERIFY( true );
+ }
+ catch(std::invalid_argument& fail) {
+ VERIFY( false );
+ }
+ catch(...) {
+ VERIFY( false );
+ }
+#endif
+#endif
+}
+
+int main()
+{
+ test01();
+ test02();
+ test03();
+}