From patchwork Tue Oct 11 20:23:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 58665 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E3C8385742F for ; Tue, 11 Oct 2022 20:23:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E3C8385742F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665519812; bh=nQXZNEQlbhyOtN3KudOCNlL3GWPK2xQPy75KL9z7xos=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=k+8A4lZ/ZyYnNglqK7JoqlZpJ/5yE+BHpo4hAuysREJ1xaNtYxZTxqUB/9I2CLZoJ 6gW9Q1XtsOSGU33tiB/uBiOW62M/BaoP5gaDu9LIkVdQcDeW6P0t5hOK/LUxgoE5ep UskhSmkvSg5Xk+d9BhgLM80VklwVJoS8Ka5KMXa0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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: To: fortran , gcc-patches Subject: [PATCH] Fortran: check types of operands of arithmetic binary operations [PR107217] Date: Tue, 11 Oct 2022 22:23:01 +0200 Importance: normal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8FVCRqNgEF4grvhuQTrAUDdQ3ZDPHL6jAGnjnmnxAvieYWeU9IvAbNJXH+I QBAhJHu5wY68RqQ+o8kcWcMxWqN44fDLhMibGzEFyvu58oEl8Om5cbO9IVDU759wT+HBEXXcE JjAiwCJMpaP55XHPpAZb6KXrFkZzNL9cDBR1ZrV0TUPk9DMN8h6jKKGknTP93rD8GIjt25oq5 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 From 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. --- 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 --- 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 != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where); switch (op1->ts.type) @@ -658,6 +661,9 @@ gfc_arith_minus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) gfc_expr *result; arith rc; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where); switch (op1->ts.type) @@ -692,6 +698,9 @@ gfc_arith_times (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) gfc_expr *result; arith rc; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where); switch (op1->ts.type) @@ -727,6 +736,9 @@ gfc_arith_divide (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) gfc_expr *result; arith rc; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + rc = ARITH_OK; result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where); @@ -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 = ARITH_OK; result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where); diff --git a/gcc/testsuite/gfortran.dg/pr107217.f90 b/gcc/testsuite/gfortran.dg/pr107217.f90 new file mode 100644 index 00000000000..9c8492e64f0 --- /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 -- 2.35.3