]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: check types of operands of arithmetic binary operations [PR107217]
authorHarald Anlauf <anlauf@gmx.de>
Tue, 11 Oct 2022 20:08:48 +0000 (22:08 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Wed, 12 Oct 2022 17:46:46 +0000 (19:46 +0200)
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
gcc/testsuite/gfortran.dg/pr107217.f90 [new file with mode: 0644]

index 9e079e42995b561fe872aed5925e5a805b9ab3ff..14ba931e37f27569c1f14f485eefb2da1d774781 100644 (file)
@@ -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 (file)
index 0000000..9c8492e
--- /dev/null
@@ -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