mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-07 01:06:48 +08:00
Return invalid_argument in more places
This commit is contained in:
parent
3d446f1eba
commit
7a21a8d6d7
@ -446,10 +446,12 @@ from_chars_result_t<UC> parse_int_string(UC const* p, UC const* pend, T& value,
|
|||||||
{
|
{
|
||||||
from_chars_result_t<UC> answer;
|
from_chars_result_t<UC> answer;
|
||||||
|
|
||||||
|
UC const* const first = p;
|
||||||
|
|
||||||
bool negative = (*p == UC('-'));
|
bool negative = (*p == UC('-'));
|
||||||
if (!std::is_signed<T>::value && negative) {
|
if (!std::is_signed<T>::value && negative) {
|
||||||
answer.ec = std::errc::invalid_argument;
|
answer.ec = std::errc::invalid_argument;
|
||||||
answer.ptr = p;
|
answer.ptr = first;
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
|
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
|
||||||
@ -478,22 +480,31 @@ from_chars_result_t<UC> parse_int_string(UC const* p, UC const* pend, T& value,
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t digit_count = size_t(p - start_digits);
|
size_t digit_count = size_t(p - start_digits);
|
||||||
answer.ec = std::errc::result_out_of_range;
|
|
||||||
|
if (digit_count == 0) {
|
||||||
|
answer.ec = std::errc::invalid_argument;
|
||||||
|
answer.ptr = first;
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
answer.ptr = p;
|
answer.ptr = p;
|
||||||
|
|
||||||
// check u64 overflow
|
// check u64 overflow
|
||||||
size_t max_digits = max_digits_u64(base);
|
size_t max_digits = max_digits_u64(base);
|
||||||
if (digit_count == 0 || digit_count > max_digits) {
|
if (digit_count > max_digits) {
|
||||||
|
answer.ec = std::errc::result_out_of_range;
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
// this check can be eliminated for all other types, but they will all require a max_digits(base) equivalent
|
// this check can be eliminated for all other types, but they will all require a max_digits(base) equivalent
|
||||||
if (digit_count == max_digits && i < min_safe_u64(base)) {
|
if (digit_count == max_digits && i < min_safe_u64(base)) {
|
||||||
|
answer.ec = std::errc::result_out_of_range;
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check other types overflow
|
// check other types overflow
|
||||||
if (!std::is_same<T, uint64_t>::value) {
|
if (!std::is_same<T, uint64_t>::value) {
|
||||||
if (i > uint64_t(std::numeric_limits<T>::max()) + uint64_t(negative)) {
|
if (i > uint64_t(std::numeric_limits<T>::max()) + uint64_t(negative)) {
|
||||||
|
answer.ec = std::errc::result_out_of_range;
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user