https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83763
Bug ID: 83763
Summary: PDT variable sees content deallocated if variable is
passed as an input to a function, and the function
result is assigned to that same variable
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: berke.durak at gmail dot com
Target Milestone: ---
% cat test_case.f90
module bar
implicit none
type :: foo(n)
integer, len :: n=10
real :: vec(n)
end type foo
contains
function baz(a) result(b)
type(foo(n=*)), intent(in) :: a
type(foo(n=a%n)) :: b
b%vec(1)=a%vec(1)
end function baz
end module bar
program test
use bar
implicit none
call main
contains
subroutine main
!type(foo(n)) :: x,y
type(foo) :: x!,y
x%vec=0
x=baz(x) ! Segmentation fault
!x=baz(y) ! THIS WORKS
end subroutine main
end program test
% /usr/local/gfortran-bin/bin/gfortran test_case.f90 -o tc -Wall -Wextra -g -O0
-fdump-tree-all
% ./tc
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x7f455c722aef in ???
#1 0x4008f2 in __bar_MOD_baz
at /home/alpha/src/fortran/24_Autoder_with_PDTs/test_case.f90:15
#2 0x4009ec in main
at /home/alpha/src/fortran/24_Autoder_with_PDTs/test_case.f90:32
zsh: segmentation fault ./tc
Looking at some intermediate representation, it seems to me that x%vec
is deallocated just before calling the function baz(), probably because the
result will be assigned to x:
% cat test_case.f90.088t.fixup_cfg4
...
<bb 5> :
_2 = x.vec.data;
if (_2 != 0B)
goto <bb 6>; [INV]
else
goto <bb 7>; [INV]
<bb 6> :
_3 = x.vec.data;
__builtin_free (_3);
<bb 7> :
x.vec.data = 0B;
D.3841 = baz (&x); [return slot optimization]
...
% /usr/local/gfortran-bin/bin/gfortran --version
GNU Fortran (GCC) 8.0.0 20180109 (experimental) [trunk revision 256361]
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.