Skip to content

to_chars() error with chars_format::fixed and defaulted precision #825

@k3DW

Description

@k3DW

This happens when the absolute value is less than 1/10.

This is fine

char buffer[1024]{};
auto num = decimal32{ 1000000, -7 };
to_chars(std::begin(buffer), std::end(buffer), num, chars_format::fixed);

This crashes in a memset() call

char buffer[1024]{};
auto num = decimal32{ 999999, -7 };
to_chars(std::begin(buffer), std::end(buffer), num, chars_format::fixed);

The error is coming from "charconv.hpp". It's trying to memset() with a size of static_cast<std::size_t>(-1), inside the function detail::to_chars_fixed_impl().

else if (num_leading_zeros > precision)
{
    *first++ = '0';
    *first++ = '.';
    std::memset(first, '0', static_cast<std::size_t>(precision));
    return {first + precision, std::errc()};
}

It looks like real_precision is defined at the top of the function but isn't used other than checking the buffer size. I'd hazard a guess that all uses of precision in this function should actually say real_precision.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions