From 92ed82367e7ccf5e031e9cb7c653c14a2d64ca89 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Sun, 10 May 2020 19:46:06 +0200 Subject: [PATCH] PR fortran/93499 - ICE on division by zero in declaration statements Division by zero in declaration statements could sometimes generate NULL pointers being passed around that lead to ICEs. 2020-05-10 Harald Anlauf gcc/fortran/ PR fortran/93499 * arith.c (gfc_divide): Catch division by zero. (eval_intrinsic_f3): Safeguard for NULL operands. gcc/testsuite/ PR fortran/93499 * gfortran.dg/pr93499.f90: New test. --- gcc/fortran/ChangeLog | 6 +++++ gcc/fortran/arith.c | 34 +++++++++++++++++++++++++++ gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/gfortran.dg/pr93499.f90 | 10 ++++++++ 4 files changed, 55 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/pr93499.f90 diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index beeabfa2839..6f1db3890f9 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,9 @@ +2020-05-10 Harald Anlauf + + PR fortran/93499 + * arith.c (gfc_divide): Catch division by zero. + (eval_intrinsic_f3): Safeguard for NULL operands. + 2020-05-05 Steve Kargl Harald Anlauf diff --git a/gcc/fortran/arith.c b/gcc/fortran/arith.c index 422ef40c431..1cd0867a941 100644 --- a/gcc/fortran/arith.c +++ b/gcc/fortran/arith.c @@ -1746,6 +1746,9 @@ eval_intrinsic_f3 (gfc_intrinsic_op op, gfc_expr *result; eval_f f; + if (!op1 && !op2) + return NULL; + result = reduce_binary0 (op1, op2); if (result != NULL) return eval_type_intrinsic0(op, result); @@ -1803,6 +1806,37 @@ gfc_multiply (gfc_expr *op1, gfc_expr *op2) gfc_expr * gfc_divide (gfc_expr *op1, gfc_expr *op2) { + if (op2 && op2->expr_type == EXPR_CONSTANT) + { + arith rc = ARITH_OK; + switch (op2->ts.type) + { + case BT_INTEGER: + /* non-integer divided by integer 0 is handled elsewhere. */ + if (mpz_sgn (op2->value.integer) == 0 + && op1->ts.type == BT_INTEGER) + rc = ARITH_DIV0; + break; + case BT_REAL: + if (mpfr_sgn (op2->value.real) == 0 + && flag_range_check == 1) + rc = ARITH_DIV0; + break; + case BT_COMPLEX: + if (mpc_cmp_si_si (op2->value.complex, 0, 0) == 0 + && flag_range_check == 1) + rc = ARITH_DIV0; + break; + default: + gfc_internal_error ("gfc_divide(): Bad basic type"); + } + if (rc == ARITH_DIV0) + { + gfc_seen_div0 = true; + gfc_error ("Division by zero at %L", &op2->where); + return NULL; + } + } return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 9deb7ac1cff..791c62aeab8 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-05-10 Harald Anlauf + + PR fortran/93499 + * gfortran.dg/pr93499.f90: New test. + 2020-05-09 Jakub Jelinek PR testsuite/95008 diff --git a/gcc/testsuite/gfortran.dg/pr93499.f90 b/gcc/testsuite/gfortran.dg/pr93499.f90 new file mode 100644 index 00000000000..7a414bb6016 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr93499.f90 @@ -0,0 +1,10 @@ +! { dg-do compile } +! PR 93499 - this used to ICE. Original test case by Gerhard Steinmetz. + +program p + integer :: a((0.)/0) ! { dg-error "Division by zero" } + type t(n) + integer, len :: n + end type t + type(t((0)/0)) :: x ! { dg-error "Division by zero" } +end -- 2.30.2