Bug 31487 - Character array initialization in derived type: bad zero padding
Summary: Character array initialization in derived type: bad zero padding
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: wrong-code
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2007-04-05 14:49 UTC by Harald Anlauf
Modified: 2007-12-09 09:19 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.3.0
Last reconfirmed: 2007-04-16 15:38:02


Attachments
Demo code (356 bytes, text/plain)
2007-04-05 14:51 UTC, Harald Anlauf
Details
Fix for PR - not yet regtested (427 bytes, patch)
2007-12-07 18:37 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Harald Anlauf 2007-04-05 14:49:58 UTC
Hi,

here's an annoying one.  Default initializing a
character array in a derived type leads to zero
padded strings instead of space-padded ones.
E.g.

  ! This works correctly:
  character(len=16) ::    tdefi(2) = (/'0z1jan0000','1hr       '/)
  !                                     1234567890

  ! Broken in derived type:
  type t_ctl
     character(len=16) :: tdefi(2) = (/'0z1jan0000','1hr       '/)
     !                                  1234567890
  end type t_ctl


See the attached program for details.
Comment 1 Harald Anlauf 2007-04-05 14:51:05 UTC
Created attachment 13332 [details]
Demo code
Comment 2 Tobias Schlüter 2007-04-16 15:37:36 UTC
I'll have a look.
Comment 3 Tobias Schlüter 2007-04-17 16:12:03 UTC
The strings aren't actually padded.  From the assembly output:
_ctl.1000:
        .ascii "0z1jan0000"
        .space 6
        .ascii "1hr       "
        .space 6
        .align 5
_tdefi.996:
        .ascii "0z1jan0000      "
        .ascii "1hr             "
        .const
In other words, they are put into the right memory locations, but without any regard to their string nature.  I've not yet figured out where this goes wrong.
Comment 4 Paul Thomas 2007-12-07 18:37:18 UTC
Created attachment 14708 [details]
Fix for PR - not yet regtested
Comment 5 Dominique d'Humieres 2007-12-07 23:05:40 UTC
I have applied the patch in #4 and I have a lot of failures:

[ibook-dhum] f90/bug% cat pr30200_red.f90
   type test
    character(len=100) :: names(5)
   end type test
   type(test) :: keyword
   keyword%names=(/"a","b","c","d","e"/)
   END
[ibook-dhum] f90/bug% gfc pr30200_red.f90
f951: internal compiler error: Bus error
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.

I did not looked yet at all the failures, but all the one I have looked at gives a bus error.

Comment 6 Paul Thomas 2007-12-08 06:43:40 UTC
(In reply to comment #5)


> I did not looked yet at all the failures, but all the one I have looked at
> gives a bus error.

Thanks Dominique.  I'm looking at PR32129 first and then this.  I am sure that this fix is fundamentally right but that I have just forgotten a check on the existence of something.

Cheers

Paul
Comment 7 Paul Thomas 2007-12-08 06:50:26 UTC
(In reply to comment #5)


> I did not looked yet at all the failures, but all the one I have looked at
> gives a bus error.

In fact, I do not check that the initializer is not NULL.

Paul
Comment 8 Dominique d'Humieres 2007-12-08 20:43:31 UTC
Now works as advertized without regression on i686-apple-darwin9.

Comment 9 Paul Thomas 2007-12-09 09:17:37 UTC
Subject: Bug 31487

Author: pault
Date: Sun Dec  9 09:17:24 2007
New Revision: 130719

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130719
Log:
2007-12-09  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/32129
	* dump-parse-tree.c (gfc_show_expr_n): New function for
	debugging.
	* gfortran.h : Add prototype for gfc_show_expr_n.
	* expr.c (simplify_constructor): Copy the constructor
	expression and try to simplify that.  If success, replace the
	original.  Otherwise discard the copy, keep going through
	the structure and return success.

	PR fortran/31487
	* decl.c (build_struct): Pad out default initializers with
	spaces to the component character length.

2007-12-09  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/32129
	* gfortran.dg/derived_comp_array_ref_6.f90: New test.
	* gfortran.dg/derived_comp_array_ref_7.f90: New test.

	PR fortran/31487
	* gfortran.dg/char_component_initializer_1.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/char_component_initializer_1.f90
    trunk/gcc/testsuite/gfortran.dg/derived_comp_array_ref_6.f90
    trunk/gcc/testsuite/gfortran.dg/derived_comp_array_ref_7.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/decl.c
    trunk/gcc/fortran/dump-parse-tree.c
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/testsuite/ChangeLog

Comment 10 Paul Thomas 2007-12-09 09:19:29 UTC
Fixed on trunk

Paul