Bug 30200 - [4.1 only] write(*,myfmt="(1X,a,'xyz')") "A" prints Az' instead of Axyz
Summary: [4.1 only] write(*,myfmt="(1X,a,'xyz')") "A" prints Az' instead of Axyz
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Jerry DeLisle
URL:
Keywords: wrong-code
Depends on:
Blocks: 29975
  Show dependency treegraph
 
Reported: 2006-12-13 15:23 UTC by Joost VandeVondele
Modified: 2006-12-25 23:10 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-12-15 02:16:16


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joost VandeVondele 2006-12-13 15:23:26 UTC
vondele@pcihopt1:/scratch/vondele/clean/cp2k/tests/QS/regtest-gpw-2> gfortran test.f90
vondele@pcihopt1:/scratch/vondele/clean/cp2k/tests/QS/regtest-gpw-2> ./a.out
 avondele@pcihopt1:/scratch/vondele/clean/cp2k/tests/QS/regtest-gpw-2> valgrind --tool=memcheck ./a.out
==16188== Memcheck, a memory error detector.
==16188== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==16188== Using LibVEX rev 1575, a library for dynamic binary translation.
==16188== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==16188== Using valgrind-3.1.1, a dynamic binary instrumentation framework.
==16188== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==16188== For more details, rerun with: -v
==16188==
==16188== Invalid read of size 1
==16188==    at 0x4B93D8D: formatted_transfer_scalar (transfer.c:834)
==16188==    by 0x4B94182: formatted_transfer (transfer.c:1355)
==16188==    by 0x400CF2: MAIN__ (in /scratch/vondele/clean/cp2k/tests/QS/regtest-gpw-2/a.out)
==16188==    by 0x400D2D: main (fmain.c:18)
==16188==  Address 0x5148646 is 6 bytes inside a block of size 10 free'd
==16188==    at 0x4A1984D: free (vg_replace_malloc.c:235)
==16188==    by 0x400CC6: MAIN__ (in /scratch/vondele/clean/cp2k/tests/QS/regtest-gpw-2/a.out)
==16188==    by 0x400D2D: main (fmain.c:18)
==16188==
==16188== Invalid read of size 1
==16188==    at 0x4B93DA9: formatted_transfer_scalar (transfer.c:838)
==16188==    by 0x4B94182: formatted_transfer (transfer.c:1355)
==16188==    by 0x400CF2: MAIN__ (in /scratch/vondele/clean/cp2k/tests/QS/regtest-gpw-2/a.out)
==16188==    by 0x400D2D: main (fmain.c:18)
==16188==  Address 0x5148647 is 7 bytes inside a block of size 10 free'd
==16188==    at 0x4A1984D: free (vg_replace_malloc.c:235)
==16188==    by 0x400CC6: MAIN__ (in /scratch/vondele/clean/cp2k/tests/QS/regtest-gpw-2/a.out)
==16188==    by 0x400D2D: main (fmain.c:18)
 a ==16188==
