[libcxx-dev] [libcxx-bugs] Incoherent behaviour on integer overflow

Hans Wennborg via libcxx-dev libcxx-dev at lists.llvm.org
Mon Jan 27 09:56:14 PST 2020


Trying again, with the original attachments.

On Mon, Jan 27, 2020 at 9:54 AM Hans Wennborg <hans at chromium.org> wrote:
>
> +libcxx-dev as I'm not sure how many people read libcxx-bugs.
>
> On Mon, Jan 27, 2020 at 9:37 AM phiippe dunski via libcxx-bugs
> <libcxx-bugs at lists.llvm.org> wrote:
> >
> > Hello,
> > I'm not sure that this mail should really have to be sent on this list, Maybe should I report this to specific clang mailing list. But, I hope you put me on the right way...
> >
> > Here is the overflow_error.cpp which contains what I expected to be 16 error case when using std::integral_constant.
> >
> > The point was, for me, perfectly clear : for each integer type, if I try to define some constant whose value is  std::numeric_limits<the_type>::min()-1 or std::numeric_limits<the_type>::max() + 1, I should get the same behaviour : at least some compile time warning, or mabe even some compile time error.
> >
> > Having two error cases for each data type, and eight data types, I was expecting to get ... sixteen error when trying to compile this file.
> >
> > But, as you can see it in the error_output.txt, they are only fourteen errors:
> > using std::numeric_limits<uint32_t>::max() +1 and std::numeric_limits<uint64_t>::max() + 1 do not produce the same behaviour than usigne other data types or values.
> >
> > IMHO, there is some incoherence behind that.  Isn't it?
> >
> > PS: Please forgive my poor English, but my mother tongue is French
> > _______________________________________________
> > libcxx-bugs mailing list
> > libcxx-bugs at lists.llvm.org
> > https://lists.llvm.org/cgi-bin/mailman/listinfo/libcxx-bugs
-------------- next part --------------
overflow_erro.cpp:4:57: error: non-type template argument evaluates to -129, which cannot be narrowed to type 'signed char' [-Wc++11-narrowing]
using Int8MinError = std::integral_constant<std::int8_t,std::numeric_limits<std::int8_t>::min()-1>;
                                                        ^
overflow_erro.cpp:5:59: error: non-type template argument evaluates to -32769, which cannot be narrowed to type 'short' [-Wc++11-narrowing]
using Int16MinError = std::integral_constant<std::int16_t,std::numeric_limits<std::int16_t>::min()-1>;
                                                          ^
overflow_erro.cpp:6:59: error: non-type template argument is not a constant expression
using Int16MinError = std::integral_constant<std::int32_t,std::numeric_limits<std::int32_t>::min()-1>;
                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
overflow_erro.cpp:6:99: note: value -2147483649 is outside the range of representable values of type 'int'
using Int16MinError = std::integral_constant<std::int32_t,std::numeric_limits<std::int32_t>::min()-1>;
                                                                                                  ^
overflow_erro.cpp:7:59: error: non-type template argument is not a constant expression
using Int16MinError = std::integral_constant<std::int64_t,std::numeric_limits<std::int64_t>::min()-1>;
                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
overflow_erro.cpp:7:99: note: value -9223372036854775809 is outside the range of representable values of type 'long'
using Int16MinError = std::integral_constant<std::int64_t,std::numeric_limits<std::int64_t>::min()-1>;
                                                                                                  ^
overflow_erro.cpp:10:57: error: non-type template argument evaluates to 128, which cannot be narrowed to type 'signed char' [-Wc++11-narrowing]
using Int8MaxError = std::integral_constant<std::int8_t,std::numeric_limits<std::int8_t>::max() + 1>;
                                                        ^
overflow_erro.cpp:11:59: error: non-type template argument evaluates to 32768, which cannot be narrowed to type 'short' [-Wc++11-narrowing]
using Int16MaxError = std::integral_constant<std::int16_t,std::numeric_limits<std::int16_t>::max() + 1>;
                                                          ^
overflow_erro.cpp:12:59: error: non-type template argument is not a constant expression
using Int32MaxError = std::integral_constant<std::int32_t,std::numeric_limits<std::int32_t>::max() + 1>;
                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
overflow_erro.cpp:12:100: note: value 2147483648 is outside the range of representable values of type 'int'
using Int32MaxError = std::integral_constant<std::int32_t,std::numeric_limits<std::int32_t>::max() + 1>;
                                                                                                   ^
overflow_erro.cpp:13:59: error: non-type template argument is not a constant expression
using Int64MaxError = std::integral_constant<std::int64_t,std::numeric_limits<std::int64_t>::max() + 1>;
                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
overflow_erro.cpp:13:100: note: value 9223372036854775808 is outside the range of representable values of type 'long'
using Int64MaxError = std::integral_constant<std::int64_t,std::numeric_limits<std::int64_t>::max() + 1>;
                                                                                                   ^
overflow_erro.cpp:16:59: error: non-type template argument evaluates to -1, which cannot be narrowed to type 'unsigned char' [-Wc++11-narrowing]
using UInt8MinError = std::integral_constant<std::uint8_t,-1>;
                                                          ^
overflow_erro.cpp:17:61: error: non-type template argument evaluates to -1, which cannot be narrowed to type 'unsigned short' [-Wc++11-narrowing]
using UInt16MinError = std::integral_constant<std::uint16_t,-1>;
                                                            ^
overflow_erro.cpp:18:61: error: non-type template argument evaluates to -1, which cannot be narrowed to type 'unsigned int' [-Wc++11-narrowing]
using UInt32MinError = std::integral_constant<std::uint32_t,-1>;
                                                            ^
overflow_erro.cpp:19:61: error: non-type template argument evaluates to -1, which cannot be narrowed to type 'unsigned long' [-Wc++11-narrowing]
using UInt64MinError = std::integral_constant<std::uint64_t,-1>;
                                                            ^
overflow_erro.cpp:22:59: error: non-type template argument evaluates to 256, which cannot be narrowed to type 'unsigned char' [-Wc++11-narrowing]
using UInt8MaxError = std::integral_constant<std::uint8_t,std::numeric_limits<std::uint8_t>::max() + 1>;
                                                          ^
overflow_erro.cpp:23:61: error: non-type template argument evaluates to 65536, which cannot be narrowed to type 'unsigned short' [-Wc++11-narrowing]
using UInt16MaxError = std::integral_constant<std::uint16_t,std::numeric_limits<std::uint16_t>::max() + 1>;
                                                            ^
14 errors generated.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: overflow_erro.cpp
Type: text/x-c++src
Size: 1579 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-dev/attachments/20200127/6f89aaa1/attachment.cpp>


More information about the libcxx-dev mailing list