Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for PPC64 errno value converting LDBL_MIN #104

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/boost/charconv/to_chars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,12 @@
format[pos] = '\n';
const auto rv = print_val(first, last - first, format, value);

if (rv == -1)
if (rv <= 0)

Check warning on line 619 in include/boost/charconv/to_chars.hpp

View check run for this annotation

Codecov / codecov/patch

include/boost/charconv/to_chars.hpp#L619

Added line #L619 was not covered by tests
{
return {last, static_cast<std::errc>(errno)};
}

return {first + rv, static_cast<std::errc>(errno)};
return {first + rv, std::errc()};

Check warning on line 624 in include/boost/charconv/to_chars.hpp

View check run for this annotation

Codecov / codecov/patch

include/boost/charconv/to_chars.hpp#L624

Added line #L624 was not covered by tests
}

} // Namespace detail
Expand Down
12 changes: 11 additions & 1 deletion src/from_chars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,17 @@
char* ptr = nullptr;
value = std::strtold(temp.c_str(), &ptr);
r.ptr = ptr;
r.ec = static_cast<std::errc>(errno);

// See: https://github.com/cppalliance/charconv/issues/103
// The value of errno should be 0 since the conversion is successful, but it is incorrect
if (value == 0 || value == HUGE_VALL)
{
r.ec = static_cast<std::errc>(errno);

Check warning on line 222 in src/from_chars.cpp

View check run for this annotation

Codecov / codecov/patch

src/from_chars.cpp#L222

Added line #L222 was not covered by tests
}
else
{
r.ec = std::errc();
}
}

return r;
Expand Down
38 changes: 27 additions & 11 deletions test/limits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ std::ostream& operator<<( std::ostream& os, boost::int128_type v )
#include <boost/core/lightweight_test.hpp>
#include <system_error>
#include <limits>
#include <string>

void test_odr_use( int const* );

Expand Down Expand Up @@ -124,24 +125,46 @@ template<typename T> void test_floating_point( T value )
{
char buffer[ boost::charconv::limits<T>::max_chars10 ];
auto r = boost::charconv::to_chars( buffer, buffer + sizeof( buffer ), value );
BOOST_TEST(r.ec == std::errc());
if (!BOOST_TEST(r.ec == std::errc()))
{
std::cerr << " Value: " << value
<< "\nBuffer: " << std::string(buffer)
<< "\n Ec: " << static_cast<int>(r.ec) << std::endl;
}

T v2 = 0;
auto r2 = boost::charconv::from_chars( buffer, r.ptr, v2 );

BOOST_TEST(r2.ec == std::errc()) && BOOST_TEST_EQ( v2, value );
if (!BOOST_TEST(r2.ec == std::errc()) && BOOST_TEST_EQ( v2, value ))
{
std::cerr << " Value: " << value
<< "\nBuffer: " << std::string(buffer)
<< "\nRetVal: " << v2
<< "\n Ec: " << static_cast<int>(r2.ec) << std::endl;
}
}

// no base, max_chars
{
char buffer[ boost::charconv::limits<T>::max_chars ];
auto r = boost::charconv::to_chars( buffer, buffer + sizeof( buffer ), value );
BOOST_TEST(r.ec == std::errc());
if (!BOOST_TEST(r.ec == std::errc()))
{
std::cerr << " Value: " << value
<< "\nBuffer: " << std::string(buffer)
<< "\n Ec: " << static_cast<int>(r.ec) << std::endl;
}

T v2 = 0;
auto r2 = boost::charconv::from_chars( buffer, r.ptr, v2 );

BOOST_TEST(r2.ec == std::errc()) && BOOST_TEST_EQ( v2, value );
if (!BOOST_TEST(r2.ec == std::errc()) && BOOST_TEST_EQ( v2, value ))
{
std::cerr << " Value: " << value
<< "\nBuffer: " << std::string(buffer)
<< "\nRetVal: " << v2
<< "\n Ec: " << static_cast<int>(r2.ec) << std::endl;
}
}
}

Expand Down Expand Up @@ -173,17 +196,10 @@ int main()
test_integral<long long>();
test_integral<unsigned long long>();

#if !defined(__CYGWIN__) && !defined(__s390x__) && !((defined(__arm__) || defined(__aarch64__)) && !defined(__APPLE__)) && !(defined(__APPLE__) && (__clang_major__ == 12))

// the stub implementations fail under Cygwin, s390x, linux ARM, and Apple Clang w/Xcode 12.2;
// re-enable these when we have real ones

test_floating_point<float>();
test_floating_point<double>();
test_floating_point<long double>();

#endif // Broken Platforms

#ifdef BOOST_CHARCONV_HAS_INT128

test_integral<boost::int128_type>();
Expand Down