Dear all, the attached simple patch addresses a small, left-over issue in the above PR and was already OK'ed in the PR by Thomas. With it we initialize the data component of a non-saved pointer variable when -fcheck=pointer is specified, so that subsequent pointer checks (e.g. for the SIZE intrinsic) trigger consistently and not randomly.
I plan to commit within 24h unless there are comments. Regtested on x86_64-pc-linux-gnu. ON for mainline? Thanks, Harald
From eca02da7cfa3781727dfd1a0914b7d9d377b1511 Mon Sep 17 00:00:00 2001 From: Harald Anlauf <anl...@gmx.de> Date: Thu, 20 Feb 2025 21:03:12 +0100 Subject: [PATCH] Fortran: initialize non-saved pointers with -fcheck=pointer [PR48958] PR fortran/48958 gcc/fortran/ChangeLog: * trans-array.cc (gfc_trans_deferred_array): Initialized 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 | 7 ++++-- gcc/testsuite/gfortran.dg/pointer_init_13.f90 | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/pointer_init_13.f90 diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index ec627dddffd..27378c432f7 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -12122,8 +12122,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 index 00000000000..ece423a9db6 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pointer_init_13.f90 @@ -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" } } -- 2.43.0