From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id 83CBB385800C; Fri, 16 Feb 2024 17:30:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 83CBB385800C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708104608; bh=J1+0rNIkQpQyCBuIvrYDjYDAbP6ddoAwdmptpKVmIcI=; h=From:To:Subject:Date:From; b=c/tEZu+G9b9CxYlOHQ4nHeAogAdfi79k+DqxtdWJLKJ5RleGAtUbyARlIo37acVaT 19umzMz+8KsQLWp7aD7skGwIIn6+LBDV42JjO7A1xFBIP/JyiimyOP93LQonD64Fkk 5fvP96dT+xB+uBVp1+Nz+r2d5ftnrB+CFtseAPfM= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-8336] c++: ICE with reinterpret_cast and switch [PR113545] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: ebe00c9d3a0436dec5c354a62d98e444d763ff95 X-Git-Newrev: e501a279fb4298c9b23637d573287e059b3b06c8 Message-Id: <20240216173008.83CBB385800C@sourceware.org> Date: Fri, 16 Feb 2024 17:30:08 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e501a279fb4298c9b23637d573287e059b3b06c8 commit r13-8336-ge501a279fb4298c9b23637d573287e059b3b06c8 Author: Marek Polacek Date: Fri Feb 16 12:25:26 2024 -0500 c++: ICE with reinterpret_cast and switch [PR113545] Jason, this is the patch you proposed for PR113545. It looks very safe so I'm posting it here so that it's not forgotten. PR c++/113545 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_switch_expr): If the condition doesn't reduce to an INTEGER_CST, consider it non-constant. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-reinterpret3.C: New test. * g++.dg/cpp1y/constexpr-reinterpret4.C: New test. (cherry picked from commit 39d989022dd0eacf1a7b95b7b20621acbe717d70) Diff: --- gcc/cp/constexpr.cc | 10 ++++ .../g++.dg/cpp1y/constexpr-reinterpret3.C | 54 ++++++++++++++++++++++ .../g++.dg/cpp1y/constexpr-reinterpret4.C | 54 ++++++++++++++++++++++ 3 files changed, 118 insertions(+) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index ab129c087555..a3c21e88e7ba 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -6722,6 +6722,16 @@ cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, non_constant_p, overflow_p); VERIFY_CONSTANT (cond); + if (TREE_CODE (cond) != INTEGER_CST) + { + /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable + switch condition even if it's constant enough for other things + (c++/113545). */ + gcc_checking_assert (ctx->quiet); + *non_constant_p = true; + return t; + } + *jump_target = cond; tree body diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C new file mode 100644 index 000000000000..adc0b418df91 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C @@ -0,0 +1,54 @@ +// PR c++/113545 +// { dg-do run { target c++14 } } + +char foo; + +// This one caught a call to gcc_unreachable in +// cp/constexpr.cc:label_matches, when passed a convert_expr from the +// cast in the call. +constexpr unsigned char swbar(__UINTPTR_TYPE__ baz) +{ + switch (baz) + { + case 13: + return 11; + case 14: + return 78; + case 2048: + return 13; + default: + return 42; + } +} + +// For reference, the equivalent* if-statements. +constexpr unsigned char ifbar(__UINTPTR_TYPE__ baz) +{ + if (baz == 13) + return 11; + else if (baz == 14) + return 78; + else if (baz == 2048) + return 13; + else + return 42; +} + +__attribute__ ((__noipa__)) +void xyzzy(int x) +{ + if (x != 42) + __builtin_abort (); +} + +int main() +{ + unsigned const char c = swbar(reinterpret_cast<__UINTPTR_TYPE__>(&foo)); + xyzzy(c); + unsigned const char d = ifbar(reinterpret_cast<__UINTPTR_TYPE__>(&foo)); + xyzzy(d); + unsigned const char e = swbar((__UINTPTR_TYPE__) &foo); + xyzzy(e); + unsigned const char f = ifbar((__UINTPTR_TYPE__) &foo); + xyzzy(f); +} diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret4.C new file mode 100644 index 000000000000..9aaa6e463bc6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret4.C @@ -0,0 +1,54 @@ +// PR c++/113545 +// { dg-do compile { target c++14 } } + +char foo; + +// This one caught a call to gcc_unreachable in +// cp/constexpr.cc:label_matches, when passed a convert_expr from the +// cast in the call. +constexpr unsigned char swbar(__UINTPTR_TYPE__ baz) +{ + switch (baz) + { + case 13: + return 11; + case 14: + return 78; + case 2048: + return 13; + default: + return 42; + } +} + +// For reference, the equivalent* if-statements. +constexpr unsigned char ifbar(__UINTPTR_TYPE__ baz) +{ + if (baz == 13) + return 11; + else if (baz == 14) + return 78; + else if (baz == 2048) + return 13; + else + return 42; +} + +__attribute__ ((__noipa__)) +void xyzzy(int x) +{ + if (x != 42) + __builtin_abort (); +} + +int main() +{ + unsigned constexpr char c = swbar(reinterpret_cast<__UINTPTR_TYPE__>(&foo)); // { dg-error "conversion from pointer type" } + xyzzy(c); + unsigned constexpr char d = ifbar(reinterpret_cast<__UINTPTR_TYPE__>(&foo)); // { dg-error "conversion from pointer type" } + xyzzy(d); + unsigned constexpr char e = swbar((__UINTPTR_TYPE__) &foo); // { dg-error "conversion from pointer type" } + xyzzy(e); + unsigned constexpr char f = ifbar((__UINTPTR_TYPE__) &foo); // { dg-error "conversion from pointer type" } + xyzzy(f); +}