[Bug fortran/85786] Segfault in associated intrinsic

2018-05-15 Thread sgk at troutmask dot apl.washington.edu
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85786

--- Comment #3 from Steve Kargl  ---
On Tue, May 15, 2018 at 04:50:41AM +, angus at agibson dot me wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85786
> 
> --- Comment #2 from Angus Gibson  ---
> Changing the declaration of e to also be 'target', and associating the 
> pointer:
> 
> CS%v(2)%p => e
> if (query_ptr(e, CS)) then
> ...
> 
> still causes the segfault.  Perhaps I disconnected this from the original code
> too much! Even if CS%v(2)%p wasn't associated, I don't see why there should be
> a segfault?

Here's a slight rewrite with debugging print statements.
Certainly, seems like you've found a bug :(


program test

   implicit none

   type :: p2d
  real, pointer :: p(:,:) => null()
   end type p2d

   type :: test_cs
  type(p2d), pointer :: v(:) => null()
   end type test_cs

   type(test_cs), pointer :: cs
   real, allocatable, target :: e(:,:)

   allocate(cs)
   print '(A,L1)', 'associated(cs) = ', associated(cs)

   allocate(cs%v(2))
   print '(A,L1)', 'associated(cs%v) = ', associated(cs%v)

   allocate(e(2,2))
   e = 42
   print '(A,I0)', 'loc(e) = ', loc(e)
   print '(A,4F6.1)', 'e = ', e

   if (query_ptr(e, cs)) then
  print *, 'associated'
   else
  print *, 'not associated'
   end if

   contains

  logical function query_ptr(f_ptr, cs)

 real, target, intent(in) :: f_ptr(:,:)
 type(test_cs), pointer, intent(inout) :: cs

 print '(A,I0)','loc(f_ptr) = ', loc(f_ptr)
 print '(A,4F6.1)', 'f_ptr = ', f_ptr

 if (associated(cs)) then
print *, 'in query'
print '(A,L1)', 'associated(cs%v) = ', associated(cs%v)
cs%v(2)%p => f_ptr
print '(A,L1)', 'associated(cs%v(2)%p) = ', associated(cs%v(2)%p)
print '(A,I0)', 'loc(cs%v(2)%p) = ', loc(cs%v(2)%p)
query_ptr = associated(cs%v(2)%p, f_ptr)
 else
query_ptr = .false.
 end if
  end function query_ptr

end program test

Pretty good indication of why I don't use pointers.

[Bug fortran/85786] Segfault in associated intrinsic

2018-05-14 Thread angus at agibson dot me
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85786

--- Comment #2 from Angus Gibson  ---
Changing the declaration of e to also be 'target', and associating the pointer:

CS%v(2)%p => e
if (query_ptr(e, CS)) then
...

still causes the segfault.  Perhaps I disconnected this from the original code
too much! Even if CS%v(2)%p wasn't associated, I don't see why there should be
a segfault?

[Bug fortran/85786] Segfault in associated intrinsic

2018-05-14 Thread kargl at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85786

kargl at gcc dot gnu.org changed:

   What|Removed |Added

 CC||kargl at gcc dot gnu.org

--- Comment #1 from kargl at gcc dot gnu.org ---
(In reply to Angus Gibson from comment #0)
> Created attachment 44132 [details]
> Segfaulting example
> 
> I've run into a bit of a heisenbug regarding the associated intrinsic
> (example attached).  On my machine, it'll reliably hit a segmentation fault
> inside the gfortran implementation of the intrinsic.  I've pared it down a
> lot from the original codebase in which it happened, but it's still a bit
> complex.  Curiously, it seems to require a print statement in query_ptr to
> trigger (this actually isn't in the original codebase, but there are a few
> external calls there that I didn't replicate).

So, what is cs%v(2)%p associated to?

if (.not.associated(CS%v(2)%p)) then
  print *, 'in query'
endif

query_ptr = associated(CS%v(2)%p, f_ptr)