The following code aborts with a segfault (invalid memory reference) upon call of the apply function. This happens with gfortran 9.1.1 as well as a recent gfortran-10. module cs implicit none private public classStar_map_ifc public fun, apply abstract interface function classStar_map_ifc(x) result(y) class(*), pointer :: y class(*), target, intent(in) :: x end function classStar_map_ifc end interface contains function fun(x) result(y) class(*), pointer :: y class(*), target, intent(in) :: x select type (x) type is (integer) y => x class default y => null() end select end function fun function apply(f, x) result(y) procedure(classStar_map_ifc) :: f integer, intent(in) :: x integer :: y class(*), pointer :: p p => f(x) select type (p) type is (integer) y = p end select end function apply end module cs program classStar_map2 use cs implicit none integer :: x, y procedure(classStar_map_ifc), pointer :: f x = 123654 y = apply(fun, x) print *, x, y end program classStar_map2
The test case violates, 15.5.2.4 Ordinary dummy variables, subsection 11 of the F2018 standard: If the dummy argument has the TARGET attribute and the effective argument does not have the TARGET attribute or is an array section with a vector subscript, any pointers associated with the dummy argument become undefined when execution of the procedure completes. fun returns an undefined pointer, so anything can happen (including a segfault). nagfor catches this, by the way: Runtime Error: 93925.f90, line 36: Reference to dangling pointer P Target was RETURNed from procedure F
Created attachment 50048 [details] classStar_map2 with pointer attribute for variable x
Created attachment 50049 [details] classStar_map3 with class(*), pointer for variable x
Created attachment 50050 [details] classStar_map4 without target nor pointer attributes for variable x
Sorry for this invalid test case, obviously I did reduce too much. The three attached variations should hopefully all be conforming to the standard and still produce the same error. Please reopen if the testcases are ok now. The attached classStar_map2 is a slight variation on the original code, and just uses an "integer, pointer" variable for x (as is roughly the context where I originally encountered the bug). classStar_map3 has no target attribute but uses "class(*), pointer" for variable as well as argument x. classStar_map4 is a bit further reduced. It has no target nor pointer attribute for x, but does not use the value of x, just returns y=>null() and y=987456.
OK, these test cases look valid. Reopening.
Ah, go on! It's on my tree - I'll commit as obvious. Paul
The master branch has been updated by Paul Thomas <pault@gcc.gnu.org>: https://gcc.gnu.org/g:4225af228b5d52e8d7593dea49584c91b4bc2e1f commit r11-6928-g4225af228b5d52e8d7593dea49584c91b4bc2e1f Author: Paul Thomas <pault@gcc.gnu.org> Date: Wed Jan 27 11:34:02 2021 +0000 Fortran: Fix ICE due to elemental procedure pointers [PR93924/5]. 2021-01-27 Paul Thomas <pault@gcc.gnu.org> gcc/fortran PR fortran/93924 PR fortran/93925 * trans-expr.c (gfc_conv_procedure_call): Suppress the call to gfc_conv_intrinsic_to_class for unlimited polymorphic procedure pointers. (gfc_trans_assignment_1): Similarly suppress class assignment for class valued procedure pointers. gcc/testsuite/ PR fortran/93924 PR fortran/93925 * gfortran.dg/proc_ptr_52.f90 : New test.
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:bf22e268b5c2a03216503254cfcb828016a7d998 commit r10-10077-gbf22e268b5c2a03216503254cfcb828016a7d998 Author: Paul Thomas <pault@gcc.gnu.org> Date: Wed Jan 27 11:34:02 2021 +0000 Fortran: Fix ICE due to elemental procedure pointers [PR93924/5]. 2021-01-27 Paul Thomas <pault@gcc.gnu.org> gcc/fortran PR fortran/93924 PR fortran/93925 * trans-expr.c (gfc_conv_procedure_call): Suppress the call to gfc_conv_intrinsic_to_class for unlimited polymorphic procedure pointers. (gfc_trans_assignment_1): Similarly suppress class assignment for class valued procedure pointers. gcc/testsuite/ PR fortran/93924 PR fortran/93925 * gfortran.dg/proc_ptr_52.f90 : New test. (cherry picked from commit 4225af228b5d52e8d7593dea49584c91b4bc2e1f)
Cherry-picked to 10-branch for Paul after verifying that this fixes the issue.
The releases/gcc-9 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:49cefbec30499da06f90912090bcc5eabdfefa32 commit r9-9710-g49cefbec30499da06f90912090bcc5eabdfefa32 Author: Paul Thomas <pault@gcc.gnu.org> Date: Wed Jan 27 11:34:02 2021 +0000 Fortran: Fix ICE due to elemental procedure pointers [PR93924/5]. 2021-01-27 Paul Thomas <pault@gcc.gnu.org> gcc/fortran PR fortran/93924 PR fortran/93925 * trans-expr.c (gfc_conv_procedure_call): Suppress the call to gfc_conv_intrinsic_to_class for unlimited polymorphic procedure pointers. (gfc_trans_assignment_1): Similarly suppress class assignment for class valued procedure pointers. gcc/testsuite/ PR fortran/93924 PR fortran/93925 * gfortran.dg/proc_ptr_52.f90 : New test. (cherry picked from commit 4225af228b5d52e8d7593dea49584c91b4bc2e1f)
Should be fixed on all open branches. Closing. Thanks for the report!