Bug 102174 - Unused result of undefined behavior arithmetic is accepted during constant evaluation
Summary: Unused result of undefined behavior arithmetic is accepted during constant ev...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2021-09-02 11:07 UTC by Johel Ernesto Guerrero Peña
Modified: 2021-12-04 13:03 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-12-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Johel Ernesto Guerrero Peña 2021-09-02 11:07:54 UTC
See https://godbolt.org/z/d38ozc91x.
```C++
#include <cstdint>
constexpr bool f() {
  1 / 0;
  1.0 / 0;
  1 % 0;
  INT32_MIN / INT32_C(-1);
  INT32_MIN * INT32_C(-1);
  INT32_MAX + INT32_C(1);
  INT16_C(INT32_MAX + INT32_C(1));
  return true;
}
constexpr bool b{f()};
```
```
<source>: In function 'constexpr bool f()':
<source>:3:5: warning: division by zero [-Wdiv-by-zero]
    3 |   1 / 0;
      |   ~~^~~
<source>:3:5: warning: statement has no effect [-Wunused-value]
<source>:4:7: warning: division by zero [-Wdiv-by-zero]
    4 |   1.0 / 0;
      |   ~~~~^~~
<source>:4:7: warning: statement has no effect [-Wunused-value]
<source>:5:5: warning: division by zero [-Wdiv-by-zero]
    5 |   1 % 0;
      |   ~~^~~
<source>:5:5: warning: statement has no effect [-Wunused-value]
<source>:6:13: warning: integer overflow in expression of type 'int' results in '-2147483648' [-Woverflow]
    6 |   INT32_MIN / INT32_C(-1);
      |             ^
<source>:6:13: warning: statement has no effect [-Wunused-value]
<source>:7:13: warning: integer overflow in expression of type 'int' results in '-2147483648' [-Woverflow]
    7 |   INT32_MIN * INT32_C(-1);
      |             ^
<source>:7:13: warning: statement has no effect [-Wunused-value]
<source>:8:13: warning: integer overflow in expression of type 'int' results in '-2147483648' [-Woverflow]
    8 |   INT32_MAX + INT32_C(1);
      |             ^
<source>:8:13: warning: statement has no effect [-Wunused-value]
In file included from /opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/include/stdint.h:9,
                 from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/cstdint:41,
                 from <source>:1:
<source>:9:21: warning: integer overflow in expression of type 'int' results in '-2147483648' [-Woverflow]
    9 |   INT16_C(INT32_MAX + INT32_C(1));
      |                     ^
<source>:9:21: warning: statement has no effect [-Wunused-value]
Compiler returned: 0
```
Comment 1 Andrew Pinski 2021-12-04 13:03:40 UTC
Confirmed.