boostorg / test

The reference C++ unit testing framework (TDD, xUnit, C++03/11/14/17)
http://boost.org/libs/test
Boost Software License 1.0
182 stars 141 forks source link

Overflow warning in `print_helper.hpp` #398

Closed JBouwer closed 9 months ago

JBouwer commented 11 months ago

Using: clang++ -stdlib=libc++ -std=c++17 -Wall ... on an Intel x64 Mac (Clang 11), with selected code [^1] results in:

...
In file included from boost/test/unit_test.hpp:18:
In file included from boost/test/test_tools.hpp:42:
In file included from boost/test/tools/context.hpp:19:
In file included from boost/test/utils/lazy_ostream.hpp:16:
boost/test/tools/detail/print_helper.hpp:100:71: warning: overflow in expression; result is 2147483347 with type 'int' [-Winteger-overflow]
            return ostr.precision( 2 + std::numeric_limits<T>::digits * 301/1000 );
                                                                      ^

It is pretty easily fixed by promoting the calculation to the parameter type; return ostr.precision( 2 + std::streamsize(std::numeric_limits<T>::digits) * 301/1000 );

[^1]: Occurences in my code are pretty obscure: For me it manifests during (implicit) printing of big enough types; I can induce it with: BOOST_CHECK_NE( boost::multiprecision::checked_cpp_int(std::numeric_limits<int>::max()), 0 );