[Bug c++/85125] constant expression with const_cast UB does not emit error

2019-09-01 Thread john at mcfarlane dot name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125

--- Comment #7 from John McFarlane  ---
Confirmed. Thank you!

On Mon, 19 Aug 2019 at 15:02, mpolacek at gcc dot gnu.org <
gcc-bugzi...@gcc.gnu.org> wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125
>
> Marek Polacek  changed:
>
>What|Removed |Added
>
> 
>  Status|ASSIGNED|RESOLVED
>  Resolution|--- |FIXED
>
> --- Comment #6 from Marek Polacek  ---
> Done for 10.1 via:
>
> Author: mpolacek
> Date: Mon Aug 19 13:59:13 2019
> New Revision: 274671
>
> URL: https://gcc.gnu.org/viewcvs?rev=274671=gcc=rev
> Log:
> PR c++/91264 - detect modifying const objects in constexpr.
> * constexpr.c (modifying_const_object_error): New function.
> (cxx_eval_call_expression): Set TREE_READONLY on a CONSTRUCTOR of
> a const-qualified object after it's been fully constructed.
> (modifying_const_object_p): New function.
> (cxx_eval_store_expression): Detect modifying a const object
> during constant expression evaluation.
> (cxx_eval_increment_expression): Use a better location when
> building
> up the store.
> (cxx_eval_constant_expression) : Mark a constant
> object's constructor TREE_READONLY.
>
> * g++.dg/cpp1y/constexpr-tracking-const1.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const2.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const3.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const4.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const5.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const6.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const7.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const8.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const9.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const10.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const11.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const12.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const13.C: New test.
> * g++.dg/cpp1y/constexpr-tracking-const14.C: New test.
>
> Added:
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const1.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const10.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const11.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const12.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const13.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const2.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const5.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const6.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const8.C
> trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const9.C
> Modified:
> trunk/gcc/cp/ChangeLog
> trunk/gcc/cp/constexpr.c
> trunk/gcc/testsuite/ChangeLog
>
> --
> You are receiving this mail because:
> You are on the CC list for the bug.
> You reported the bug.

[Bug c++/85125] constant expression with const_cast UB does not emit error

2019-08-19 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125

Marek Polacek  changed:

   What|Removed |Added

 Status|ASSIGNED|RESOLVED
 Resolution|--- |FIXED

--- Comment #6 from Marek Polacek  ---
Done for 10.1 via:

Author: mpolacek
Date: Mon Aug 19 13:59:13 2019
New Revision: 274671

URL: https://gcc.gnu.org/viewcvs?rev=274671=gcc=rev
Log:
PR c++/91264 - detect modifying const objects in constexpr.
* constexpr.c (modifying_const_object_error): New function.
(cxx_eval_call_expression): Set TREE_READONLY on a CONSTRUCTOR of
a const-qualified object after it's been fully constructed.
(modifying_const_object_p): New function.
(cxx_eval_store_expression): Detect modifying a const object
during constant expression evaluation.
(cxx_eval_increment_expression): Use a better location when building
up the store.
(cxx_eval_constant_expression) : Mark a constant
object's constructor TREE_READONLY.

* g++.dg/cpp1y/constexpr-tracking-const1.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const2.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const3.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const4.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const5.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const6.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const7.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const8.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const9.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const10.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const11.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const12.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const13.C: New test.
* g++.dg/cpp1y/constexpr-tracking-const14.C: New test.

Added:
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const1.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const10.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const11.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const12.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const13.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const2.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const5.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const6.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const8.C
trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const9.C
Modified:
trunk/gcc/cp/ChangeLog
trunk/gcc/cp/constexpr.c
trunk/gcc/testsuite/ChangeLog

[Bug c++/85125] constant expression with const_cast UB does not emit error

2019-08-07 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125

Marek Polacek  changed:

   What|Removed |Added

   Keywords||patch
 Status|NEW |ASSIGNED
 CC||mpolacek at gcc dot gnu.org
   Assignee|unassigned at gcc dot gnu.org  |mpolacek at gcc dot 
gnu.org

--- Comment #5 from Marek Polacek  ---
Recently I posted a patch for this:
https://gcc.gnu.org/ml/gcc-patches/2019-07/msg01914.html

With that, the original testcase now gives:

85125.C:6:22: error: non-constant condition for static assertion
6 | static_assert(f()==77, "");
  |   ~~~^~~~
85125.C:6:20:   in ‘constexpr’ expansion of ‘f()’
85125.C:3:27: error: modifying a const object ‘(int&)(& i)’ is not allowed in a
constant expression
3 |   const_cast(i) = 88;
  |   ^~~~
85125.C:2:17: note: originally declared ‘const’ here
2 |   int const i = 77;
  | ^

and the one from Comment 3 gives:

85125-2.C:12:16: error: non-constant condition for static assertion
   12 | static_assert(f());
  |   ~^~
85125-2.C:12:16:   in ‘constexpr’ expansion of ‘f()’
85125-2.C:8:12: error: modifying a const object ‘*(int*)s.S::ptr’ is not
allowed in a constant expression
8 | *s.ptr = 2;
  | ~~~^~~
85125-2.C:7:16: note: originally declared ‘const’ here
7 | auto const s = S{};
  |^

[Bug c++/85125] constant expression with const_cast UB does not emit error

2019-06-21 Thread david at doublewise dot net
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125

--- Comment #4 from David Stone  ---
*** Bug 86623 has been marked as a duplicate of this bug. ***

[Bug c++/85125] constant expression with const_cast UB does not emit error

2019-06-21 Thread david at doublewise dot net
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125

David Stone  changed:

   What|Removed |Added

 CC||david at doublewise dot net

--- Comment #3 from David Stone  ---
Here is a reproduction that does not require const_cast:


struct S {
int a = 1;
int * ptr = 
};

constexpr bool f() {
auto const s = S{};
*s.ptr = 2;
return s.a == 2;
}

static_assert(f());


See it live: https://godbolt.org/z/30O7_j

[Bug c++/85125] constant expression with const_cast UB does not emit error

2018-04-15 Thread john at mcfarlane dot name
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125

--- Comment #2 from John McFarlane  ---
Here are two more:

constexpr int dubious_increment(int i) { return ++i + ++i; }
static_assert(dubious_increment(0) == 3);

constexpr int dubious_increment(int i) { return i; }
static_assert(dubious_increment(0) == 2);

The former might be a special case because it produces warnings when compiled
at a high-enough warning level (-Wall) but continues to compile nevertheless.
(Clan, warns at any level but also happily compiles.) The second just compiles
without warning no matter what.

Would these be classes as the same issue or should I submit a separate report?
Also, I'm not trying very hard to find these so I suspect there are more.

[Bug c++/85125] constant expression with const_cast UB does not emit error

2018-04-03 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125

Jonathan Wakely  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2018-04-03
 Ever confirmed|0   |1

--- Comment #1 from Jonathan Wakely  ---
N.B. GCC 5.x releases are no longer supported, but this bug is still present in
current releases.