https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97798

--- Comment #21 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jonathan Wakely
<r...@gcc.gnu.org>:

https://gcc.gnu.org/g:62c2d527307d8adce31f5c9ca6e19e15b2866b83

commit r9-9045-g62c2d527307d8adce31f5c9ca6e19e15b2866b83
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Thu Nov 12 10:29:21 2020 +0000

    libstdc++: Fix __numeric_traits_integer<__int20> [PR 97798]

    The expression used to calculate the maximum value for an integer type
    assumes that the number of bits in the value representation is always
    sizeof(T) * CHAR_BIT. This is not true for the __int20 type on msp430,
    which has only 20 bits in the value representation but 32 bits in the
    object representation. This causes an integer overflow in a constant
    expression, which is ill-formed.

    This problem was already solved by DJ for std::numeric_limits<__int20>
    by generalizing the helper macros to use a specified number of bits
    instead of assuming sizeof(T) * CHAR_BIT. Then the INT_N_n types can
    specify the number of bits using the __GLIBCXX_BITSIZE_INT_N_n macros
    that the compiler defines.

    I'm using a slightly different approach here. I've replaced the helper
    macros entirely, and just expanded the calculations in the initializers
    for the static data members. By reordering the data members we can reuse
    __is_signed and __digits in the other initializers. This removes the
    repetition of expanding __glibcxx_signed(T) and __glibcxx_digits(T)
    multiple times in each initializer.

    The __is_integer_nonstrict trait now defines a new constant, __width,
    which is sizeof(T) * CHAR_BIT by default (defined as an enumerator so
    that no storage is needed for a static data member). By specializing
    __is_integer_nonstrict for the INT_N types that have padding bits, we
    can provide the correct width via the __GLIBCXX_BITSIZE_INT_N_n macros.

    The backport for the gcc-9 branch adds the __is_integer_nonstrict trait,
    which was not previously present on the branch at all.

    libstdc++-v3/ChangeLog:

            PR libstdc++/97798
            * include/ext/numeric_traits.h (__glibcxx_signed)
            (__glibcxx_digits, __glibcxx_min, __glibcxx_max): Remove
            macros.
            (__is_integer_nonstrict::__width): Define new constant.
            (__numeric_traits_integer): Define constants in terms of each
            other and __is_integer_nonstrict::__width, rather than the
            removed macros.
            (_GLIBCXX_INT_N_TRAITS): Macro to define explicit
            specializations for non-standard integer types.

    (cherry picked from commit 99f22a5ed91c7e4306b727f61c01484faf104115)

Reply via email to