Set failbit when strtold sets errno to ERANGE when parsing floating point values.

llvm-svn: 179461
This commit is contained in:
Howard Hinnant 2013-04-13 18:19:25 +00:00
parent adc1727c39
commit 40487ca25e
2 changed files with 55 additions and 0 deletions

View File

@ -906,13 +906,20 @@ __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
{
if (__a != __a_end)
{
typename remove_reference<decltype(errno)>::type __save_errno = errno;
errno = 0;
char *__p2;
long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
typename remove_reference<decltype(errno)>::type __current_errno = errno;
if (__current_errno == 0)
errno = __save_errno;
if (__p2 != __a_end)
{
__err = ios_base::failbit;
return 0;
}
else if (__current_errno == ERANGE)
__err = ios_base::failbit;
return static_cast<_Tp>(__ld);
}
__err = ios_base::failbit;

View File

@ -168,4 +168,52 @@ int main()
assert(err == ios.goodbit);
assert(std::isnan(v));
}
{
const char str[] = "1.189731495357231765021264e+49321";
std::ios_base::iostate err = ios.goodbit;
v = 0;
input_iterator<const char*> iter =
f.get(input_iterator<const char*>(str),
input_iterator<const char*>(str+sizeof(str)),
ios, err, v);
assert(iter.base() == str+sizeof(str)-1);
assert(err == ios.failbit);
assert(v == INFINITY);
}
{
const char str[] = "1.189731495357231765021264e+49329";
std::ios_base::iostate err = ios.goodbit;
v = 0;
input_iterator<const char*> iter =
f.get(input_iterator<const char*>(str),
input_iterator<const char*>(str+sizeof(str)),
ios, err, v);
assert(iter.base() == str+sizeof(str)-1);
assert(err == ios.failbit);
assert(v == INFINITY);
}
{
const char str[] = "11.189731495357231765021264e+4932";
std::ios_base::iostate err = ios.goodbit;
v = 0;
input_iterator<const char*> iter =
f.get(input_iterator<const char*>(str),
input_iterator<const char*>(str+sizeof(str)),
ios, err, v);
assert(iter.base() == str+sizeof(str)-1);
assert(err == ios.failbit);
assert(v == INFINITY);
}
{
const char str[] = "91.189731495357231765021264e+4932";
std::ios_base::iostate err = ios.goodbit;
v = 0;
input_iterator<const char*> iter =
f.get(input_iterator<const char*>(str),
input_iterator<const char*>(str+sizeof(str)),
ios, err, v);
assert(iter.base() == str+sizeof(str)-1);
assert(err == ios.failbit);
assert(v == INFINITY);
}
}