From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2071) id B15EE3858290; Wed, 12 Oct 2022 17:48:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B15EE3858290 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665596909; bh=TwBtL83Zxi6/otStYE+kiWQDPsQ3AyGaueVxkBoaUnE=; h=From:To:Subject:Date:From; b=ZNtmrzJUoJB+tHFDi3fyehAR0PMrbYVR9kFColkjovnJ8eeAuFJqU1bCx47wi//UL IDi99q1cgd4DAGFiubdfdzKLJzbulMzoizQgUH+ALKIhvnOLQAZ3lyaChHUUBZsJZ5 r84d3me2xKh3qibZfB9dSw/S/P4Mi4fSn2ETJJh8= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Harald Anlauf To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3260] Fortran: check types of operands of arithmetic binary operations [PR107217] X-Act-Checkin: gcc X-Git-Author: Harald Anlauf X-Git-Refname: refs/heads/master X-Git-Oldrev: caf9db5a7f99fae8b6088328b9b48ee79fa5e5f0 X-Git-Newrev: 7858368c3f3875f6bf634119e5731dc3c808a7c3 Message-Id: <20221012174829.B15EE3858290@sourceware.org> Date: Wed, 12 Oct 2022 17:48:29 +0000 (GMT) List-Id: https://gcc.gnu.org/g:7858368c3f3875f6bf634119e5731dc3c808a7c3 commit r13-3260-g7858368c3f3875f6bf634119e5731dc3c808a7c3 Author: Harald Anlauf Date: Tue Oct 11 22:08:48 2022 +0200 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. Diff: --- gcc/fortran/arith.cc | 15 +++++++++++++++ gcc/testsuite/gfortran.dg/pr107217.f90 | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) 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