GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
fortran

Fortran: Fix incorrect code generation for allocatable character array components

Corrects an issue where copying allocatable character arrays with differing lengths resulted in corrupted data.

A bug in Fortran’s code generation for structure constructors has been fixed. When copying an allocatable character array into an explicit-length allocatable character array component, the previous implementation used a bitwise copy which failed when source and destination element lengths differed. This change replaces the bitwise copy with an element-by-element string copy, ensuring correct padding or truncation.

In Details

The gfc_duplicate_allocatable function was incorrectly used for assigning explicit-length allocatable character array components. This function performs a bitwise copy, which is unsuitable when lengths differ. The fix introduces gfc_char_elem_addr and gfc_trans_alloc_subarray_assign to use gfc_trans_string_copy for proper element-wise copying, maintaining the integrity of character data across differing lengths.

For Context
allocatable character array
A Fortran array whose size and shape can be determined at runtime. 'Character' specifies the data type as text.
structure constructor
A Fortran construct used to initialize or create values for derived types (structures), similar to object initialization in other languages.
gfc_duplicate_allocatable
A GCC internal function for copying allocatable Fortran variables, which performs a raw memory copy.
gfc_trans_string_copy
A GCC internal function for safely copying Fortran character strings, handling potential differences in length and ensuring proper padding or truncation.
Filed Under: fortrancompiler bugcode generation