https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64921
--- Comment #17 from Uroš Bizjak <ubizjak at gmail dot com> --- (In reply to Mat Cross from comment #14) > For the record, perhaps it is of interest for me to note that we are running > into this (cf. PR64230 comment 9) on code like > > Program test > Implicit None > Type :: t1 > Integer, Allocatable :: i > End Type > Type :: t2 > Integer, Allocatable :: i > End Type > Type, Extends (t1) :: t3 > Type (t2) :: j > End Type > Type, Extends (t3) :: t4 > Integer, Allocatable :: k > End Type > Call s > Print *, 'ok' > Contains > Subroutine s > Class (t1), Allocatable :: x > Allocate (t4 :: x) > End Subroutine > End Program > > Since the crash is in bad compiler-generated finalization code (since 4.9), > and given that (if I recall correctly) gfortran is using the Fortran 2008 > semantics for entities declared in a main program being implicitly saved, > this is why removing the Deallocate (in the comment 12 example) works - the > finalizer is never called then. No wonder this test crashes. Tree-optimizers (-O2) on x86_64 produce: --cut here-- test () { integer(kind=8)[0:D.4089] * restrict sizes; integer(kind=8)[0:D.4068] * restrict sizes; void * _13; integer(kind=4) * _63; integer(kind=4) * _121; <bb 2>: _13 = __builtin_malloc (24); if (_13 == 0B) goto <bb 3>; else goto <bb 4>; <bb 3>: _gfortran_os_error (&"Allocation would exceed memory limit"[1]{lb: 1 sz: 1}); <bb 4>: MEM[(c_char * {ref-all})_13] = MEM[(c_char * {ref-all})&__def_init_test_T4]; sizes_22 = __builtin_malloc (8); MEM[(integer(kind=8)[0:D.3483] *)sizes_22][0] = 1; _63 = MEM[(struct t4 *)_13].k; if (_63 == 0B) goto <bb 6>; else goto <bb 5>; <bb 5>: __builtin_free (_63); <bb 6>: sizes_79 = __builtin_malloc (8); _121 ={v} MEM[(struct t3 *)0B].j.i; __builtin_trap (); } --cut here-- The <bb 6>: part reads from address 0x0+, and if this doesn't crash, trap insn surely crashes program.