Fix heap overflow assigning CLASS array to TYPE array
Corrects array assignment to prevent heap overflows when assigning CLASS arrays to TYPE arrays.
This commit resolves a heap overflow issue in GCC’s Fortran compiler when assigning a CLASS array to an allocatable array of a specific TYPE. The problem occurred because the reallocation for the target array incorrectly used the size of the CLASS container instead of the declared TYPE’s element size. This led to overflows for larger types or incorrect sizing for smaller ones. The fix ensures that the element size is correctly taken from the declared type of the CLASS expression during assignment.
In Details
When assigning a CLASS array to an allocatable array of a declared TYPE in gfc_alloc_allocatable_for_assignment, the reallocation logic incorrectly determined the element size. It used the size of the CLASS container itself, rather than the size of the declared TYPE of the CLASS expression. This resulted in heap overflows if the declared TYPE was larger than the container, or incorrect allocation sizes if it was smaller. The patch corrects this by ensuring the element size is derived from the declared type of the CLASS expression.
- CLASS array
- In Fortran, an array whose elements are of a polymorphic type, meaning they can hold values of different derived types at runtime.
- TYPE array
- In Fortran, an array whose elements are of a specific, non-polymorphic derived type.
- allocatable
- A Fortran attribute that allows a variable (typically an array or derived type component) to be dynamically allocated and deallocated during program execution.
- heap overflow
- A type of buffer overflow that occurs when a program writes data beyond the allocated buffer in the heap memory, potentially corrupting adjacent data or causing a crash.