]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: initialize non-saved pointers with -fcheck=pointer [PR48958]
authorHarald Anlauf <anlauf@gmx.de>
Thu, 20 Feb 2025 20:22:56 +0000 (21:22 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 21 Feb 2025 08:30:05 +0000 (09:30 +0100)
PR fortran/48958

gcc/fortran/ChangeLog:

* trans-array.cc (gfc_trans_deferred_array): Initialize the data
component of non-saved pointers when -fcheck=pointer is set.

gcc/testsuite/ChangeLog:

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

gcc/fortran/trans-array.cc
gcc/testsuite/gfortran.dg/pointer_init_13.f90 [new file with mode: 0644]

index 92e933add8a84135cb23bb20e71e156afab39806..8f76870b286ad2e528c8a3c5145edb47af83b9c2 100644 (file)
@@ -12123,8 +12123,11 @@ gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block)
       type = TREE_TYPE (descriptor);
     }
 
-  /* NULLIFY the data pointer, for non-saved allocatables.  */
-  if (GFC_DESCRIPTOR_TYPE_P (type) && !sym->attr.save && sym->attr.allocatable)
+  /* NULLIFY the data pointer for non-saved allocatables, or for non-saved
+     pointers when -fcheck=pointer is specified.  */
+  if (GFC_DESCRIPTOR_TYPE_P (type) && !sym->attr.save
+      && (sym->attr.allocatable
+         || (sym->attr.pointer && (gfc_option.rtcheck & GFC_RTCHECK_POINTER))))
     {
       gfc_conv_descriptor_data_set (&init, descriptor, null_pointer_node);
       if (flag_coarray == GFC_FCOARRAY_LIB && sym->attr.codimension)
diff --git a/gcc/testsuite/gfortran.dg/pointer_init_13.f90 b/gcc/testsuite/gfortran.dg/pointer_init_13.f90
new file mode 100644 (file)
index 0000000..ece423a
--- /dev/null
@@ -0,0 +1,24 @@
+! { dg-do compile }
+! { dg-options "-fcheck=pointer -fdump-tree-optimized -O -fno-inline" }
+!
+! PR fortran/48958
+!
+! Initialize non-saved pointers with -fcheck=pointer to support runtime checks
+! of uses of possibly undefined pointers
+
+program p
+  implicit none
+  call s
+contains
+  subroutine s
+    integer, pointer :: a(:)
+    integer, pointer :: b(:) => NULL()
+    if (size (a) /= 0) stop 1
+    if (size (b) /= 0) stop 2
+    print *, size (a)
+    print *, size (b)
+  end
+end
+
+! { dg-final { scan-tree-dump-times "_gfortran_runtime_error_at" 1 "optimized" } }
+! { dg-final { scan-tree-dump-not "_gfortran_stop_numeric" "optimized" } }