User account creation filtered due to spam.

Bug 47517 - [F03] Reallocate on assignment: Segfault
Summary: [F03] Reallocate on assignment: Segfault
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2011-01-28 15:18 UTC by Tobias Burnus
Modified: 2013-02-04 22:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-02-07 05:32:21


Attachments
James' allocate_assign.f90 (552 bytes, text/plain)
2011-01-28 15:18 UTC, Tobias Burnus
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2011-01-28 15:18:23 UTC
Created attachment 23154 [details]
James' allocate_assign.f90

Follow up to bug 35810 comment 0.

The following program ends with a segfault and works with ifort (-assume realloc_lhs). I do not know whether the program is valid.

Cf. also http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/7d5d1e7f4a48071c/7c63fef65ba5ea70?lnk=gst&q=realloc_lhs

The attached program - which is James program from c.l.f except that I drastically reduced the loop count and removed some line wraps, causes a segfault in gfortran.

[...]
x2(3)%headers(5)%parts = 1,2,3,4,5,6
size(x2(3)%headers(6)%parts) = 6
x2(3)%headers(6)%parts = 1,2,3,4,5,6
           1                    0
Segmentation fault (core dumped)


Result with ifort -assume realloc_lhs:
x2(3)%headers(5)%parts = 1,2,3,4,5,6
size(x2(3)%headers(6)%parts) = 6
x2(3)%headers(6)%parts = 1,2,3,4,5,6
           1                     0
           1                     0
           1                     2
[...]
       10000                 39998
       10000                 39998
                 40000
Comment 1 Paul Thomas 2011-02-07 05:32:21 UTC
(In reply to comment #0)
This somewhat reduced version of James testcase retains the segfault in bar.  Now to understand what happens in foo!

Confirmed

Paul

>program allocate_assign
   implicit none
   type label
      integer, allocatable :: parts(:)
   end type label
   type table
      type(label), allocatable :: headers(:)
   end type table
   integer, parameter :: ik8 = selected_int_kind(18)
   type(table) x1(1)
   type(table) t1
   type(table), allocatable :: x(:)
   integer k
   integer(ik8) s
   t1 = table([label([1])])
   x1 = [t1]
   s = 0
   do k = 1, 2
      write(*,*) "1> ", k,s
      call foo
      write(*,*) "2> ",k,s, size (x)
      call bar
   end do
   write(*,*) s
contains
   subroutine foo
      x = x1
   end subroutine
   subroutine bar
      s = s+x(2)%headers(2)%parts(2)
   end subroutine
end program allocate_assign
Comment 2 Paul Thomas 2013-02-04 22:33:19 UTC
Author: pault
Date: Mon Feb  4 22:33:15 2013
New Revision: 195741

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195741
Log:
2013-02-04  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/56008
	PR fortran/47517
	* trans-array.c (gfc_alloc_allocatable_for_assignment): Save
	the lhs descriptor before it is modified for reallocation. Use
	it to deallocate allocatable components in the reallocation
	block.  Nullify allocatable components for newly (re)allocated
	arrays.

2013-02-04  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/56008
	* gfortran.dg/realloc_on _assign_16.f90 : New test.

	PR fortran/47517
	* gfortran.dg/realloc_on _assign_17.f90 : New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/realloc_on_assign_16.f90
    trunk/gcc/testsuite/gfortran.dg/realloc_on_assign_17.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Paul Thomas 2013-02-04 22:38:03 UTC
Fixed on trunk.... finally!  Thanks for the report, Tobias

Paul