Bug 25116 - [4.0] namelist read from non-opened file
Summary: [4.0] namelist read from non-opened file
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.0.3
: P3 normal
Target Milestone: 4.1.0
Assignee: Francois-Xavier Coudert
URL:
Keywords: patch
Depends on:
Blocks: 19292
  Show dependency treegraph
 
Reported: 2005-11-27 11:20 UTC by Toon Moene
Modified: 2005-12-09 18:52 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.2.0 4.1.0
Known to fail: 4.0.3
Last reconfirmed: 2005-12-02 18:08:49


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Toon Moene 2005-11-27 11:20:39 UTC
Gfortran should read a namelist from a file that has not
been given a name on an open statement from the fort.n
where n is the unit number in the read statement.
(this a defacto standard way to do it on Unix systems).
Hmm, in lynx I do not see the attach file option, so here goes:
      namelist /nlist/ i, a
      read(9,nlist)
      write(6,nlist)
      end
with the following contents of fort.9:
 &nlist i=1, a=3.5 /
should print the namelist to stdout, not give "end of file".
It works if you add "open(9,file='fort.9')" before the read.
Blocks: HIRLAM.
Comment 1 Francois-Xavier Coudert 2005-11-27 11:38:52 UTC
Confirmed on mainline. Interesting, because the Access time of the fort.9 file is indeed modified, and the error message is:

At line 2 of file nml.f
Fortran runtime error: End of file
Comment 2 Francois-Xavier Coudert 2005-11-27 23:14:43 UTC
(In reply to comment #1)
> At line 2 of file nml.f
> Fortran runtime error: End of file

Debugging shows that the bytes_left field of the stream is not set correctly (2338621003140048416 instead of 9223372036854775807). It is set in transfer.c, line 1143.
Comment 3 Jerry DeLisle 2005-11-28 01:40:50 UTC
Subject: Re:  [regression wrt g77] namelist read from
 non-opened file

fxcoudert at gcc dot gnu dot org wrote:
> ------- Comment #2 from fxcoudert at gcc dot gnu dot org  2005-11-27 23:14 -------
> (In reply to comment #1)
> 
>>At line 2 of file nml.f
>>Fortran runtime error: End of file
> 
> 
> Debugging shows that the bytes_left field of the stream is not set correctly
> (2338621003140048416 instead of 9223372036854775807). It is set in transfer.c,
> line 1143.
> 
> 
If you mean this:


   memcpy (&i, p, sizeof (gfc_offset));
   dtp->u.p.current_unit->bytes_left = i;

Maybe it should be a -=

Jerry



Comment 4 Francois-Xavier Coudert 2005-11-28 07:08:02 UTC
No, it's in fact easier than that. We shouldn't come into us_read for this file, which is formatted! Probably a bad default flag is set.
Comment 5 Jerry DeLisle 2005-11-28 08:09:52 UTC
Subject: Re:  namelist read from non-opened file

fxcoudert at gcc dot gnu dot org wrote:
> ------- Comment #4 from fxcoudert at gcc dot gnu dot org  2005-11-28 07:08 -------
> No, it's in fact easier than that. We shouldn't come into us_read for this
> file, which is formatted! Probably a bad default flag is set.
> 
> 
I think you are right.  I have been putting in debug statements all over and 
find that we are asking the wrong length of reads, 8 characters, instead of 1 in 
the failing case.  I will get back to this in a day or so.
Comment 6 Toon Moene 2005-11-28 15:36:47 UTC
> I think you are right.  I have been putting in debug statements all over and 
> find that we are asking the wrong length of reads, 8 characters, instead of 1
> in the failing case.  I will get back to this in a day or so.

Isn't this a sign that we are treating the read as UNFORMATTED ?  Which would be wrong as namelist reads are always FORMATTED (note: the file isn't opened, so that cannot discern between FORMATTED and UNFORMATTED.
Comment 7 Francois-Xavier Coudert 2005-11-28 15:59:31 UTC
I think the following patch (no time yet to regtest it, and won't have time soon, please feel free to test it) fixes it:

Index: transfer.c
===================================================================
--- transfer.c  (revision 107563)
+++ transfer.c  (working copy)
@@ -1239,11 +1239,14 @@
      memset (&u_flags, '\0', sizeof (u_flags));
      u_flags.access = ACCESS_SEQUENTIAL;
      u_flags.action = ACTION_READWRITE;
+
      /* Is it unformatted?  */
-     if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
+     if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
+                | IOPARM_DT_IONML_SET)))
        u_flags.form = FORM_UNFORMATTED;
      else
        u_flags.form = FORM_UNSPECIFIED;
+
      u_flags.delim = DELIM_UNSPECIFIED;
      u_flags.blank = BLANK_UNSPECIFIED;
      u_flags.pad = PAD_UNSPECIFIED;
Comment 8 Jerry DeLisle 2005-11-28 21:07:53 UTC
Subject: Re:  namelist read from non-opened file

fxcoudert at gcc dot gnu dot org wrote:
> +
>       /* Is it unformatted?  */
> -     if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
> +     if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
> +                | IOPARM_DT_IONML_SET)))

Yes, I think this is the exact spot I was looking at last night.  I was just 
going to go find the right namelist define to use.  I assume you tested this and 
it works, of course.
  :)
Comment 9 Toon Moene 2005-11-29 19:20:34 UTC
FX,

Your patch solved the problem for me.
AFAICS, this patch is indeed the correct fix for this problem.
Please apply it to (at least) 4.1 and trunk.
It might be appropriate for the 4.0 branch, too.
Comment 10 Francois-Xavier Coudert 2005-12-02 16:46:53 UTC
Subject: Bug 25116

Author: fxcoudert
Date: Fri Dec  2 16:46:49 2005
New Revision: 107900

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=107900
Log:
	PR libfortran/25116
	* io/transfer.c (data_transfer_init): Don't set the default for
	namelist I/O on preconnected files to UNFORMATTED.

Modified:
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/io/transfer.c

Comment 11 Francois-Xavier Coudert 2005-12-02 18:08:49 UTC
Commited to mainline, waiting a bit before committing to 4.1.
Comment 12 Francois-Xavier Coudert 2005-12-09 18:50:50 UTC
Subject: Bug 25116

Author: fxcoudert
Date: Fri Dec  9 18:50:48 2005
New Revision: 108314

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=108314
Log:
	PR libfortran/25116
	* io/transfer.c (data_transfer_init): Don't set the default for
	namelist I/O on preconnected files to UNFORMATTED.

Modified:
    branches/gcc-4_1-branch/libgfortran/ChangeLog
    branches/gcc-4_1-branch/libgfortran/io/transfer.c

Comment 13 Francois-Xavier Coudert 2005-12-09 18:52:01 UTC
Committed to 4.1