]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: error recovery on arithmetic overflow on unary operations [PR113799]
authorHarald Anlauf <anlauf@gmx.de>
Thu, 8 Feb 2024 20:51:38 +0000 (21:51 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 9 Feb 2024 18:24:02 +0000 (19:24 +0100)
PR fortran/113799

gcc/fortran/ChangeLog:

* arith.cc (reduce_unary): Remember any overflow encountered during
reduction of unary arithmetic operations on array constructors and
continue, and return error status, but terminate on serious errors.

gcc/testsuite/ChangeLog:

* gfortran.dg/arithmetic_overflow_2.f90: New test.

gcc/fortran/arith.cc
gcc/testsuite/gfortran.dg/arithmetic_overflow_2.f90 [new file with mode: 0644]

index 0598f6ac51b9922a5674946de562c04b62c513c1..d17d1aaa1d91285c10c080ca7c7f1e1ab736d55d 100644 (file)
@@ -1323,6 +1323,7 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
   gfc_constructor *c;
   gfc_expr *r;
   arith rc;
+  bool ov = false;
 
   if (op->expr_type == EXPR_CONSTANT)
     return eval (op, result);
@@ -1336,13 +1337,17 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
     {
       rc = reduce_unary (eval, c->expr, &r);
 
-      if (rc != ARITH_OK)
+      /* Remember any overflow encountered during reduction and continue,
+        but terminate on serious errors.  */
+      if (rc == ARITH_OVERFLOW)
+       ov = true;
+      else if (rc != ARITH_OK)
        break;
 
       gfc_replace_expr (c->expr, r);
     }
 
-  if (rc != ARITH_OK)
+  if (rc != ARITH_OK && rc != ARITH_OVERFLOW)
     gfc_constructor_free (head);
   else
     {
@@ -1363,7 +1368,7 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
       *result = r;
     }
 
-  return rc;
+  return ov ? ARITH_OVERFLOW : rc;
 }
 
 
diff --git a/gcc/testsuite/gfortran.dg/arithmetic_overflow_2.f90 b/gcc/testsuite/gfortran.dg/arithmetic_overflow_2.f90
new file mode 100644 (file)
index 0000000..6ca27f7
--- /dev/null
@@ -0,0 +1,12 @@
+! { dg-do compile }
+! { dg-additional-options "-frange-check" }
+!
+! PR fortran/113799 - handle arithmetic overflow on unary minus
+
+program p
+  implicit none
+  real, parameter :: inf = real(z'7F800000')
+  real, parameter :: someInf(*) = [inf, 0.]
+  print *, -someInf         ! { dg-error "Arithmetic overflow" }
+  print *, minval(-someInf) ! { dg-error "Arithmetic overflow" }
+end