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

            Bug ID: 120656
           Summary: SEGV with a Function Returning a TYPE with ALLOCATABLE
                    Component
           Product: gcc
           Version: 15.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gilbert.scott.99 at googlemail dot com
  Target Milestone: ---

$ cat test.f95

PROGRAM TEST
IMPLICIT NONE

TYPE :: FOO
   INTEGER :: I
   INTEGER, ALLOCATABLE :: N(:)
END TYPE FOO

TYPE(FOO), ALLOCATABLE :: F1(:)
TYPE(FOO), ALLOCATABLE :: F2(:)
TYPE(FOO) :: FX

ALLOCATE ( F1(0) )
ALLOCATE ( F2(0) )
FX = FOO_NEW(1)
F1 = [F1,FX]
PRINT *, 'SIZE(F1) = ',SIZE(F1)
F2 = [F2,FOO_NEW(2)]
PRINT *, 'SIZE(F2) = ',SIZE(F2)

CONTAINS

FUNCTION FOO_NEW(I)
TYPE(FOO) :: FOO_NEW
INTEGER, INTENT(IN) :: I
FOO_NEW%I = I
END FUNCTION FOO_NEW

END PROGRAM TEST

$ gfortran --version
GNU Fortran (GCC) 15.1.1 20250521 (Red Hat 15.1.1-2)

$ gfortran -std=2003 -Wall -g -o test test.f95
$ ./test
 SIZE(F1) =            1

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7fea2ac09055 in ???
#1  0x7fea2ac08045 in ???
#2  0x7fea2a939c2f in ???
#3  0x7fea2a9a24c4 in ???
#4  0x401fb1 in test
        at ./test.f95:19
#5  0x4020c6 in main
        at ./test.f95:20
Segmentation fault (core dumped)

------------------------------------------------------------------
The problem is on this line:
F2 = [F2,FOO_NEW(2)]

There is no problem if the ALLOCATABLE component of type FOO is removed.

Reply via email to