public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [ patch, libfortran] Fix PR 81581
@ 2017-07-30 18:55 Thomas Koenig
  2017-07-31  2:25 ` Jerry DeLisle
  0 siblings, 1 reply; 2+ messages in thread
From: Thomas Koenig @ 2017-07-30 18:55 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 6169 bytes --]

Hello world,

the attached patch performs a runtime-check for the dim arguments
for transformational intrinsic functions.

I debated with myself a bit if I should make this depend on
bounds checking, but I finally decided against it.  The tests
performed are extemely cheap, the values are still in registers,
so any runtime overhead should be small. By contrast, loading
something from an external structure is probably more expensive
(but I didn't bother actually check).

While visiting the code, I also took the opportunity to make
the rank and dim variables in the 'm' class of functions to index_type,
to make their use consistent.

Regarding the test case names, I was unable to resist.

Regression-tested. OK for trunk?

	Thomas

2017-07-30  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/81581
	* m4/ifuntion.m4 (name`'rtype_qual`_'atype_code): Perform check
	for dim.
	(`m'name`'rtype_qual`_'atype_code): Likewise. Change type of rank
	and tim to index_type.
	(`s'name`'rtype_qual`_'atype_code): Perform check for dim.
	* generated/iall_i16.c: Regenerated.
	* generated/iall_i1.c: Regenerated.
	* generated/iall_i2.c: Regenerated.
	* generated/iall_i4.c: Regenerated.
	* generated/iall_i8.c: Regenerated.
	* generated/iany_i16.c: Regenerated.
	* generated/iany_i1.c: Regenerated.
	* generated/iany_i2.c: Regenerated.
	* generated/iany_i4.c: Regenerated.
	* generated/iany_i8.c: Regenerated.
	* generated/iparity_i16.c: Regenerated.
	* generated/iparity_i1.c: Regenerated.
	* generated/iparity_i2.c: Regenerated.
	* generated/iparity_i4.c: Regenerated.
	* generated/iparity_i8.c: Regenerated.
	* generated/maxloc1_16_i16.c: Regenerated.
	* generated/maxloc1_16_i1.c: Regenerated.
	* generated/maxloc1_16_i2.c: Regenerated.
	* generated/maxloc1_16_i4.c: Regenerated.
	* generated/maxloc1_16_i8.c: Regenerated.
	* generated/maxloc1_16_r10.c: Regenerated.
	* generated/maxloc1_16_r16.c: Regenerated.
	* generated/maxloc1_16_r4.c: Regenerated.
	* generated/maxloc1_16_r8.c: Regenerated.
	* generated/maxloc1_4_i16.c: Regenerated.
	* generated/maxloc1_4_i1.c: Regenerated.
	* generated/maxloc1_4_i2.c: Regenerated.
	* generated/maxloc1_4_i4.c: Regenerated.
	* generated/maxloc1_4_i8.c: Regenerated.
	* generated/maxloc1_4_r10.c: Regenerated.
	* generated/maxloc1_4_r16.c: Regenerated.
	* generated/maxloc1_4_r4.c: Regenerated.
	* generated/maxloc1_4_r8.c: Regenerated.
	* generated/maxloc1_8_i16.c: Regenerated.
	* generated/maxloc1_8_i1.c: Regenerated.
	* generated/maxloc1_8_i2.c: Regenerated.
	* generated/maxloc1_8_i4.c: Regenerated.
	* generated/maxloc1_8_i8.c: Regenerated.
	* generated/maxloc1_8_r10.c: Regenerated.
	* generated/maxloc1_8_r16.c: Regenerated.
	* generated/maxloc1_8_r4.c: Regenerated.
	* generated/maxloc1_8_r8.c: Regenerated.
	* generated/maxval_i16.c: Regenerated.
	* generated/maxval_i1.c: Regenerated.
	* generated/maxval_i2.c: Regenerated.
	* generated/maxval_i4.c: Regenerated.
	* generated/maxval_i8.c: Regenerated.
	* generated/maxval_r10.c: Regenerated.
	* generated/maxval_r16.c: Regenerated.
	* generated/maxval_r4.c: Regenerated.
	* generated/maxval_r8.c: Regenerated.
	* generated/minloc1_16_i16.c: Regenerated.
	* generated/minloc1_16_i1.c: Regenerated.
	* generated/minloc1_16_i2.c: Regenerated.
	* generated/minloc1_16_i4.c: Regenerated.
	* generated/minloc1_16_i8.c: Regenerated.
	* generated/minloc1_16_r10.c: Regenerated.
	* generated/minloc1_16_r16.c: Regenerated.
	* generated/minloc1_16_r4.c: Regenerated.
	* generated/minloc1_16_r8.c: Regenerated.
	* generated/minloc1_4_i16.c: Regenerated.
	* generated/minloc1_4_i1.c: Regenerated.
	* generated/minloc1_4_i2.c: Regenerated.
	* generated/minloc1_4_i4.c: Regenerated.
	* generated/minloc1_4_i8.c: Regenerated.
	* generated/minloc1_4_r10.c: Regenerated.
	* generated/minloc1_4_r16.c: Regenerated.
	* generated/minloc1_4_r4.c: Regenerated.
	* generated/minloc1_4_r8.c: Regenerated.
	* generated/minloc1_8_i16.c: Regenerated.
	* generated/minloc1_8_i1.c: Regenerated.
	* generated/minloc1_8_i2.c: Regenerated.
	* generated/minloc1_8_i4.c: Regenerated.
	* generated/minloc1_8_i8.c: Regenerated.
	* generated/minloc1_8_r10.c: Regenerated.
	* generated/minloc1_8_r16.c: Regenerated.
	* generated/minloc1_8_r4.c: Regenerated.
	* generated/minloc1_8_r8.c: Regenerated.
	* generated/minval_i16.c: Regenerated.
	* generated/minval_i1.c: Regenerated.
	* generated/minval_i2.c: Regenerated.
	* generated/minval_i4.c: Regenerated.
	* generated/minval_i8.c: Regenerated.
	* generated/minval_r10.c: Regenerated.
	* generated/minval_r16.c: Regenerated.
	* generated/minval_r4.c: Regenerated.
	* generated/minval_r8.c: Regenerated.
	* generated/norm2_r10.c: Regenerated.
	* generated/norm2_r16.c: Regenerated.
	* generated/norm2_r4.c: Regenerated.
	* generated/norm2_r8.c: Regenerated.
	* generated/parity_l16.c: Regenerated.
	* generated/parity_l1.c: Regenerated.
	* generated/parity_l2.c: Regenerated.
	* generated/parity_l4.c: Regenerated.
	* generated/parity_l8.c: Regenerated.
	* generated/product_c10.c: Regenerated.
	* generated/product_c16.c: Regenerated.
	* generated/product_c4.c: Regenerated.
	* generated/product_c8.c: Regenerated.
	* generated/product_i16.c: Regenerated.
	* generated/product_i1.c: Regenerated.
	* generated/product_i2.c: Regenerated.
	* generated/product_i4.c: Regenerated.
	* generated/product_i8.c: Regenerated.
	* generated/product_r10.c: Regenerated.
	* generated/product_r16.c: Regenerated.
	* generated/product_r4.c: Regenerated.
	* generated/product_r8.c: Regenerated.
	* generated/sum_c10.c: Regenerated.
	* generated/sum_c16.c: Regenerated.
	* generated/sum_c4.c: Regenerated.
	* generated/sum_c8.c: Regenerated.
	* generated/sum_i16.c: Regenerated.
	* generated/sum_i1.c: Regenerated.
	* generated/sum_i2.c: Regenerated.
	* generated/sum_i4.c: Regenerated.
	* generated/sum_i8.c: Regenerated.
	* generated/sum_r10.c: Regenerated.
	* generated/sum_r16.c: Regenerated.
	* generated/sum_r4.c: Regenerated.
	* generated/sum_r8.c: Regenerated.

