Bug 105774 - Bogus overflow in constant expression with signed char++
Summary: Bogus overflow in constant expression with signed char++
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.1.1
: P3 normal
Target Milestone: ---
Assignee: Jakub Jelinek
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2022-05-30 14:31 UTC by Jeff Garrett
Modified: 2023-05-04 07:17 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 5.1.0
Last reconfirmed: 2022-06-01 00:00:00


Attachments
gcc13-pr105774.patch (699 bytes, patch)
2022-10-23 16:46 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jeff Garrett 2022-05-30 14:31:31 UTC
The following is diagnosed as ill-formed by GCC but not by Clang:

    int main() {
      constexpr auto _ = [] {
        char x = 127;
        return ++x;
      }();
    }

<source>:5:5: error: overflow in constant expression [-fpermissive]

On godbolt https://godbolt.org/z/91oeGsEbh
Originally from https://stackoverflow.com/questions/72425404/still-unsure-about-signed-integer-overflow-in-c

I believe that this is well-formed. [expr.pre.incr]/1 says x++ is equivalent to x+=1. [expr.ass]/6 says that x+=1 is equivalent to x=x+1 except that x is only evaluated once. That expression x=x+1 avoids overflow through integer promotion.

The same code with x+=1 instead of ++x is allowed by GCC.
Comment 1 Wilhelm M 2022-05-30 14:39:26 UTC
To make it more clear make the type of x *signed char`.
Comment 2 Richard Biener 2022-06-01 11:40:40 UTC
Confirmed.
Comment 3 Andrew Pinski 2022-10-22 20:55:56 UTC
Here is a C++14 testcase (lambdas were not constexpr in C++14) which shows the issue has been there since GCC 5 (which didn't have C++17 support):
constexpr signed char f(void){
    signed char x = 127;
    return ++x;
}
int main() {
    constexpr auto _ = f();
}
Comment 4 Andrew Pinski 2022-10-22 20:56:35 UTC
And one for short:
constexpr signed short f(void){
    signed short x = 0x7fff;
    return ++x;
}
int main() {
    constexpr auto _ = f();
}
Comment 5 Jakub Jelinek 2022-10-23 16:46:22 UTC
Created attachment 53763 [details]
gcc13-pr105774.patch

Untested fix.
Comment 6 GCC Commits 2022-10-24 14:26:15 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:da8c362c4c18cff2f2dfd5c4706bdda7576899a4

commit r13-3458-gda8c362c4c18cff2f2dfd5c4706bdda7576899a4
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Oct 24 16:25:29 2022 +0200

    c++: Fix up constexpr handling of char/signed char/short pre/post inc/decrement [PR105774]
    
    signed char, char or short int pre/post inc/decrement are represented by
    normal {PRE,POST}_{INC,DEC}REMENT_EXPRs in the FE and only gimplification
    ensures that the {PLUS,MINUS}_EXPR is done in unsigned version of those
    types:
        case PREINCREMENT_EXPR:
        case PREDECREMENT_EXPR:
        case POSTINCREMENT_EXPR:
        case POSTDECREMENT_EXPR:
          {
            tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
            if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
              {
                if (!TYPE_OVERFLOW_WRAPS (type))
                  type = unsigned_type_for (type);
                return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
              }
            break;
          }
    This means during constant evaluation we need to do it similarly (either
    using unsigned_type_for or using widening to integer_type_node).
    The following patch does the latter.
    
    2022-10-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/105774
            * constexpr.cc (cxx_eval_increment_expression): For signed types
            that promote to int, evaluate PLUS_EXPR or MINUS_EXPR in int type.
    
            * g++.dg/cpp1y/constexpr-105774.C: New test.
Comment 7 Jakub Jelinek 2022-10-25 09:05:29 UTC
Fixed on the trunk so far.
Comment 8 GCC Commits 2022-11-03 00:23:24 UTC
The releases/gcc-12 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:20ef7d7c578dab0585d70fbea571a74e8e8d4b47

commit r12-8888-g20ef7d7c578dab0585d70fbea571a74e8e8d4b47
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Oct 24 16:25:29 2022 +0200

    c++: Fix up constexpr handling of char/signed char/short pre/post inc/decrement [PR105774]
    
    signed char, char or short int pre/post inc/decrement are represented by
    normal {PRE,POST}_{INC,DEC}REMENT_EXPRs in the FE and only gimplification
    ensures that the {PLUS,MINUS}_EXPR is done in unsigned version of those
    types:
        case PREINCREMENT_EXPR:
        case PREDECREMENT_EXPR:
        case POSTINCREMENT_EXPR:
        case POSTDECREMENT_EXPR:
          {
            tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
            if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
              {
                if (!TYPE_OVERFLOW_WRAPS (type))
                  type = unsigned_type_for (type);
                return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
              }
            break;
          }
    This means during constant evaluation we need to do it similarly (either
    using unsigned_type_for or using widening to integer_type_node).
    The following patch does the latter.
    
    2022-10-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/105774
            * constexpr.cc (cxx_eval_increment_expression): For signed types
            that promote to int, evaluate PLUS_EXPR or MINUS_EXPR in int type.
    
            * g++.dg/cpp1y/constexpr-105774.C: New test.
    
    (cherry picked from commit da8c362c4c18cff2f2dfd5c4706bdda7576899a4)
Comment 9 GCC Commits 2022-11-04 08:31:28 UTC
The releases/gcc-11 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:11a37955860f8573570aaf8d9fb0b6e02a3d4d5a

commit r11-10362-g11a37955860f8573570aaf8d9fb0b6e02a3d4d5a
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Oct 24 16:25:29 2022 +0200

    c++: Fix up constexpr handling of char/signed char/short pre/post inc/decrement [PR105774]
    
    signed char, char or short int pre/post inc/decrement are represented by
    normal {PRE,POST}_{INC,DEC}REMENT_EXPRs in the FE and only gimplification
    ensures that the {PLUS,MINUS}_EXPR is done in unsigned version of those
    types:
        case PREINCREMENT_EXPR:
        case PREDECREMENT_EXPR:
        case POSTINCREMENT_EXPR:
        case POSTDECREMENT_EXPR:
          {
            tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
            if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
              {
                if (!TYPE_OVERFLOW_WRAPS (type))
                  type = unsigned_type_for (type);
                return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
              }
            break;
          }
    This means during constant evaluation we need to do it similarly (either
    using unsigned_type_for or using widening to integer_type_node).
    The following patch does the latter.
    
    2022-10-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/105774
            * constexpr.c (cxx_eval_increment_expression): For signed types
            that promote to int, evaluate PLUS_EXPR or MINUS_EXPR in int type.
    
            * g++.dg/cpp1y/constexpr-105774.C: New test.
    
    (cherry picked from commit da8c362c4c18cff2f2dfd5c4706bdda7576899a4)
Comment 10 GCC Commits 2023-05-03 15:19:19 UTC
The releases/gcc-10 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:11bf3345c74139c05c405d3e5bc73ee8d9e7d6a6

commit r10-11340-g11bf3345c74139c05c405d3e5bc73ee8d9e7d6a6
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Oct 24 16:25:29 2022 +0200

    c++: Fix up constexpr handling of char/signed char/short pre/post inc/decrement [PR105774]
    
    signed char, char or short int pre/post inc/decrement are represented by
    normal {PRE,POST}_{INC,DEC}REMENT_EXPRs in the FE and only gimplification
    ensures that the {PLUS,MINUS}_EXPR is done in unsigned version of those
    types:
        case PREINCREMENT_EXPR:
        case PREDECREMENT_EXPR:
        case POSTINCREMENT_EXPR:
        case POSTDECREMENT_EXPR:
          {
            tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
            if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
              {
                if (!TYPE_OVERFLOW_WRAPS (type))
                  type = unsigned_type_for (type);
                return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
              }
            break;
          }
    This means during constant evaluation we need to do it similarly (either
    using unsigned_type_for or using widening to integer_type_node).
    The following patch does the latter.
    
    2022-10-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/105774
            * constexpr.c (cxx_eval_increment_expression): For signed types
            that promote to int, evaluate PLUS_EXPR or MINUS_EXPR in int type.
    
            * g++.dg/cpp1y/constexpr-105774.C: New test.
    
    (cherry picked from commit da8c362c4c18cff2f2dfd5c4706bdda7576899a4)
Comment 11 Jakub Jelinek 2023-05-04 07:17:43 UTC
Fixed for 10.5 too.