Skip to content
Snippets Groups Projects
Commit 1c153033 authored by Jonathan Wakely's avatar Jonathan Wakely
Browse files

libstdc++: Fix <charconv> uses of signed types with <bit> functions

In <charconv> we pass the int __base parameter to our internal versions
of <bit> functions, __bit_width and __countr_zero. Those functions are
only defined for unsigned types, so we need to convert the base to
unsigned. The base must be in the range [2,36] so we can mask off the
low bits and then convert that to unsigned, so that we don't need to
care about negative values becoming large unsigned values.

libstdc++-v3/ChangeLog:

	* include/std/charconv (__from_chars_pow2_base): Convert base to
	unsigned for call to __countr_zero.
	(__from_chars_alnum): Likewise for call to __bit_width.
parent 6f2fc42d
No related branches found
No related tags found
No related merge requests found
...@@ -465,7 +465,7 @@ namespace __detail ...@@ -465,7 +465,7 @@ namespace __detail
// __glibcxx_assert((__base & (__base - 1)) == 0); // __glibcxx_assert((__base & (__base - 1)) == 0);
// __glibcxx_assert(_DecOnly ? __base <= 8 : __base <= 32); // __glibcxx_assert(_DecOnly ? __base <= 8 : __base <= 32);
const int __log2_base = __countr_zero(__base); const int __log2_base = __countr_zero(unsigned(__base & 0x3f));
const ptrdiff_t __len = __last - __first; const ptrdiff_t __len = __last - __first;
ptrdiff_t __i = 0; ptrdiff_t __i = 0;
...@@ -517,9 +517,9 @@ namespace __detail ...@@ -517,9 +517,9 @@ namespace __detail
__from_chars_alnum(const char*& __first, const char* __last, _Tp& __val, __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
int __base) int __base)
{ {
// __glibcxx_assert(!_DecOnly || __base <= 10); // __glibcxx_assert(_DecOnly ? __base <= 10 : __base <= 36);
const int __bits_per_digit = __bit_width(__base); const int __bits_per_digit = __bit_width(unsigned(__base & 0x3f));
int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits; int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits;
for (; __first != __last; ++__first) for (; __first != __last; ++__first)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment