https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128735
Bug ID: 128735
Summary: [OOP] END ASSOCIATE restores a CLASS array selector's
old descriptor
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: jvdelisle at gcc dot gnu.org
Target Milestone: ---
If the selector of ASSOCIATE (or SELECT TYPE) is a polymorphic array
pointer or allocatable, END ASSOCIATE copies the selector's descriptor
from before the block back into it. This undoes a pointer assignment
to the selector made inside the block:
program p
class(*), pointer :: q(:)
integer, target :: a(2) = [1, 2], b(3) = [3, 4, 5]
q => a
associate (x => q)
q => b
end associate
print *, size (q) ! expect 3
end program
gfortran prints 2; flang prints 3. For an allocatable selector it
brings back a freed array, so this gives a double free:
program p
call s
contains
subroutine s
class(*), allocatable :: q(:)
allocate (q, source=[1, 2])
associate (x => q)
deallocate (q)
end associate
end subroutine
end program
free(): double free detected in tcache 2
CLASS(*) and CLASS(t) selectors, both variables and components (obj%p),
are affected, with or without a SELECT TYPE on the associate name.
Scalar CLASS selectors and non-polymorphic arrays are not affected.
trans_associate_var (trans-stmt.cc) makes a temporary class container
for the associate name with gfc_conv_class_to_class (..., copyback=true).
-fdump-tree-original for the first example shows the copy-back:
finally
{
q._data = class.0._data;
q._vptr = class.0._vptr;
q._len = class.0._len;
q._len = class.0._len;
}
An associate name is neither POINTER nor ALLOCATABLE (F2023 11.1.3.3;
see PR54322), so the block cannot change _data or _vptr through it,
and no copy-back is needed.