]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: error recovery simplifying UNPACK for insufficient FIELD [PR107922]
authorHarald Anlauf <anlauf@gmx.de>
Thu, 1 Dec 2022 20:16:46 +0000 (21:16 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Sun, 4 Dec 2022 19:59:03 +0000 (20:59 +0100)
gcc/fortran/ChangeLog:

PR fortran/107922
* simplify.cc (gfc_simplify_unpack): Terminate simplification when
array-valued argument FIELD does not provide enough elements.

gcc/testsuite/ChangeLog:

PR fortran/107922
* gfortran.dg/unpack_field_1.f90: New test.

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

index b6184181f26a090c4c5a306571f2c6028fd2d39c..aff9a1b8ced5e15ae02b06d65eda77f3cc22c6c6 100644 (file)
@@ -8485,7 +8485,16 @@ gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
            }
        }
       else if (field->expr_type == EXPR_ARRAY)
-       e = gfc_copy_expr (field_ctor->expr);
+       {
+         if (field_ctor)
+           e = gfc_copy_expr (field_ctor->expr);
+         else
+           {
+             /* Not enough elements in array FIELD.  */
+             gfc_free_expr (result);
+             return &gfc_bad_expr;
+           }
+       }
       else
        e = gfc_copy_expr (field);
 
diff --git a/gcc/testsuite/gfortran.dg/unpack_field_1.f90 b/gcc/testsuite/gfortran.dg/unpack_field_1.f90
new file mode 100644 (file)
index 0000000..ca3cfbd
--- /dev/null
@@ -0,0 +1,15 @@
+! { dg-do compile }
+! PR fortran/107922 - ICE in gfc_simplify_unpack
+! Test error recovery when shapes of FIELD and MASK do not match
+! Contributed by G.Steinmetz
+
+program p
+  integer, parameter :: a(2) = 1
+  integer, parameter :: d(3) = 1
+  logical, parameter :: mask(3) = [.false.,.true.,.false.]
+  integer, parameter :: b(2) = unpack(a,mask,a)          ! { dg-error "must have identical shape" }
+  integer :: c(3) = unpack(a,[.false.,.true.,.false.],a) ! { dg-error "must have identical shape" }
+  print *, unpack(a,mask,a)                              ! { dg-error "must have identical shape" }
+  print *, unpack(a,mask,d) ! OK
+  print *, unpack(a,mask,3) ! OK
+end