Skip to content

Commit 595ed63

Browse files
committed
Fix to_ulong to throw overflow_error as expected
1 parent 16484e6 commit 595ed63

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

libcxx/include/bitset

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ protected:
219219

220220
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
221221
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
222-
return to_ulong(_BoolConstant < _Size< sizeof(unsigned long) * CHAR_BIT>());
222+
return __to_ulong(_BoolConstant < _Size< sizeof(unsigned long) * CHAR_BIT>());
223223
}
224224
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
225-
return to_ullong(_BoolConstant < _Size< sizeof(unsigned long long) * CHAR_BIT>());
225+
return __to_ullong(_BoolConstant < _Size< sizeof(unsigned long long) * CHAR_BIT>());
226226
}
227227

228228
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
@@ -234,14 +234,14 @@ private:
234234
void __init(unsigned long long __v, false_type) _NOEXCEPT;
235235
_LIBCPP_HIDE_FROM_ABI void __init(unsigned long long __v, true_type) _NOEXCEPT;
236236
# endif // _LIBCPP_CXX03_LANG
237-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(false_type) const;
238-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type) const;
239-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type, false_type) const;
240-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type, true_type) const;
241-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(false_type) const;
242-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type) const;
243-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, false_type) const;
244-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, true_type) const;
237+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(false_type) const;
238+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(true_type) const;
239+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(true_type, false_type) const;
240+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(true_type, true_type) const;
241+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(false_type) const;
242+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type) const;
243+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type, false_type) const;
244+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __to_ullong(true_type, true_type) const;
245245
};
246246

247247
template <size_t _N_words, size_t _Size>
@@ -352,28 +352,28 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Siz
352352

353353
template <size_t _N_words, size_t _Size>
354354
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
355-
__bitset<_N_words, _Size>::to_ulong(false_type) const {
355+
__bitset<_N_words, _Size>::__to_ulong(false_type) const {
356356
if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
357-
std::__throw_overflow_error("bitset to_ulong overflow error");
357+
std::__throw_overflow_error("bitset __to_ulong overflow error");
358358

359-
return to_ulong(true_type());
359+
return __to_ulong(true_type());
360360
}
361361

362362
template <size_t _N_words, size_t _Size>
363363
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
364-
__bitset<_N_words, _Size>::to_ulong(true_type) const {
365-
return to_ulong(true_type(), _BoolConstant<sizeof(__storage_type) < sizeof(unsigned long)>());
364+
__bitset<_N_words, _Size>::__to_ulong(true_type) const {
365+
return __to_ulong(true_type(), _BoolConstant<sizeof(__storage_type) < sizeof(unsigned long)>());
366366
}
367367

368368
template <size_t _N_words, size_t _Size>
369369
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
370-
__bitset<_N_words, _Size>::to_ulong(true_type, false_type) const {
370+
__bitset<_N_words, _Size>::__to_ulong(true_type, false_type) const {
371371
return static_cast<unsigned long>(__first_[0]);
372372
}
373373

374374
template <size_t _N_words, size_t _Size>
375375
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
376-
__bitset<_N_words, _Size>::to_ulong(true_type, true_type) const {
376+
__bitset<_N_words, _Size>::__to_ulong(true_type, true_type) const {
377377
unsigned long __r = static_cast<unsigned long>(__first_[0]);
378378
_LIBCPP_DIAGNOSTIC_PUSH
379379
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
@@ -382,30 +382,31 @@ __bitset<_N_words, _Size>::to_ulong(true_type, true_type) const {
382382
_LIBCPP_DIAGNOSTIC_POP
383383
return __r;
384384
}
385+
385386
template <size_t _N_words, size_t _Size>
386387
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
387-
__bitset<_N_words, _Size>::to_ullong(false_type) const {
388+
__bitset<_N_words, _Size>::__to_ullong(false_type) const {
388389
if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true) != __e)
389-
std::__throw_overflow_error("bitset to_ullong overflow error");
390+
std::__throw_overflow_error("bitset __to_ullong overflow error");
390391

391-
return to_ullong(true_type());
392+
return __to_ullong(true_type());
392393
}
393394

394395
template <size_t _N_words, size_t _Size>
395396
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
396-
__bitset<_N_words, _Size>::to_ullong(true_type) const {
397-
return to_ullong(true_type(), _BoolConstant<sizeof(__storage_type) < sizeof(unsigned long long)>());
397+
__bitset<_N_words, _Size>::__to_ullong(true_type) const {
398+
return __to_ullong(true_type(), _BoolConstant<sizeof(__storage_type) < sizeof(unsigned long long)>());
398399
}
399400

400401
template <size_t _N_words, size_t _Size>
401402
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
402-
__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const {
403+
__bitset<_N_words, _Size>::__to_ullong(true_type, false_type) const {
403404
return static_cast<unsigned long long>(__first_[0]);
404405
}
405406

406407
template <size_t _N_words, size_t _Size>
407408
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
408-
__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
409+
__bitset<_N_words, _Size>::__to_ullong(true_type, true_type) const {
409410
unsigned long long __r = static_cast<unsigned long long>(__first_[0]);
410411
_LIBCPP_DIAGNOSTIC_PUSH
411412
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
@@ -510,6 +511,10 @@ protected:
510511
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
511512

512513
_LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
514+
515+
private:
516+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(false_type) const;
517+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong(true_type) const;
513518
};
514519

515520
template <size_t _Size>
@@ -546,6 +551,19 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Siz
546551

547552
template <size_t _Size>
548553
inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
554+
return __to_ulong(_BoolConstant < _Size< sizeof(unsigned long) * CHAR_BIT>());
555+
}
556+
557+
template <size_t _Size>
558+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::__to_ulong(false_type) const {
559+
if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
560+
__throw_overflow_error("__bitset<1, _Size>::__to_ulong overflow error");
561+
562+
return static_cast<unsigned long>(__first_);
563+
}
564+
565+
template <size_t _Size>
566+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::__to_ulong(true_type) const {
549567
return static_cast<unsigned long>(__first_);
550568
}
551569

0 commit comments

Comments
 (0)