Bug 47348 - wrong string length with array constructor
Summary: wrong string length with array constructor
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: Fortran_character 32834 47065
  Show dependency treegraph
 
Reported: 2011-01-18 18:14 UTC by Thomas Koenig
Modified: 2011-03-13 17:40 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-02-21 10:33:10


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2011-01-18 18:14:31 UTC
This one is strange; it is with recent trunk.

ig25@linux-fd1f:~/Krempel/Char-4> cat foo.f90
program main
  implicit none
  character(len=3) :: c(3)

  c = 'x'

  print '(3(A,"|"))', (/ (/ trim(c(1)), 'a' /)//'c', 'cd' /)
  print '(3(A,"|"))', (/ (/ c(1)(1:len_trim(c(1))), 'a' /)//'c', 'cd' /)
  print '(3(A,"|"))', (/ (/ c(1)(1:1), 'a' /)//'c', 'cd' /)
end program main
ig25@linux-fd1f:~/Krempel/Char-4> gfortran foo.f90
ig25@linux-fd1f:~/Krempel/Char-4> ./a.out
xc|ac|cd|
x |a |cd|
xc|ac|cd|

The second line should be identical to the first and third one.

Modified from nested_array_constructor_2.f90, BTW.

Goes back at least to 4.4.1, so probably not a regression.
Comment 1 kargls 2011-01-18 18:42:45 UTC
gfortran 4.2.5 tries to grab 4 GB of memory and eventually
I killed the job.  With 4.3.something, I get

troutmask:sgk[211] gfc43 -o z df.f90 
df.f90: In function 'main':
df.f90:5: internal compiler error: Segmentation fault: 11
Please submit a full bug report,
with preprocessed source if appropriate.

Definitely, an interesting bug.
Comment 2 Thomas Koenig 2011-01-18 19:39:32 UTC
Of course, I can't replace trim(c(1)) with c(1)(1:len_trim(c(1)) until
this is resolved (which is why I found the bug in the first place),
so mark this as blocking PR 47065.
Comment 3 Thomas Koenig 2011-01-19 19:14:04 UTC
Reduced test case:

program main
  implicit none
  character(len=*), parameter :: fmt='(2(A,"|"))'
  integer :: i
  character(len=4) :: q

  q = 'yy'
  i = 2

  print fmt, (/ trim(q), 'ae' /)//'c'
  print fmt, (/ q(1:i), 'ae' /)//'c'

end program main

This prints

yyc|aec|
yy  c|ae  c|

so the string length of the array constructor is wrong.
Comment 4 Thomas Koenig 2011-01-20 07:20:17 UTC
More reduced test case:

program main
  implicit none
  character(len=*), parameter :: fmt='(A,A)'
  integer :: i
  character(len=4) :: q

  q = 'abcd'
  i = 2
  print fmt, (/q(1:i) /),"|"

end program main

produces

ab  |

May be related to PR 44735, which is also an issue with
substrings with non-constant length.

Paul, this is a case of something (trans-*?) picking up the
wrong string length and ignoring the substring.  Do you have
any idea where to start looking?
Comment 5 paul.richard.thomas@gmail.com 2011-01-22 16:50:00 UTC
Dear Thomas,

> Paul, this is a case of something (trans-*?) picking up the
> wrong string length and ignoring the substring.  Do you have
> any idea where to start looking?
>

Almost certainly.  I am very busy right now with reallocation in
scalar assignments.  I'll flag this one up.

cheers

Paul
Comment 6 Paul Thomas 2011-02-19 15:03:30 UTC
Author: pault
Date: Sat Feb 19 15:03:27 2011
New Revision: 170317

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=170317
Log:
2011-02-19  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/47348
	* trans-array.c (get_array_ctor_all_strlen): Move up in file.
	(get_array_ctor_var_strlen): Add block dummy and add call to
	get_array_ctor_all_strlen instead of giving up on substrings.
	Call gcc_unreachable for default case.
	(get_array_ctor_strlen): Add extra argument to in call to
	get_array_ctor_var_strlen.

2011-02-19  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/47348
	* gfortran.dg/array_constructor_36.f90 : New test.
	* gfortran.dg/bounds_check_10.f90 : Change dg-output message to
	allow for comparison between different elements of the array
	constructor at different levels of optimization.


Added:
    trunk/gcc/testsuite/gfortran.dg/array_constructor_36.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/bounds_check_10.f90
Comment 7 Dominique d'Humieres 2011-02-21 10:33:10 UTC
Any plan to backport the fix in revision 170317?
Comment 8 Paul Thomas 2011-02-21 12:06:42 UTC
(In reply to comment #7)
> Any plan to backport the fix in revision 170317?

I had not planned so to do but would respond positively to popular pressure how far back would you want to go?

Paul
Comment 9 tkoenig@netcologne.de 2011-02-21 22:40:52 UTC
Hi Paul,

> (In reply to comment #7)
>> Any plan to backport the fix in revision 170317?
> 
> I had not planned so to do but would respond positively to popular pressure how
> far back would you want to go?

I think 4.5 would be great.  Anything further back is probably unneeded
(the bug has remained undetected for a few years now...)

Thomas
Comment 10 Paul Thomas 2011-03-13 17:37:47 UTC
Author: pault
Date: Sun Mar 13 17:37:43 2011
New Revision: 170923

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=170923
Log:
2011-03-13  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/47348
	* trans-array.c (get_array_ctor_all_strlen): Move up in file.
	(get_array_ctor_var_strlen): Add block dummy and add call to
	get_array_ctor_all_strlen instead of giving up on substrings.
	Call gcc_unreachable for default case.
	(get_array_ctor_strlen): Add extra argument to in call to
	get_array_ctor_var_strlen.

2011-03-13  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/47348
	* gfortran.dg/array_constructor_36.f90 : New test.
	* gfortran.dg/bounds_check_10.f90 : Change dg-output message to
	allow for comparison between different elements of the array
	constructor at different levels of optimization.


Added:
    branches/gcc-4_5-branch/gcc/testsuite/gfortran.dg/array_constructor_36.f90
Modified:
    branches/gcc-4_5-branch/gcc/fortran/ChangeLog
    branches/gcc-4_5-branch/gcc/fortran/trans-array.c
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog
Comment 11 Paul Thomas 2011-03-13 17:40:13 UTC
(In reply to comment #10)
Fixed on 4.5 and trunk.

Closing

Thanks for the report.

Paul