fortran: Fix segfault in derived-type assignment to a pointer LHS
Resolves a segfault in Fortran derived-type assignment when the left-hand side is a pointer.
This commit fixes a segfault occurring in GFortran when performing derived-type assignment where the left-hand side (LHS) is a pointer. The issue arose when a component of the derived type had a defined assignment routine that used INTENT(INOUT) or INTENT(OUT) with finalizable arguments. In such cases, generate_component_assignments would copy the LHS to a temporary. If the LHS was a scalar pointer, this temporary also became a pointer but was not allocated, leading to a write through a null pointer. The fix prevents scalar temporaries from receiving the POINTER attribute, resolving the segfault.
In Details
The generate_component_assignments function in GFortran handles assignment to components of derived types, including cases where the component itself has a defined assignment. When the primary assignment's LHS is a scalar pointer and a component's defined assignment requires INTENT(INOUT) or INTENT(OUT) with finalization, generate_component_assignments creates a temporary for the LHS. This temporary incorrectly inherited the POINTER attribute, and since it was unallocated, assignments to it resulted in a null pointer dereference. The change in resolve.cc ensures that such scalar t…
- Derived type
- A user-defined composite data type in Fortran, similar to a struct or record in other languages.
- Pointer
- A variable in Fortran that holds the memory address of another data item.
- LHS
- Left-Hand Side; the target of an assignment operation.
- INTENT(INOUT)
- A Fortran argument intent specifier indicating that the argument can be passed into the procedure and its value can be modified by the procedure.
- Finalizable
- Describes a derived type or component that has an associated finalization procedure which is executed when the variable goes out of scope or is no longer needed.