==16188== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 3 from 2)
==16188== malloc/free: in use at exit: 0 bytes in 0 blocks.
==16188== malloc/free: 11 allocs, 11 frees, 29,400 bytes allocated.
==16188== For counts of detected errors, rerun with: -v
==16188== All heap blocks were freed -- no leaks are possible.
vondele@pcihopt1:/scratch/vondele/clean/cp2k/tests/QS/regtest-gpw-2> cat test.f90
   character(len=100) myfmt
   type test
    character(len=100) :: names(5)
   end type test
   type(test) :: keyword
   keyword%names=(/"a","b","c","d","e"/)
   myfmt="1X"
   WRITE(unit=6,fmt="("//TRIM(myfmt)//",a,' ')",advance="NO") TRIM(keyword%names(1))
   END
Comment 1 Joost VandeVondele 2006-12-13 19:28:08 UTC
This problem seems to be at the root of most CP2K regtest failures described in PR29975
Comment 2 Tobias Burnus 2006-12-13 19:47:50 UTC
Simplified version (might not cover all problems):
   character(len=20) myfmt
   myfmt="(1X,a,'x')"
   WRITE(*,fmt=trim(myfmt)) "y"
   END

If one looks now at the dump:
    _gfortran_string_trim (&len.2, (void * *) &pstr.1, 20, &myfmt);
    dt_parm.0.format = pstr.1;
    dt_parm.0.format_len = len.2;
    _gfortran_st_write (&dt_parm.0);
        _gfortran_internal_free (pstr.1);
    _gfortran_transfer_character (&dt_parm.0, "y", 1);
    _gfortran_st_write_done (&dt_parm.0);

I'm not sure whether this is the problem, but pstr.1 is freed before "y" is send to "write" - but the string has to be written after "y".
Comment 3 Tobias Burnus 2006-12-13 20:11:29 UTC
CC: some more libgfortran/io experienced persons.

There seems to go something wrong when one uses
  format = "(1X,a,'xyz')"
  write(*,fmt=trim(format)) "A"
The result is:
 Az'

Expected:
 Axyz

The values in write_constant_string are:
  length = 3       // ok
  q[-1] = '' (0)   // should be  '
  q[0] = '' (0)    // should be  x
  q[1] = 'y' (121) 
  q[2] = 'z' (122)

The following works:
- no trim
- format = "(1X,'abc',a,'xyz')"
- format = "(1X,'abc',a)"
- format = "(1X,'xyz')"
Comment 4 Thomas Koenig 2006-12-13 21:12:19 UTC
(In reply to comment #3)
> CC: some more libgfortran/io experienced persons.
> 
> There seems to go something wrong when one uses
>   format = "(1X,a,'xyz')"
>   write(*,fmt=trim(format)) "A"
> The result is:
>  Az'

WORKSFORME:

$ cat foo.f90
program main
  character (len=20) format
  format = "(1X,a,'xyz')"
  write(*,fmt=trim(format)) "A"
end
$ gfortran foo.f90
$ ./a.out
 Axyz
$ gfortran -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../../gcc/trunk/configure --prefix=/home/ig25 --enable-languages=c,fortran
Thread model: posix
gcc version 4.3.0 20061127 (experimental)
Comment 5 Jerry DeLisle 2006-12-14 00:26:19 UTC
Subject: Re:  write(*,myfmt="(1X,a,'xyz')") "A"  prints
 Az' instead of Axyz

I wonder if this is not a case of the io.h dependency bug hitting yet again.  I 
would fix it if I knew how.
Comment 6 Jerry DeLisle 2006-12-14 02:29:54 UTC
More information.  I get Tobias bad result with -m64 on x86-64-Linux.  The problem goes away with -m32.

$ gfortran -m32 pr30200-2.f90 
$ ./a.out
 Axyz
$ gfortran -m64 pr30200-2.f90 
$ ./a.out
 Az'

I get a similar result with the orginal test case from Joost.  The problem is -m64 specific.
Comment 7 Joost VandeVondele 2006-12-14 06:41:36 UTC
(In reply to comment #6)
> More information.  I get Tobias bad result with -m64 on x86-64-Linux.  The
> problem goes away with -m32.
> 
> $ gfortran -m32 pr30200-2.f90 
> $ ./a.out
>  Axyz
> $ gfortran -m64 pr30200-2.f90 
> $ ./a.out
>  Az'

whereas -m32 is printing the right result, I think it is coincidence, at least to original testcase still yields valgrind errors at runtime if compiled with -m32

Comment 8 Jerry DeLisle 2006-12-15 02:16:16 UTC
I will take this on.
Comment 9 patchapp@dberlin.org 2006-12-15 08:45:44 UTC
Subject: Bug number PR30200

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-12/msg01099.html
Comment 10 Jerry DeLisle 2006-12-15 08:51:36 UTC
Tobias observation in comment #2 is correct.  The submitted patch moves the memory free actions to after the write transfers have completed.  No more valgrind errors.
Comment 11 Jerry DeLisle 2006-12-15 19:48:29 UTC
Subject: Bug 30200

Author: jvdelisle
Date: Fri Dec 15 19:48:08 2006
New Revision: 119940

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=119940
Log:
2006-12-15  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/30200
	* trans-io.c (build_dt): Move post block for format_expr to end.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-io.c

Comment 12 Jerry DeLisle 2006-12-15 19:53:02 UTC
Subject: Bug 30200

Author: jvdelisle
Date: Fri Dec 15 19:52:49 2006
New Revision: 119941

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=119941
Log:
2006-12-15  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/30200
	* gfortran.dg/write_fmt_trim.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/write_fmt_trim.f90
Modified:
    trunk/gcc/testsuite/ChangeLog

Comment 13 Jerry DeLisle 2006-12-19 06:29:01 UTC
Subject: Bug 30200

Author: jvdelisle
Date: Tue Dec 19 06:28:47 2006
New Revision: 120041

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=120041
Log:
2006-12-18  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/30200
	* trans-io.c (build_dt): Move post block for format_expr to end.

Modified:
    branches/gcc-4_2-branch/gcc/fortran/ChangeLog
    branches/gcc-4_2-branch/gcc/fortran/trans-io.c

Comment 14 Jerry DeLisle 2006-12-19 06:32:19 UTC
Subject: Bug 30200

Author: jvdelisle
Date: Tue Dec 19 06:32:09 2006
New Revision: 120042

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=120042
Log:
2006-12-18  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/30200
	* gfortran.dg/write_fmt_trim.f90: New test.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/gfortran.dg/write_fmt_trim.f90
Modified:
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 15 Tobias Burnus 2006-12-21 15:20:56 UTC
Will this be be fixed for 4.1 too? Or can this PR be closed?
Comment 16 Jerry DeLisle 2006-12-21 16:03:22 UTC
I have the patch ready, I will commit sometime today to 4.1
Comment 17 Jerry DeLisle 2006-12-25 22:53:40 UTC
Subject: Bug 30200

Author: jvdelisle
Date: Mon Dec 25 22:53:29 2006
New Revision: 120199

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=120199
Log:
2006-12-25  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/30200
	* trans-io.c (build_dt): Move post block for format_expr to end.

Modified:
    branches/gcc-4_1-branch/gcc/fortran/ChangeLog
    branches/gcc-4_1-branch/gcc/fortran/trans-io.c

Comment 18 Jerry DeLisle 2006-12-25 23:08:30 UTC
Subject: Bug 30200

Author: jvdelisle
Date: Mon Dec 25 23:08:17 2006
New Revision: 120201

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=120201
Log:
2006-12-25  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libfortran/30145
	* gfortran.dg/write_zero_array.f90: New test.

2006-12-25  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/30200
	* gfortran.dg/write_fmt_trim.f90: New test.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/write_fmt_trim.f90
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/write_zero_array.f90
Modified:
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 19 Jerry DeLisle 2006-12-25 23:10:16 UTC
Fixed on 4.1