2017-07-30  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/81581
	* gfortran.dg/dim_sum_1.f90: New test case.
	* gfortran.dg/dim_sum_2.f90: New test case.
	* gfortran.dg/dim_sum_3.f90: New test case.

[-- Attachment #2: p1.diff --]
[-- Type: text/x-patch, Size: 226291 bytes --]

Index: m4/ifunction.m4
===================================================================
--- m4/ifunction.m4	(Revision 250720)
+++ m4/ifunction.m4	(Arbeitskopie)
@@ -45,6 +45,13 @@ name`'rtype_qual`_'atype_code (rtype * const restr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in u_name intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -193,8 +200,8 @@ void
   rtype_name * restrict dest;
   const atype_name * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -204,6 +211,14 @@ void
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in u_name intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -380,6 +395,13 @@ void
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in u_name intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i1.c
===================================================================
--- generated/iall_i1.c	(Revision 250720)
+++ generated/iall_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i1 (gfc_array_i1 * const restrict retarray,
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i16.c
===================================================================
--- generated/iall_i16.c	(Revision 250720)
+++ generated/iall_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i16 (gfc_array_i16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i16 (gfc_array_i16 * const restrict retarray
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i16 (gfc_array_i16 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i16 (gfc_array_i16 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i2.c
===================================================================
--- generated/iall_i2.c	(Revision 250720)
+++ generated/iall_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i2 (gfc_array_i2 * const restrict retarray,
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i4.c
===================================================================
--- generated/iall_i4.c	(Revision 250720)
+++ generated/iall_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i4 (gfc_array_i4 * const restrict retarray,
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i8.c
===================================================================
--- generated/iall_i8.c	(Revision 250720)
+++ generated/iall_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i8 (gfc_array_i8 * const restrict retarray,
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i1.c
===================================================================
--- generated/iany_i1.c	(Revision 250720)
+++ generated/iany_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i1 (gfc_array_i1 * const restrict retarray,
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i16.c
===================================================================
--- generated/iany_i16.c	(Revision 250720)
+++ generated/iany_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i16 (gfc_array_i16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i16 (gfc_array_i16 * const restrict retarray
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i16 (gfc_array_i16 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i16 (gfc_array_i16 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i2.c
===================================================================
--- generated/iany_i2.c	(Revision 250720)
+++ generated/iany_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i2 (gfc_array_i2 * const restrict retarray,
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i4.c
===================================================================
--- generated/iany_i4.c	(Revision 250720)
+++ generated/iany_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i4 (gfc_array_i4 * const restrict retarray,
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i8.c
===================================================================
--- generated/iany_i8.c	(Revision 250720)
+++ generated/iany_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i8 (gfc_array_i8 * const restrict retarray,
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i1.c
===================================================================
--- generated/iparity_i1.c	(Revision 250720)
+++ generated/iparity_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i1 (gfc_array_i1 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i1 (gfc_array_i1 * const restrict retarra
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i1 (gfc_array_i1 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i1 (gfc_array_i1 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i16.c
===================================================================
--- generated/iparity_i16.c	(Revision 250720)
+++ generated/iparity_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i16 (gfc_array_i16 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i16 (gfc_array_i16 * const restrict retar
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i16 (gfc_array_i16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i16 (gfc_array_i16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i2.c
===================================================================
--- generated/iparity_i2.c	(Revision 250720)
+++ generated/iparity_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i2 (gfc_array_i2 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i2 (gfc_array_i2 * const restrict retarra
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i2 (gfc_array_i2 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i2 (gfc_array_i2 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i4.c
===================================================================
--- generated/iparity_i4.c	(Revision 250720)
+++ generated/iparity_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i4 (gfc_array_i4 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i4 (gfc_array_i4 * const restrict retarra
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i4 (gfc_array_i4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i4 (gfc_array_i4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i8.c
===================================================================
--- generated/iparity_i8.c	(Revision 250720)
+++ generated/iparity_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i8 (gfc_array_i8 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i8 (gfc_array_i8 * const restrict retarra
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i8 (gfc_array_i8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i8 (gfc_array_i8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i1.c
===================================================================
--- generated/maxloc1_16_i1.c	(Revision 250720)
+++ generated/maxloc1_16_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i1 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i1 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i1 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i1 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i16.c
===================================================================
--- generated/maxloc1_16_i16.c	(Revision 250720)
+++ generated/maxloc1_16_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i16 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i16 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i2.c
===================================================================
--- generated/maxloc1_16_i2.c	(Revision 250720)
+++ generated/maxloc1_16_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i2 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i2 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i2 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i2 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i4.c
===================================================================
--- generated/maxloc1_16_i4.c	(Revision 250720)
+++ generated/maxloc1_16_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i4 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i4 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i8.c
===================================================================
--- generated/maxloc1_16_i8.c	(Revision 250720)
+++ generated/maxloc1_16_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i8 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i8 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_r10.c
===================================================================
--- generated/maxloc1_16_r10.c	(Revision 250720)
+++ generated/maxloc1_16_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_r10 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_r10 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_r10 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_r10 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_r16.c
===================================================================
--- generated/maxloc1_16_r16.c	(Revision 250720)
+++ generated/maxloc1_16_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_r16 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_r16 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_r16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_r16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_r4.c
===================================================================
--- generated/maxloc1_16_r4.c	(Revision 250720)
+++ generated/maxloc1_16_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_r4 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_r4 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_r4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_r4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_r8.c
===================================================================
--- generated/maxloc1_16_r8.c	(Revision 250720)
+++ generated/maxloc1_16_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_r8 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_r8 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_r8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_r8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i1.c
===================================================================
--- generated/maxloc1_4_i1.c	(Revision 250720)
+++ generated/maxloc1_4_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i1 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i1 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i1 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i1 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i16.c
===================================================================
--- generated/maxloc1_4_i16.c	(Revision 250720)
+++ generated/maxloc1_4_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i16 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i16 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i2.c
===================================================================
--- generated/maxloc1_4_i2.c	(Revision 250720)
+++ generated/maxloc1_4_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i2 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i2 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i2 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i2 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i4.c
===================================================================
--- generated/maxloc1_4_i4.c	(Revision 250720)
+++ generated/maxloc1_4_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i8.c
===================================================================
--- generated/maxloc1_4_i8.c	(Revision 250720)
+++ generated/maxloc1_4_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i8 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i8 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_r10.c
===================================================================
--- generated/maxloc1_4_r10.c	(Revision 250720)
+++ generated/maxloc1_4_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_r10 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_r10 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_r10 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_r10 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_r16.c
===================================================================
--- generated/maxloc1_4_r16.c	(Revision 250720)
+++ generated/maxloc1_4_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_r16 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_r16 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_r16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_r16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_r4.c
===================================================================
--- generated/maxloc1_4_r4.c	(Revision 250720)
+++ generated/maxloc1_4_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_r4 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_r4 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_r4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_r4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_r8.c
===================================================================
--- generated/maxloc1_4_r8.c	(Revision 250720)
+++ generated/maxloc1_4_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_r8 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_r8 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_r8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_r8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i1.c
===================================================================
--- generated/maxloc1_8_i1.c	(Revision 250720)
+++ generated/maxloc1_8_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i1 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i1 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i1 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i1 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i16.c
===================================================================
--- generated/maxloc1_8_i16.c	(Revision 250720)
+++ generated/maxloc1_8_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i16 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i16 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i2.c
===================================================================
--- generated/maxloc1_8_i2.c	(Revision 250720)
+++ generated/maxloc1_8_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i2 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i2 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i2 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i2 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i4.c
===================================================================
--- generated/maxloc1_8_i4.c	(Revision 250720)
+++ generated/maxloc1_8_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i4 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i4 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i8.c
===================================================================
--- generated/maxloc1_8_i8.c	(Revision 250720)
+++ generated/maxloc1_8_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i8 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i8 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_r10.c
===================================================================
--- generated/maxloc1_8_r10.c	(Revision 250720)
+++ generated/maxloc1_8_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_r10 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_r10 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_r10 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_r10 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_r16.c
===================================================================
--- generated/maxloc1_8_r16.c	(Revision 250720)
+++ generated/maxloc1_8_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_r16 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_r16 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_r16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_r16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_r4.c
===================================================================
--- generated/maxloc1_8_r4.c	(Revision 250720)
+++ generated/maxloc1_8_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_r4 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_r4 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_r4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_r4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_r8.c
===================================================================
--- generated/maxloc1_8_r8.c	(Revision 250720)
+++ generated/maxloc1_8_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_r8 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_r8 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_r8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_r8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i1.c
===================================================================
--- generated/maxval_i1.c	(Revision 250720)
+++ generated/maxval_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i1 (gfc_array_i1 * const restrict retarray
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i1 (gfc_array_i1 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i1 (gfc_array_i1 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i16.c
===================================================================
--- generated/maxval_i16.c	(Revision 250720)
+++ generated/maxval_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i16 (gfc_array_i16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i16 (gfc_array_i16 * const restrict retarr
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i16 (gfc_array_i16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i16 (gfc_array_i16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i2.c
===================================================================
--- generated/maxval_i2.c	(Revision 250720)
+++ generated/maxval_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i2 (gfc_array_i2 * const restrict retarray
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i2 (gfc_array_i2 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i2 (gfc_array_i2 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i4.c
===================================================================
--- generated/maxval_i4.c	(Revision 250720)
+++ generated/maxval_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i4 (gfc_array_i4 * const restrict retarray
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i4 (gfc_array_i4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i4 (gfc_array_i4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i8.c
===================================================================
--- generated/maxval_i8.c	(Revision 250720)
+++ generated/maxval_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i8 (gfc_array_i8 * const restrict retarray
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i8 (gfc_array_i8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i8 (gfc_array_i8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_r10.c
===================================================================
--- generated/maxval_r10.c	(Revision 250720)
+++ generated/maxval_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_r10 (gfc_array_r10 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_r10 (gfc_array_r10 * const restrict retarr
   GFC_REAL_10 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_r10 (gfc_array_r10 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_r10 (gfc_array_r10 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_r16.c
===================================================================
--- generated/maxval_r16.c	(Revision 250720)
+++ generated/maxval_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_r16 (gfc_array_r16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_r16 (gfc_array_r16 * const restrict retarr
   GFC_REAL_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_r16 (gfc_array_r16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_r16 (gfc_array_r16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_r4.c
===================================================================
--- generated/maxval_r4.c	(Revision 250720)
+++ generated/maxval_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_r4 (gfc_array_r4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_r4 (gfc_array_r4 * const restrict retarray
   GFC_REAL_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_r4 (gfc_array_r4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_r4 (gfc_array_r4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_r8.c
===================================================================
--- generated/maxval_r8.c	(Revision 250720)
+++ generated/maxval_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_r8 (gfc_array_r8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_r8 (gfc_array_r8 * const restrict retarray
   GFC_REAL_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_r8 (gfc_array_r8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_r8 (gfc_array_r8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i1.c
===================================================================
--- generated/minloc1_16_i1.c	(Revision 250720)
+++ generated/minloc1_16_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i1 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i1 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i1 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i1 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i16.c
===================================================================
--- generated/minloc1_16_i16.c	(Revision 250720)
+++ generated/minloc1_16_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i16 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i16 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i2.c
===================================================================
--- generated/minloc1_16_i2.c	(Revision 250720)
+++ generated/minloc1_16_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i2 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i2 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i2 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i2 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i4.c
===================================================================
--- generated/minloc1_16_i4.c	(Revision 250720)
+++ generated/minloc1_16_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i4 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i4 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i8.c
===================================================================
--- generated/minloc1_16_i8.c	(Revision 250720)
+++ generated/minloc1_16_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i8 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i8 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_r10.c
===================================================================
--- generated/minloc1_16_r10.c	(Revision 250720)
+++ generated/minloc1_16_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_r10 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_r10 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_r10 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_r10 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_r16.c
===================================================================
--- generated/minloc1_16_r16.c	(Revision 250720)
+++ generated/minloc1_16_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_r16 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_r16 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_r16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_r16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_r4.c
===================================================================
--- generated/minloc1_16_r4.c	(Revision 250720)
+++ generated/minloc1_16_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_r4 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_r4 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_r4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_r4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_r8.c
===================================================================
--- generated/minloc1_16_r8.c	(Revision 250720)
+++ generated/minloc1_16_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_r8 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_r8 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_r8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_r8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i1.c
===================================================================
--- generated/minloc1_4_i1.c	(Revision 250720)
+++ generated/minloc1_4_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i1 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i1 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i1 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i1 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i16.c
===================================================================
--- generated/minloc1_4_i16.c	(Revision 250720)
+++ generated/minloc1_4_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i16 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i16 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i2.c
===================================================================
--- generated/minloc1_4_i2.c	(Revision 250720)
+++ generated/minloc1_4_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i2 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i2 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i2 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i2 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i4.c
===================================================================
--- generated/minloc1_4_i4.c	(Revision 250720)
+++ generated/minloc1_4_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i4 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i4 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i8.c
===================================================================
--- generated/minloc1_4_i8.c	(Revision 250720)
+++ generated/minloc1_4_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i8 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i8 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_r10.c
===================================================================
--- generated/minloc1_4_r10.c	(Revision 250720)
+++ generated/minloc1_4_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_r10 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_r10 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_r10 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_r10 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_r16.c
===================================================================
--- generated/minloc1_4_r16.c	(Revision 250720)
+++ generated/minloc1_4_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_r16 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_r16 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_r16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_r16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_r4.c
===================================================================
--- generated/minloc1_4_r4.c	(Revision 250720)
+++ generated/minloc1_4_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_r4 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_r4 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_r4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_r4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_r8.c
===================================================================
--- generated/minloc1_4_r8.c	(Revision 250720)
+++ generated/minloc1_4_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_r8 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_r8 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_r8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_r8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i1.c
===================================================================
--- generated/minloc1_8_i1.c	(Revision 250720)
+++ generated/minloc1_8_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i1 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i1 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i1 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i1 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i16.c
===================================================================
--- generated/minloc1_8_i16.c	(Revision 250720)
+++ generated/minloc1_8_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i16 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i16 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i2.c
===================================================================
--- generated/minloc1_8_i2.c	(Revision 250720)
+++ generated/minloc1_8_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i2 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i2 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i2 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i2 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i4.c
===================================================================
--- generated/minloc1_8_i4.c	(Revision 250720)
+++ generated/minloc1_8_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i4 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i4 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i8.c
===================================================================
--- generated/minloc1_8_i8.c	(Revision 250720)
+++ generated/minloc1_8_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i8 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i8 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_r10.c
===================================================================
--- generated/minloc1_8_r10.c	(Revision 250720)
+++ generated/minloc1_8_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_r10 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_r10 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_r10 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_r10 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_r16.c
===================================================================
--- generated/minloc1_8_r16.c	(Revision 250720)
+++ generated/minloc1_8_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_r16 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_r16 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_r16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_r16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_r4.c
===================================================================
--- generated/minloc1_8_r4.c	(Revision 250720)
+++ generated/minloc1_8_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_r4 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_r4 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_r4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_r4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_r8.c
===================================================================
--- generated/minloc1_8_r8.c	(Revision 250720)
+++ generated/minloc1_8_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_r8 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_r8 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_r8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_r8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i1.c
===================================================================
--- generated/minval_i1.c	(Revision 250720)
+++ generated/minval_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i1 (gfc_array_i1 * const restrict retarray
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i1 (gfc_array_i1 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i1 (gfc_array_i1 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i16.c
===================================================================
--- generated/minval_i16.c	(Revision 250720)
+++ generated/minval_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i16 (gfc_array_i16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i16 (gfc_array_i16 * const restrict retarr
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i16 (gfc_array_i16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i16 (gfc_array_i16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i2.c
===================================================================
--- generated/minval_i2.c	(Revision 250720)
+++ generated/minval_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i2 (gfc_array_i2 * const restrict retarray
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i2 (gfc_array_i2 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i2 (gfc_array_i2 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i4.c
===================================================================
--- generated/minval_i4.c	(Revision 250720)
+++ generated/minval_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i4 (gfc_array_i4 * const restrict retarray
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i4 (gfc_array_i4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i4 (gfc_array_i4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i8.c
===================================================================
--- generated/minval_i8.c	(Revision 250720)
+++ generated/minval_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i8 (gfc_array_i8 * const restrict retarray
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i8 (gfc_array_i8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i8 (gfc_array_i8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_r10.c
===================================================================
--- generated/minval_r10.c	(Revision 250720)
+++ generated/minval_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_r10 (gfc_array_r10 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_r10 (gfc_array_r10 * const restrict retarr
   GFC_REAL_10 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_r10 (gfc_array_r10 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_r10 (gfc_array_r10 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_r16.c
===================================================================
--- generated/minval_r16.c	(Revision 250720)
+++ generated/minval_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_r16 (gfc_array_r16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_r16 (gfc_array_r16 * const restrict retarr
   GFC_REAL_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_r16 (gfc_array_r16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_r16 (gfc_array_r16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_r4.c
===================================================================
--- generated/minval_r4.c	(Revision 250720)
+++ generated/minval_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_r4 (gfc_array_r4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_r4 (gfc_array_r4 * const restrict retarray
   GFC_REAL_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_r4 (gfc_array_r4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_r4 (gfc_array_r4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_r8.c
===================================================================
--- generated/minval_r8.c	(Revision 250720)
+++ generated/minval_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_r8 (gfc_array_r8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_r8 (gfc_array_r8 * const restrict retarray
   GFC_REAL_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_r8 (gfc_array_r8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_r8 (gfc_array_r8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/norm2_r10.c
===================================================================
--- generated/norm2_r10.c	(Revision 250720)
+++ generated/norm2_r10.c	(Arbeitskopie)
@@ -55,9 +55,16 @@ norm2_r10 (gfc_array_r10 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in NORM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/norm2_r16.c
===================================================================
--- generated/norm2_r16.c	(Revision 250720)
+++ generated/norm2_r16.c	(Arbeitskopie)
@@ -59,9 +59,16 @@ norm2_r16 (gfc_array_r16 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in NORM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/norm2_r4.c
===================================================================
--- generated/norm2_r4.c	(Revision 250720)
+++ generated/norm2_r4.c	(Arbeitskopie)
@@ -55,9 +55,16 @@ norm2_r4 (gfc_array_r4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in NORM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/norm2_r8.c
===================================================================
--- generated/norm2_r8.c	(Revision 250720)
+++ generated/norm2_r8.c	(Arbeitskopie)
@@ -55,9 +55,16 @@ norm2_r8 (gfc_array_r8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in NORM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l1.c
===================================================================
--- generated/parity_l1.c	(Revision 250720)
+++ generated/parity_l1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l1 (gfc_array_l1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l16.c
===================================================================
--- generated/parity_l16.c	(Revision 250720)
+++ generated/parity_l16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l16 (gfc_array_l16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l2.c
===================================================================
--- generated/parity_l2.c	(Revision 250720)
+++ generated/parity_l2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l2 (gfc_array_l2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l4.c
===================================================================
--- generated/parity_l4.c	(Revision 250720)
+++ generated/parity_l4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l4 (gfc_array_l4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l8.c
===================================================================
--- generated/parity_l8.c	(Revision 250720)
+++ generated/parity_l8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l8 (gfc_array_l8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/product_c10.c
===================================================================
--- generated/product_c10.c	(Revision 250720)
+++ generated/product_c10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_c10 (gfc_array_c10 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_c10 (gfc_array_c10 * const restrict retar
   GFC_COMPLEX_10 * restrict dest;
   const GFC_COMPLEX_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_c10 (gfc_array_c10 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_c10 (gfc_array_c10 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_c16.c
===================================================================
--- generated/product_c16.c	(Revision 250720)
+++ generated/product_c16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_c16 (gfc_array_c16 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_c16 (gfc_array_c16 * const restrict retar
   GFC_COMPLEX_16 * restrict dest;
   const GFC_COMPLEX_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_c16 (gfc_array_c16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_c16 (gfc_array_c16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_c4.c
===================================================================
--- generated/product_c4.c	(Revision 250720)
+++ generated/product_c4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_c4 (gfc_array_c4 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_c4 (gfc_array_c4 * const restrict retarra
   GFC_COMPLEX_4 * restrict dest;
   const GFC_COMPLEX_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_c4 (gfc_array_c4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_c4 (gfc_array_c4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_c8.c
===================================================================
--- generated/product_c8.c	(Revision 250720)
+++ generated/product_c8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_c8 (gfc_array_c8 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_c8 (gfc_array_c8 * const restrict retarra
   GFC_COMPLEX_8 * restrict dest;
   const GFC_COMPLEX_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_c8 (gfc_array_c8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_c8 (gfc_array_c8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i1.c
===================================================================
--- generated/product_i1.c	(Revision 250720)
+++ generated/product_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i1 (gfc_array_i1 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i1 (gfc_array_i1 * const restrict retarra
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i1 (gfc_array_i1 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i1 (gfc_array_i1 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i16.c
===================================================================
--- generated/product_i16.c	(Revision 250720)
+++ generated/product_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i16 (gfc_array_i16 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i16 (gfc_array_i16 * const restrict retar
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i16 (gfc_array_i16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i16 (gfc_array_i16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i2.c
===================================================================
--- generated/product_i2.c	(Revision 250720)
+++ generated/product_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i2 (gfc_array_i2 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i2 (gfc_array_i2 * const restrict retarra
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i2 (gfc_array_i2 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i2 (gfc_array_i2 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i4.c
===================================================================
--- generated/product_i4.c	(Revision 250720)
+++ generated/product_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i4 (gfc_array_i4 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i4 (gfc_array_i4 * const restrict retarra
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i4 (gfc_array_i4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i4 (gfc_array_i4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i8.c
===================================================================
--- generated/product_i8.c	(Revision 250720)
+++ generated/product_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i8 (gfc_array_i8 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i8 (gfc_array_i8 * const restrict retarra
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i8 (gfc_array_i8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i8 (gfc_array_i8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_r10.c
===================================================================
--- generated/product_r10.c	(Revision 250720)
+++ generated/product_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_r10 (gfc_array_r10 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_r10 (gfc_array_r10 * const restrict retar
   GFC_REAL_10 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_r10 (gfc_array_r10 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_r10 (gfc_array_r10 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_r16.c
===================================================================
--- generated/product_r16.c	(Revision 250720)
+++ generated/product_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_r16 (gfc_array_r16 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_r16 (gfc_array_r16 * const restrict retar
   GFC_REAL_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_r16 (gfc_array_r16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_r16 (gfc_array_r16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_r4.c
===================================================================
--- generated/product_r4.c	(Revision 250720)
+++ generated/product_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_r4 (gfc_array_r4 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_r4 (gfc_array_r4 * const restrict retarra
   GFC_REAL_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_r4 (gfc_array_r4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_r4 (gfc_array_r4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_r8.c
===================================================================
--- generated/product_r8.c	(Revision 250720)
+++ generated/product_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_r8 (gfc_array_r8 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_r8 (gfc_array_r8 * const restrict retarra
   GFC_REAL_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_r8 (gfc_array_r8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_r8 (gfc_array_r8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_c10.c
===================================================================
--- generated/sum_c10.c	(Revision 250720)
+++ generated/sum_c10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_c10 (gfc_array_c10 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_c10 (gfc_array_c10 * const restrict retarray,
   GFC_COMPLEX_10 * restrict dest;
   const GFC_COMPLEX_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_c10 (gfc_array_c10 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_c10 (gfc_array_c10 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_c16.c
===================================================================
--- generated/sum_c16.c	(Revision 250720)
+++ generated/sum_c16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_c16 (gfc_array_c16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_c16 (gfc_array_c16 * const restrict retarray,
   GFC_COMPLEX_16 * restrict dest;
   const GFC_COMPLEX_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_c16 (gfc_array_c16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_c16 (gfc_array_c16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_c4.c
===================================================================
--- generated/sum_c4.c	(Revision 250720)
+++ generated/sum_c4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_c4 (gfc_array_c4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_c4 (gfc_array_c4 * const restrict retarray,
   GFC_COMPLEX_4 * restrict dest;
   const GFC_COMPLEX_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_c4 (gfc_array_c4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_c4 (gfc_array_c4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_c8.c
===================================================================
--- generated/sum_c8.c	(Revision 250720)
+++ generated/sum_c8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_c8 (gfc_array_c8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_c8 (gfc_array_c8 * const restrict retarray,
   GFC_COMPLEX_8 * restrict dest;
   const GFC_COMPLEX_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_c8 (gfc_array_c8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_c8 (gfc_array_c8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i1.c
===================================================================
--- generated/sum_i1.c	(Revision 250720)
+++ generated/sum_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i1 (gfc_array_i1 * const restrict retarray,
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i16.c
===================================================================
--- generated/sum_i16.c	(Revision 250720)
+++ generated/sum_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i16 (gfc_array_i16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i16 (gfc_array_i16 * const restrict retarray,
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i16 (gfc_array_i16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i16 (gfc_array_i16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i2.c
===================================================================
--- generated/sum_i2.c	(Revision 250720)
+++ generated/sum_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i2 (gfc_array_i2 * const restrict retarray,
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i4.c
===================================================================
--- generated/sum_i4.c	(Revision 250720)
+++ generated/sum_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i4 (gfc_array_i4 * const restrict retarray,
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i8.c
===================================================================
--- generated/sum_i8.c	(Revision 250720)
+++ generated/sum_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i8 (gfc_array_i8 * const restrict retarray,
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_r10.c
===================================================================
--- generated/sum_r10.c	(Revision 250720)
+++ generated/sum_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_r10 (gfc_array_r10 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_r10 (gfc_array_r10 * const restrict retarray,
   GFC_REAL_10 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_r10 (gfc_array_r10 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_r10 (gfc_array_r10 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_r16.c
===================================================================
--- generated/sum_r16.c	(Revision 250720)
+++ generated/sum_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_r16 (gfc_array_r16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_r16 (gfc_array_r16 * const restrict retarray,
   GFC_REAL_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_r16 (gfc_array_r16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_r16 (gfc_array_r16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_r4.c
===================================================================
--- generated/sum_r4.c	(Revision 250720)
+++ generated/sum_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_r4 (gfc_array_r4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_r4 (gfc_array_r4 * const restrict retarray,
   GFC_REAL_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_r4 (gfc_array_r4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_r4 (gfc_array_r4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_r8.c
===================================================================
--- generated/sum_r8.c	(Revision 250720)
+++ generated/sum_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_r8 (gfc_array_r8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_r8 (gfc_array_r8 * const restrict retarray,
   GFC_REAL_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_r8 (gfc_array_r8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_r8 (gfc_array_r8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);

[-- Attachment #3: dim_sum_1.f90 --]
[-- Type: text/x-fortran, Size: 836 bytes --]

! { dg-do  run }
! { dg-shouldfail "Dim argument incorrect in SUM intrinsic: is 5, should be between 1 and 2" }

program summation

  integer, parameter :: n1=5, n2=7
  integer, dimension(1:n1,1:n2) :: arr
  integer, dimension(1:n1) :: r2
  integer, dimension(1:n2) :: r1
  integer :: i,j
  character(len=80) :: c1, c2
  character(len=50) :: fmt = '(10I5)'
  do j=1,n2
     do i=1,n1
        arr(i,j) = i+j*10
     end do
  end do
  
  r1 = sum(arr,dim=1)
  write (unit=c2, fmt=fmt) r1
  call print_sum(1,c1)
  if (c1 /= c2) call abort
  r2 = sum(arr,dim=2)
  write (unit=c2, fmt=fmt) r2
  call print_sum(2,c1)
  if (c1 /= c2) call abort
  call print_sum(5,c1)

contains

  subroutine print_sum(d, c)
    integer, intent(in) :: d
    character(len=80), intent(out) :: c
    write (unit=c, fmt=fmt) sum(arr,dim=d)
   end subroutine

end

[-- Attachment #4: dim_sum_2.f90 --]
[-- Type: text/x-fortran, Size: 872 bytes --]

! { dg-do  run }
! { dg-shouldfail "Dim argument incorrect in SUM intrinsic: is 5, should be between 1 and 2" }

program summation

  integer, parameter :: n1=5, n2=7
  integer, dimension(1:n1,1:n2) :: arr
  integer, dimension(1:n1) :: r2
  integer, dimension(1:n2) :: r1
  integer :: i,j
  character(len=80) :: c1, c2
  character(len=50) :: fmt = '(10I5)'
  do j=1,n2
     do i=1,n1
        arr(i,j) = i+j*10
     end do
  end do
  
  r1 = sum(arr,dim=1,mask=arr>23)
  write (unit=c2, fmt=fmt) r1
  call print_sum(1,c1)
  if (c1 /= c2) call abort
  r2 = sum(arr,dim=2,mask=arr>23)
  write (unit=c2, fmt=fmt) r2
  call print_sum(2,c1)
  if (c1 /= c2) call abort
  call print_sum(5,c1)

contains

  subroutine print_sum(d, c)
    integer, intent(in) :: d
    character(len=80), intent(out) :: c
    write (unit=c, fmt=fmt) sum(arr,dim=d,mask=arr>23)
   end subroutine

end

[-- Attachment #5: dim_sum_3.f90 --]
[-- Type: text/x-fortran, Size: 872 bytes --]

! { dg-do  run }
! { dg-shouldfail "Dim argument incorrect in SUM intrinsic: is 5, should be between 1 and 2" }

program summation

  integer, parameter :: n1=5, n2=7
  integer, dimension(1:n1,1:n2) :: arr
  integer, dimension(1:n1) :: r2
  integer, dimension(1:n2) :: r1
  integer :: i,j
  character(len=80) :: c1, c2
  character(len=50) :: fmt = '(10I5)'
  do j=1,n2
     do i=1,n1
        arr(i,j) = i+j*10
     end do
  end do
  
  r1 = sum(arr,dim=1,mask=.true.)
  write (unit=c2, fmt=fmt) r1
  call print_sum(1,c1)
  if (c1 /= c2) call abort
  r2 = sum(arr,dim=2,mask=.true.)
  write (unit=c2, fmt=fmt) r2
  call print_sum(2,c1)
  if (c1 /= c2) call abort
  call print_sum(5,c1)

contains

  subroutine print_sum(d, c)
    integer, intent(in) :: d
    character(len=80), intent(out) :: c
    write (unit=c, fmt=fmt) sum(arr,dim=d,mask=.true.)
   end subroutine

end

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [ patch, libfortran] Fix PR 81581
  2017-07-30 18:55 [ patch, libfortran] Fix PR 81581 Thomas Koenig
@ 2017-07-31  2:25 ` Jerry DeLisle
  0 siblings, 0 replies; 2+ messages in thread
From: Jerry DeLisle @ 2017-07-31  2:25 UTC (permalink / raw)
  To: Thomas Koenig, fortran, gcc-patches

On 07/30/2017 11:55 AM, Thomas Koenig wrote:
> Hello world,
> 
> the attached patch performs a runtime-check for the dim arguments
> for transformational intrinsic functions.
> 
> I debated with myself a bit if I should make this depend on
> bounds checking, but I finally decided against it.  The tests
> performed are extemely cheap, the values are still in registers,
> so any runtime overhead should be small. By contrast, loading
> something from an external structure is probably more expensive
> (but I didn't bother actually check).
> 
> While visiting the code, I also took the opportunity to make
> the rank and dim variables in the 'm' class of functions to index_type,
> to make their use consistent.
> 
> Regarding the test case names, I was unable to resist.
> 
> Regression-tested. OK for trunk?
> 

Looks OK, thanks,

Jerry

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-07-31  2:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-30 18:55 [ patch, libfortran] Fix PR 81581 Thomas Koenig
2017-07-31  2:25 ` Jerry DeLisle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).