Fortran: Fix double free error with parenthesized array constructors.
Corrects a double-free bug in Fortran array constructors involving allocatable components.
This commit resolves a double-free vulnerability in the Fortran frontend related to parenthesized array constructors. The issue occurred because allocatable components within such constructors were being freed twice: once when the temporary expression was handled and again when the underlying variables were processed. The fix ensures that parenthesized array constructors receive a deep copy of their temporary expression, similar to regular constructors, and that the expr_may_alias_variables analysis correctly accounts for them, preventing the erroneous double-free.
In Details
This patch addresses a memory corruption bug (double free) in gcc/fortran/trans-array.cc and gcc/fortran/trans-expr.cc. The problem arises when an allocatable component is part of an array constructor enclosed in parentheses, e.g., ( / a, b /). The temporary descriptor for this parenthesized expression was performing a shallow copy of the constructor's temporary. Since the allocatable components were still shared with the original variables, and expr_may_alias_variables did not properly look through the parentheses to detect this aliasing (especially in elemental contexts), the tempor…
- array constructor
- A Fortran construct used to create arrays, where elements are specified explicitly within slashes and delimiters (e.g.,
(/ 1, 2, 3 /)). - allocatable component
- A component of a derived type in Fortran that has been declared with the
allocatableattribute, meaning its memory is dynamically managed (allocated and deallocated). - double free
- A memory corruption error that occurs when memory is deallocated more than once. This can lead to heap corruption and program crashes.
- deep copy
- A copy operation that creates a completely independent copy of an object, including all objects it refers to. Changes to the copy do not affect the original, and vice versa.
- shallow copy
- A copy operation that creates a new object but copies references to the objects referred to by the original. Changes to the referred-to objects will affect both the original and the copy.
- elemental procedure
- In Fortran, a procedure that operates on scalar arguments and can be implicitly applied element-by-element to array arguments.