public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105774] New: Bogus overflow in constant expression
@ 2022-05-30 14:31 jeff at jgarrett dot org
  2022-05-30 14:39 ` [Bug c++/105774] " klaus.doldinger64 at googlemail dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: jeff at jgarrett dot org @ 2022-05-30 14:31 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105774
           Summary: Bogus overflow in constant expression
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeff at jgarrett dot org
  Target Milestone: ---

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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
@ 2022-05-30 14:39 ` klaus.doldinger64 at googlemail dot com
  2022-06-01 11:40 ` rguenth at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: klaus.doldinger64 at googlemail dot com @ 2022-05-30 14:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Wilhelm M <klaus.doldinger64 at googlemail dot com> ---
To make it more clear make the type of x *signed char`.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
  2022-05-30 14:39 ` [Bug c++/105774] " klaus.doldinger64 at googlemail dot com
@ 2022-06-01 11:40 ` rguenth at gcc dot gnu.org
  2022-10-22 20:55 ` [Bug c++/105774] Bogus overflow in constant expression with signed char++ pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-06-01 11:40 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |rejects-valid
   Last reconfirmed|                            |2022-06-01
     Ever confirmed|0                           |1

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
  2022-05-30 14:39 ` [Bug c++/105774] " klaus.doldinger64 at googlemail dot com
  2022-06-01 11:40 ` rguenth at gcc dot gnu.org
@ 2022-10-22 20:55 ` pinskia at gcc dot gnu.org
  2022-10-22 20:56 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-22 20:55 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Bogus overflow in constant  |Bogus overflow in constant
                   |expression with char++      |expression with signed
                   |                            |char++
      Known to fail|                            |5.1.0

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
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();
}

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
                   ` (2 preceding siblings ...)
  2022-10-22 20:55 ` [Bug c++/105774] Bogus overflow in constant expression with signed char++ pinskia at gcc dot gnu.org
@ 2022-10-22 20:56 ` pinskia at gcc dot gnu.org
  2022-10-23 16:46 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-22 20:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
And one for short:
constexpr signed short f(void){
    signed short x = 0x7fff;
    return ++x;
}
int main() {
    constexpr auto _ = f();
}

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
                   ` (3 preceding siblings ...)
  2022-10-22 20:56 ` pinskia at gcc dot gnu.org
@ 2022-10-23 16:46 ` jakub at gcc dot gnu.org
  2022-10-24 14:26 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-10-23 16:46 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 53763
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53763&action=edit
gcc13-pr105774.patch

Untested fix.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
                   ` (4 preceding siblings ...)
  2022-10-23 16:46 ` jakub at gcc dot gnu.org
@ 2022-10-24 14:26 ` cvs-commit at gcc dot gnu.org
  2022-10-25  9:05 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-24 14:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
                   ` (5 preceding siblings ...)
  2022-10-24 14:26 ` cvs-commit at gcc dot gnu.org
@ 2022-10-25  9:05 ` jakub at gcc dot gnu.org
  2022-11-03  0:23 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-10-25  9:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed on the trunk so far.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
                   ` (6 preceding siblings ...)
  2022-10-25  9:05 ` jakub at gcc dot gnu.org
@ 2022-11-03  0:23 ` cvs-commit at gcc dot gnu.org
  2022-11-04  8:31 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-11-03  0:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
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)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
                   ` (7 preceding siblings ...)
  2022-11-03  0:23 ` cvs-commit at gcc dot gnu.org
@ 2022-11-04  8:31 ` cvs-commit at gcc dot gnu.org
  2023-05-03 15:19 ` cvs-commit at gcc dot gnu.org
  2023-05-04  7:17 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-11-04  8:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
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)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
                   ` (8 preceding siblings ...)
  2022-11-04  8:31 ` cvs-commit at gcc dot gnu.org
@ 2023-05-03 15:19 ` cvs-commit at gcc dot gnu.org
  2023-05-04  7:17 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-05-03 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
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)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/105774] Bogus overflow in constant expression with signed char++
  2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
                   ` (9 preceding siblings ...)
  2023-05-03 15:19 ` cvs-commit at gcc dot gnu.org
@ 2023-05-04  7:17 ` jakub at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-04  7:17 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #11 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 10.5 too.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-05-04  7:17 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-30 14:31 [Bug c++/105774] New: Bogus overflow in constant expression jeff at jgarrett dot org
2022-05-30 14:39 ` [Bug c++/105774] " klaus.doldinger64 at googlemail dot com
2022-06-01 11:40 ` rguenth at gcc dot gnu.org
2022-10-22 20:55 ` [Bug c++/105774] Bogus overflow in constant expression with signed char++ pinskia at gcc dot gnu.org
2022-10-22 20:56 ` pinskia at gcc dot gnu.org
2022-10-23 16:46 ` jakub at gcc dot gnu.org
2022-10-24 14:26 ` cvs-commit at gcc dot gnu.org
2022-10-25  9:05 ` jakub at gcc dot gnu.org
2022-11-03  0:23 ` cvs-commit at gcc dot gnu.org
2022-11-04  8:31 ` cvs-commit at gcc dot gnu.org
2023-05-03 15:19 ` cvs-commit at gcc dot gnu.org
2023-05-04  7:17 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).