This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch, fortran] PR31867 - function result with character LEN computed at run time


:ADDPATCH fortran:

This turns out to be a very strange bug because it is rather hard to
trigger but is, at the same time, quite central to the scalarizer.

Tobias Burnus posted this shortened testcase to the PR:

program xjoin
 implicit none
 character (len=5) :: words(2) = (/"two  ","three"/)
 write (*,*)  len(join(words)) ! should be 8 is 10
 write (*,*)  join(words)      ! valgrind error
contains
function join(words) result(str)
 character (len=*), intent(in)        :: words(:)
 character (len=sum(len_trim(words))) :: str
 str = ''
end function join
end program xjoin

The character length of 'str' is correctly calculate within the
procedure but goes wrong in the interface version that is used in the
main program.  A quick perusal of the code shows that, in this latter,
a structure "parm" is built, which is a finger-print of
trans-array.c(gfc_conv_expr_descriptor).  It is immediately noticable
that the offset field is zero, which is incorrect for the scalarized
calculation of the character length.  This is confirmed by resetting
the declaraion of words to:

character (len=5) :: words(0:1) = (/"two ","three"/),

whereupon the testcase works correctly.  The patch consists of
calculating the offset, using, to start, the offset stored with the
declaration and then correcting it for loops, where the stride is not
one.

As remarked above, a curious feature is the difficulty in actually
triggering this bug.  For the main part, the offset produced here is
not used.  Therefore, the testcase is a development of the reporter's
and tests various combinations of array references and strides.

Bootstrapped and regtested on x86_ia64/FC5 - OK for trunk?

Paul

2007-05-15 Paul Thomas <pault@gcc.gnu.org>

	PR fortran/31867
	* trans-array.c (gfc_conv_expr_descriptor): Obtain the stored
	offset for non-descriptor, source arrays and correct for stride
	not equal to one before writing to field of output descriptor.

2007-05-15 Paul Thomas <pault@gcc.gnu.org>

	PR fortran/31867
	* gfortran.dg/char_length_5.f90: New test.

Attachment: pr31867_final.diff
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]