| Issue |
242866
|
| Summary |
[libc++] Floating-point `from_chars` reads `*last`, and applies an unconsumed exponent under `chars_format::fixed`
|
| Labels |
libc++
|
| Assignees |
|
| Reporter |
dotnwat
|
The decimal slow path of `std::from_chars` for `float` and `double` parses the text a second time, and the second parse does not agree with the first. There are two symptoms. Both need a significand that Clinger's fast path and Eisel-Lemire cannot round, for example `9007199254740993` (2^53 + 1).
1. **A read of `*last`.** For text that ends in an exponent marker with nothing after it, such as `9007199254740993e`, the byte at `last` is read. The byte does not change the result, but the read is outside `[first, last)`: it faults if `last` is the start of an inaccessible page, and an ASan-instrumented libc++ reports a `heap-buffer-overflow`.
2. **A wrong value with `chars_format::fixed`.** `9007199254740993e2` gives `9.0071992547409933e+17` with `ptr` at the `e`. The exponent is left unconsumed, as LWG3456 has it, but it is applied to the value anyway. `9007199254740992e2`, which a fast path handles, correctly gives `9007199254740992`.
### Reproducer
```cpp
#include <charconv>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <string_view>
#include <sys/mman.h>
#include <unistd.h>
// Converts a copy of `text` that ends where an inaccessible page begins,
// so a read of `*last` faults.
static void convert(std::string_view text, std::chars_format format) {
const auto page = static_cast<std::size_t>(sysconf(_SC_PAGESIZE));
void* map = mmap(nullptr, 2 * page, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
char* last = static_cast<char*>(map) + page;
mprotect(last, page, PROT_NONE);
char* first = last - text.size();
std::memcpy(first, text.data(), text.size());
double value = 0;
const auto result = std::from_chars(first, last, value, format);
std::printf("%-20.*s %-7s -> %.17g, consumed %td of %zu\n",
static_cast<int>(text.size()), text.data(),
format == std::chars_format::fixed ? "fixed" : "general",
value, result.ptr - first, text.size());
std::fflush(stdout);
}
int main() {
convert("9007199254740992e2", std::chars_format::fixed);
convert("9007199254740993e2", std::chars_format::fixed);
convert("9007199254740992e", std::chars_format::general);
convert("9007199254740993e", std::chars_format::general);
}
```
Output with libc++ 23.1.2 on x86-64 Linux:
```
9007199254740992e2 fixed -> 9007199254740992, consumed 16 of 18
9007199254740993e2 fixed -> 9.0071992547409933e+17, consumed 16 of 18
9007199254740992e general -> 9007199254740992, consumed 16 of 17
Segmentation fault
```
Expected:
```
9007199254740992e2 fixed -> 9007199254740992, consumed 16 of 18
9007199254740993e2 fixed -> 9007199254740992, consumed 16 of 18
9007199254740992e general -> 9007199254740992, consumed 16 of 17
9007199254740993e general -> 9007199254740992, consumed 16 of 17
```
`float` reads `*last` the same way, for example with `16777217e` (2^24 + 1).
With an ASan-instrumented libc++ and the text in a heap buffer of exactly 17 bytes, the read is reported as:
```
ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 1
#0 __llvm_libc_common_utils::internal::HighPrecisionDecimal::HighPrecisionDecimal<char>(char const*, unsigned long)
#1 __llvm_libc_common_utils::internal::simple_decimal_conversion<double, char>(char const*, unsigned long, RoundDirection)
#2 __llvm_libc_common_utils::internal::decimal_exp_to_float<double, char>(ExpandedFloat<double>, bool, RoundDirection, char const*, unsigned long)
#3 std::__1::__from_chars_floating_point_decimal<double>(char const*, char const*, std::__1::chars_format, char const*, bool)
#4 std::__1::__from_chars_floating_point_impl<double>(char const*, char const*, std::__1::chars_format)
#5 std::__1::from_chars(char const*, char const*, double&, std::__1::chars_format)
is located 0 bytes after 17-byte region
```
### Cause
`__from_chars_floating_point_decimal` in `libcxx/src/include/from_chars_floating_point.h` parses the significand and the exponent itself, then calls
```cpp
auto __temp = LIBC_NAMESPACE::shared::decimal_exp_to_float<_Fp>(
{__fractional.__mantissa, __exponent},
__fractional.__truncated,
LIBC_NAMESPACE::shared::RoundDirection::Nearest,
__ptr,
__last - __ptr);
```
which hands over everything up to `__last`, not only the part it accepted. When the fast paths fail, `decimal_exp_to_float` falls back to `simple_decimal_conversion(numStart, num_len)`, which builds a `HighPrecisionDecimal` from that text and so parses it again, with its own grammar:
- It always accepts an exponent, so under `chars_format::fixed` it applies the one libc++ decided to leave alone.
- After an exponent marker it reads the next character without checking the length, in `libc/src/__support/high_precision_decimal.h`:
```cpp
if (num_cur < num_len && tolower(num_string[num_cur]) ==
constants<CharType>::DECIMAL_EXPONENT_MARKER) {
++num_cur;
if (isdigit(num_string[num_cur]) || get_sign(num_string + num_cur) != 0) {
```
That is harmless for `strtod`, where the next character is at worst the terminator, but not for a range bounded by a length. libc++'s own `__parse_exponent` checks `__offset + 1 < __n` and correctly leaves a lone `e` unconsumed.
### Possible fix
Passing the slow path only the text libc++ accepted, from `__ptr` up to `__first + __offset` (the position `from_chars` returns), looks like it would fix both, since the second parse would then see exactly what the first one matched. Separately, `HighPrecisionDecimal` takes a length and should check `num_cur < num_len` after the `++num_cur`.
### Versions
- libc++ 23.1.2 built from source, x86-64 Linux: both symptoms.
- libc++ 21.1.8 as shipped in FreeBSD 16.0-CURRENT, amd64: the read of `*last`, for `double` and `float`. I did not test the `fixed` case there.
- `main` on 2026-11-15 has the same code in both files.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs