aboutsummaryrefslogtreecommitdiff
path: root/gcc/fortran/arith.c
diff options
context:
space:
mode:
authorHarald Anlauf <anlauf@gmx.de>2020-05-10 19:46:06 +0200
committerHarald Anlauf <anlauf@gmx.de>2020-05-10 19:46:06 +0200
commit92ed82367e7ccf5e031e9cb7c653c14a2d64ca89 (patch)
tree528e107b9a21b3009080c3a34032b0ffd54629be /gcc/fortran/arith.c
parentef6394205d7bcab00dca01182d708ad5a6360a7b (diff)
downloadgcc-92ed82367e7ccf5e031e9cb7c653c14a2d64ca89.zip
gcc-92ed82367e7ccf5e031e9cb7c653c14a2d64ca89.tar.gz
gcc-92ed82367e7ccf5e031e9cb7c653c14a2d64ca89.tar.bz2
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 <anlauf@gmx.de> 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.
Diffstat (limited to 'gcc/fortran/arith.c')
-rw-r--r--gcc/fortran/arith.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/gcc/fortran/arith.c b/gcc/fortran/arith.c
index 422ef40..1cd0867 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);
}