From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by sourceware.org (Postfix) with ESMTPS id 974253858D28; Tue, 11 Oct 2022 20:23:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 974253858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1665519781; bh=8fz2fFHpoE/0TCuLHyJMmPfTENv9tN5RiFAAiC/ix1I=; h=X-UI-Sender-Class:From:To:Subject:Date; b=ZO+IGY+cg+JCk4DTtPLzRozmCW6t4d/xDclPDxVT1kABOA3iaqsoJYs7NtSiswTNF 51q6YBgQqVefvhvTF425sT2S1ciyDvHyGo3s+pLFot1WeHH20hqTgJbvTuPvItEIAk +FZx77Thtc0yqXlt+hQnrtkrOMvQwb1/PEKR5Ols= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [93.207.82.174] ([93.207.82.174]) by web-mail.gmx.net (3c-app-gmx-bs49.server.lan [172.19.170.102]) (via HTTP); Tue, 11 Oct 2022 22:23:01 +0200 MIME-Version: 1.0 Message-ID: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] Fortran: check types of operands of arithmetic binary operations [PR107217] Content-Type: multipart/mixed; boundary=sgnirk-7eb0e93a-d6ba-4eb8-a128-4cbd1899d789 Date: Tue, 11 Oct 2022 22:23:01 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:37mOzRaTTWgRNwXnTS1OTU8i5vOFNfLB3TLIhVHTOUe8b0sHsQDie7KOIJOLbdSoe6Vrf 5FHZaB0GOojyG3njWYRahsbov4tj5IW3MxI60sGBoExnooqEgXOY1uawAfdhWg2uFLyA72gKZWaJ rVLlyC9c8OSIxaR3KUi8lpdEvzG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 YsASNmVz4HfuRE+9V3KJGCSuUAgOovEOpX2srSMWCfbn4+yLqD295JToLtj7l7OAKfZ6qDTHl 1qqWeMu8s9LJg+LXe+xz/XlVZWNMOTQ84CtmAtuUdwrK6vq9mda0cYKj7axiY/wTycw8D5dwm B2ugq/ZFwJrZJ3x0Upm7ZJBO4V7NuDiWSWFe7V8KsAbLm2UwPwQKWjDgEzSH6mf23/L6qKTnd +mitnAOzSFAwGTTxqADpOJSBJJvKzvU0dNVFj4NJ6WWvi++Q3jJ0fL+Bmk4gT9KKnFQgDHi74 kXkaal8cMYi/cENj37VkpPV2VggARfAT+TKZPpEy0aKpqulEYErU93t0r+bwRhvcBVKn/zIWE ImhzsNqrdos6U1qbek2mcFQnljRFBBhD2IL6iflhxMyUt89oItlV/o6/piAC/E9c38DwAE87D ZDYe3EpJ X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --sgnirk-7eb0e93a-d6ba-4eb8-a128-4cbd1899d789 Content-Type: text/plain; charset=UTF-8 Dear all, we need to check that the operands of arithmetic binary operations are consistent and of numeric type. The PR reported an issue for multiplication ("*"), but we better extend this to the other binary operations. I chose the following solution: - consistent types for +,-,*,/, keeping an internal error if any unhandled type shows up, - numeric types for ** Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald --sgnirk-7eb0e93a-d6ba-4eb8-a128-4cbd1899d789 Content-Type: text/x-patch Content-Disposition: attachment; filename=pr107217.diff Content-Transfer-Encoding: quoted-printable =46rom a95f251504bcb8ba28b7db1d2b7990631c761e9c Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Tue, 11 Oct 2022 22:08:48 +0200 Subject: [PATCH] Fortran: check types of operands of arithmetic binary operations [PR107217] gcc/fortran/ChangeLog: PR fortran/107217 * arith.cc (gfc_arith_plus): Compare consistency of types of operands. (gfc_arith_minus): Likewise. (gfc_arith_times): Likewise. (gfc_arith_divide): Likewise. (arith_power): Check that both operands are of numeric type. gcc/testsuite/ChangeLog: PR fortran/107217 * gfortran.dg/pr107217.f90: New test. =2D-- gcc/fortran/arith.cc | 15 +++++++++++++++ gcc/testsuite/gfortran.dg/pr107217.f90 | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/pr107217.f90 diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc index 9e079e42995..14ba931e37f 100644 =2D-- a/gcc/fortran/arith.cc +++ b/gcc/fortran/arith.cc @@ -624,6 +624,9 @@ gfc_arith_plus (gfc_expr *op1, gfc_expr *op2, gfc_expr= **resultp) gfc_expr *result; arith rc; + if (op1->ts.type !=3D op2->ts.type) + return ARITH_INVALID_TYPE; + result =3D gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->whe= re); switch (op1->ts.type) @@ -658,6 +661,9 @@ gfc_arith_minus (gfc_expr *op1, gfc_expr *op2, gfc_exp= r **resultp) gfc_expr *result; arith rc; + if (op1->ts.type !=3D op2->ts.type) + return ARITH_INVALID_TYPE; + result =3D gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->whe= re); switch (op1->ts.type) @@ -692,6 +698,9 @@ gfc_arith_times (gfc_expr *op1, gfc_expr *op2, gfc_exp= r **resultp) gfc_expr *result; arith rc; + if (op1->ts.type !=3D op2->ts.type) + return ARITH_INVALID_TYPE; + result =3D gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->whe= re); switch (op1->ts.type) @@ -727,6 +736,9 @@ gfc_arith_divide (gfc_expr *op1, gfc_expr *op2, gfc_ex= pr **resultp) gfc_expr *result; arith rc; + if (op1->ts.type !=3D op2->ts.type) + return ARITH_INVALID_TYPE; + rc =3D ARITH_OK; result =3D gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->whe= re); @@ -815,6 +827,9 @@ arith_power (gfc_expr *op1, gfc_expr *op2, gfc_expr **= resultp) gfc_expr *result; arith rc; + if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts)) + return ARITH_INVALID_TYPE; + rc =3D ARITH_OK; result =3D gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->whe= re); diff --git a/gcc/testsuite/gfortran.dg/pr107217.f90 b/gcc/testsuite/gfortr= an.dg/pr107217.f90 new file mode 100644 index 00000000000..9c8492e64f0 =2D-- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr107217.f90 @@ -0,0 +1,18 @@ +! { dg-do compile } +! PR fortran/107217 - ICE in gfc_arith_times +! Contributed by G.Steinmetz + +program p + print *, [real :: (['1'])] * 2 ! { dg-error "Cannot convert" } + print *, 2 * [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] + 2 ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] - 2 ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] / 2 ! { dg-error "Cannot convert" } + print *, 1 / [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] ** 2 ! { dg-error "Cannot convert" } + print *, 2 ** [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, 2.0 ** [real :: (.true.)] ! { dg-error "Cannot convert" } + print *, [real :: (.true.)] ** 2.0 ! { dg-error "Cannot convert" } + print *, [complex :: (['1'])] ** (1.0,2.0) ! { dg-error "Cannot convert= " } + print *, (1.0,2.0) ** [complex :: (['1'])] ! { dg-error "Cannot convert= " } +end =2D- 2.35.3 --sgnirk-7eb0e93a-d6ba-4eb8-a128-4cbd1899d789--