https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125117
Bug ID: 125117
Summary: [16 regression] Assigning character(len=N) component
to character(:), allocatable function result loses
conten
Product: gcc
Version: 16.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: matthew.thompson at nasa dot gov
Target Milestone: ---
GCC version:
gcc version 16.1.0 (GCC)
System type:
Target: x86_64-pc-linux-gnu
Configure options:
../configure --prefix=/ford1/share/gmao_SIteam/gcc/gcc-16.1.0
--enable-languages=c,c++,fortran --disable-multilib
Threads: posix
Complete compilation command:
gfortran -O0 -o bug gcc16_allocatable_char_bug.f90
Compiler output:
(none -- compiles cleanly with no warnings or errors)
Program output (wrong):
FAIL: got=[ ]
Expected output (verified correct with GCC 15.2.0):
PASS
Note: The testcase does not include any other files, so no preprocessed
.i file is needed per the GCC bug reporting guidelines.
--------------------------------------------------------------------
DESCRIPTION
In a function with a character(:), allocatable result, assigning a
character(len=N) dummy argument to the result variable silently produces
a string of spaces instead of copying the actual content. The allocatable
is allocated to the correct length but the content is not transferred.
This is a regression: GCC 15.2.0 produces the correct result.
--------------------------------------------------------------------
TESTCASE (gcc16_allocatable_char_bug.f90)
program test
implicit none
character(len=64) :: fixed
character(:), allocatable :: got
fixed = 'hello world'
got = getName(fixed)
if (trim(got) == 'hello world') then
print *, 'PASS'
else
print *, 'FAIL: got=[', trim(got), ']'
end if
contains
function getName(fixed) result(name)
character(len=64), intent(in) :: fixed
character(:), allocatable :: name
name = fixed
end function
end program
--------------------------------------------------------------------
ANALYSIS
The bug is in this single line inside the function:
name = fixed
where "fixed" is character(len=64) and "name" is character(:), allocatable.
GCC 16.1.0 allocates the result to length 64 but fails to copy the string
content, leaving it as spaces.
WORKAROUND
name = trim(fixed)
Using trim() on the right-hand side works correctly on both GCC 15 and GCC 16.
--------------------------------------------------------------------
ADDITIONAL CONTEXT
This bug was discovered via a test failure in pFUnit v4.18.0
(https://github.com/Goddard-Fortran-Ecosystem/pFUnit/issues/548).
A workaround fix has been proposed upstream at:
https://github.com/Goddard-Fortran-Ecosystem/pFUnit/pull/549