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: 1932 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp2284157wrs; Tue, 11 Oct 2022 13:23:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6qtrwojnxuCLJp3RgtLBNqDJvB9oyvZ9KXPThHyYZ/uqEvGvwvFNMnpkxUf4DfY7njlBVq X-Received: by 2002:a17:906:5d11:b0:78d:a41c:6a2d with SMTP id g17-20020a1709065d1100b0078da41c6a2dmr11641005ejt.215.1665519839106; Tue, 11 Oct 2022 13:23:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665519839; cv=none; d=google.com; s=arc-20160816; b=ea+9H8BO7r8ulb+uUdFu+StEYDCpzElcfdPSb1a6cpm7KzePoPbzDedzOfdfrjFzGC R2FHyy7ic47K+/RaMzSe6mHXhMYDYkFVD3SxrcOeaKqZEh0oWgb8/hf1hx0ZQFYErPc2 jR2zOUf/O2qBKTEwQ907pI3HjfOBkqPA8OxugjsOhiEgP2+Bvv5YtAsPMBpF3/pP8/am dImzgqf/LEPcIUj17ovEIxQCoPWevqQPWYYFIX70n5cL4Dq3FgIQDh5HZpiX/Mk+3uS7 aeHFJozacNdMqPKs4wG+l4wV5g9XQuwjgGRLAsEIczb7D7ZWX/cFglJgRXSAktwfcFU3 d18A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:sensitivity :importance:date:subject:to:message-id:mime-version:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=nQXZNEQlbhyOtN3KudOCNlL3GWPK2xQPy75KL9z7xos=; b=pJCdQFy92sZFJDbcSVep1SpPfQJCwx9ph69EDiKOrwcdCblc/1UJR0WAL5GorI/5d7 94+isep797GyCrw+/qHSqjRiQWEfOZy2UuOgdDBqgv7asLimytFPMSinHrP5ztpSpAJC ZfbTCwVY/4YI7qqT7cwGhZrtedbNUrxJaZWxJVACi1TlTUcPziskqEESC6gor79k25Gd Dy4xgKiiIHgoEM04KGiovRf51KbNUoYfknMBwK8VVBS8WESCeL1+wgEKxHBGQ3Sl8E24 yvtG78tw+6agE/cD8VUPKOJgK/RGs2oUSufxqxlYjrIv5vdzvaqgewp7HBe/c+S4+dXK 9ghg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qoRqAGZ8; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t18-20020a056402021200b00459b04a18e5si13257374edv.435.2022.10.11.13.23.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Oct 2022 13:23:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qoRqAGZ8; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 12AF93857BB1 for ; Tue, 11 Oct 2022 20:23:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 12AF93857BB1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665519829; bh=nQXZNEQlbhyOtN3KudOCNlL3GWPK2xQPy75KL9z7xos=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=qoRqAGZ84+cYTI0RolVJd+LSV2b/uYFFScIpLW8k7TmNCVC92/tM7+/FMQNxkhHGw AjR62ynHeGCqo1xN+Ho7I1YiDq7Ckz/f+6YOX5xAS5LJQC9qiqMzIFUzhH28LBu7e5 24Sg5f3YQwxqHJub+ikMJbTH8aZ88J/tY6Ttafvw= 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 Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:37mOzRaTTWgRNwXnTS1OTU8i5vOFNfLB3TLIhVHTOUe8b0sHsQDie7KOIJOLbdSoe6Vrf 5FHZaB0GOojyG3njWYRahsbov4tj5IW3MxI60sGBoExnooqEgXOY1uawAfdhWg2uFLyA72gKZWaJ rVLlyC9c8OSIxaR3K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D8GIjt25oq5 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746424130729551496?= X-GMAIL-MSGID: =?utf-8?q?1746424130729551496?= 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