Fix invalid free in Fortran CLASS finalization wrapper
Corrects an invalid memory free when deallocating CLASS objects with specific nested derived type components in Fortran.
A bug in the Fortran compiler’s handling of CLASS objects with non-allocatable derived type array components, which themselves contained allocatable components, led to an invalid memory free during finalization. The compiler was incorrectly attempting to deallocate components of the last component’s type for all array elements. This commit modifies the finalization wrapper to skip this redundant deallocation for such structures, resolving the invalid free and potential memory corruption.
In Details
The gfc_trans_deallocate function in gcc/fortran/trans-stmt.cc had a flaw when handling the finalization of CLASS objects. For a specific case involving a non-allocatable array component of a derived type, which itself contained allocatable components, the function would incorrectly apply the generic allocatable-component cleanup to the array elements. This commit modifies gfc_trans_deallocate to skip this generic cleanup when finalize_component has generated a non-allocatable array component expression, thus preventing the invalid free.
- CLASS object
- In Fortran, an object whose dynamic type can change during execution. It is associated with polymorphic variables.
- finalization wrapper
- A piece of generated code that handles the cleanup and deallocation of objects, particularly when they go out of scope or are explicitly deallocated.
- allocatable component
- A component within a derived type that can be dynamically allocated and deallocated using ALLOCATE and DEALLOCATE statements.
- derived type
- A user-defined data type in Fortran, similar to a struct or class in other languages.
- gfc_trans_deallocate
- The GCC internal function responsible for translating the Fortran DEALLOCATE statement into RTL.