http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53642
Bug #: 53642 Summary: Front-end optimization: Wrong string length for deferred-length strings Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: fortran AssignedTo: unassig...@gcc.gnu.org ReportedBy: bur...@gcc.gnu.org CC: dam...@rouson.net, tkoe...@gcc.gnu.org As reported by Damian Rouson at http://gcc.gnu.org/ml/fortran/2012-06/msg00069.html Without optimization, the following program prints - and should print: 3 3 However, with -ffrontend-optimize the result is, wrongly, 3 4 Damian: Use -fno-frontend-optimize as work-around. character(len=4) :: string="123 " character(:), allocatable :: trimmed trimmed = trim(string) print *,len_trim(string),len(trimmed) end >From the original dump: (a) Without FE optimization _gfortran_string_trim (&len.1, (void * *) &pstr.0, 4, &string); D.1864 = len.1; if (trimmed != 0B) goto L.1; trimmed = (character(kind=1)[1:.trimmed] *) __builtin_malloc ((sizetype) len.1); ... .trimmed = len.1; (b) With FE optimization if (trimmed != 0B) goto L.1; trimmed = (character(kind=1)[1:.trimmed] *) __builtin_malloc (4); ... .trimmed = 4;