https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100814

            Bug ID: 100814
           Summary: Fortran memory error on assignment from polymorphic
                    variable
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jhaiduce at gmail dot com
  Target Milestone: ---

The following code produces a memory error when compiled with recent versions
of gfortran:
```
module distributed_array

  implicit none

  type :: darray_segment
    integer::rank
    integer::offset
    integer::length
    real(kind=8), allocatable::data(:)
  contains
  end type darray_segment

  type :: darray
    type(darray_segment), allocatable::segments(:)
  end type darray

contains

  function new_darray(segments)
    class(darray_segment), intent(in)::segments(:)
    type(darray)::new_darray

    new_darray%segments = segments

  end function new_darray

end module distributed_array

program test_darray

  use distributed_array, ONLY: darray, darray_segment, new_darray

  implicit none

  integer, parameter::np_src = 4
  integer, parameter::np_dest = 3
  type(darray)::src_darray
  type(darray)::dest_darray

  type(darray_segment)::src_segments(np_src)
  type(darray_segment)::dest_segments(np_dest)

  src_darray = new_darray(src_segments)
  dest_darray = new_darray(dest_segments)

end program test_darray
```

The above code runs without error when compiled with gfortran 4.9.4 and 10.2,
but when compiled with gfortran 10.3 and 11.1 it produces the following output:
```
darray_test: malloc.c:2385: sysmalloc: Assertion `(old_top == initial_top (av)
&& old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse
(old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.

Program received signal SIGABRT: Process abort signal.

Backtrace for this error:
#0  0x7f727c59fbf0 in ???
#1  0x7f727c59ee45 in ???
#2  0x7f727c20d83f in ???
    at
/build/glibc-vjB4T1/glibc-2.28/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
#3  0x7f727c20d7bb in __GI_raise
    at ../sysdeps/unix/sysv/linux/raise.c:51
#4  0x7f727c1f8534 in __GI_abort
    at /build/glibc-vjB4T1/glibc-2.28/stdlib/abort.c:79
#5  0x7f727c255a67 in __malloc_assert
    at /build/glibc-vjB4T1/glibc-2.28/malloc/malloc.c:298
#6  0x7f727c257e6e in sysmalloc
    at /build/glibc-vjB4T1/glibc-2.28/malloc/malloc.c:2382
#7  0x7f727c2592c8 in _int_malloc
    at /build/glibc-vjB4T1/glibc-2.28/malloc/malloc.c:4133
#8  0x7f727c25a3e2 in __GI___libc_malloc
    at /build/glibc-vjB4T1/glibc-2.28/malloc/malloc.c:3049
#9  0x401f10 in __distributed_array_MOD_new_darray
    at /test/src/test/darray_tests.F90:23
#10  0x402933 in test_darray
    at /test/src/test/darray_tests.F90:44
#11  0x402aaf in main
    at /test/src/test/darray_tests.F90:31

```

The code also runs without error when compiled with ifort 2021.2.

The problem appears to be triggered by the assignment `new_darray%segments =
segments`. The error can be prevented by changing the declaration
`class(darray_segment), intent(in)::segments(:)` to `type(darray_segment),
intent(in)::segments(:)` (replacing class with type), or by replacing the
assignment with an explicit allocation: `allocate( new_darray%segments, source=
segments )`.

Reply via email to