Bug 82007 - DTIO write format stored in a string leads to severe errors
Summary: DTIO write format stored in a string leads to severe errors
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 7.2.0
: P3 normal
Target Milestone: ---
Assignee: Jerry DeLisle
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-28 12:15 UTC by Édouard Canot
Modified: 2018-02-20 04:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-08-30 00:00:00


Attachments
Source file of dtio_bug_1.f90 (693 bytes, text/plain)
2017-08-28 12:15 UTC, Édouard Canot
Details
A peliminary patch (654 bytes, patch)
2017-12-31 01:07 UTC, Jerry DeLisle
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Édouard Canot 2017-08-28 12:15:36 UTC
Created attachment 42061 [details]
Source file of dtio_bug_1.f90

Many tests of the DTIO feature (in .../gcc/testsuite/gfortran.dg/dtio_*.f90) use inline format like in:
   write( unit=10, fmt="(DT'zeroth')", iostat=myiostat, iomsg=astring) member

When I attempted to put the DTIO format string "(DT'zeroth')" in a character string, I got many kinds of error, from ICE (Internal Compiler Error) to bad execution.

See the attached file (which comes from the Gfortran testsuite dtio_1.f90, simplified as much as possible) : dtio_bug_1.f90

According the length of the character string, it may lead to some errors. I think the bug is related to how is read the DTIO format string.

The bug occurs either from GCC-7.2 compiled by myself on Ubuntu-16.10, either from GCC-7.2 packaged in Ubuntu-17.10.

Regards,
Édouard
Comment 1 Édouard Canot 2017-08-28 12:19:39 UTC
I omit to say that many lines in the attached source are commented by a double bang (!!); remove some of them to reveal the bug...

EC
Comment 2 Jerry DeLisle 2017-08-29 04:15:34 UTC
I am investigating. Thanks for report
Comment 3 Dominique d'Humieres 2017-08-30 12:33:49 UTC
Confirmed.
Comment 4 Jerry DeLisle 2017-08-31 03:27:31 UTC
With gfortran 7.2.1 and trunk, I do not see the error from the write at line 46:

    write( unit=10, fmt=fmt_str, iostat=myiostat, iomsg=astring) member

I do see the ICE with this one:

    write( unit=10, fmt=trim(fmt_str), iostat=myiostat, iomsg=astring) member
Comment 5 Jerry DeLisle 2017-10-01 17:13:23 UTC
The ice is because we are not handling the case where the expreesion is type function vs character expression or character constant. I am thinking we need to simplify the expression before trying to use it.
Comment 6 Jerry DeLisle 2017-12-31 01:07:13 UTC
Created attachment 42992 [details]
A peliminary patch

The attached patch allows the test cases to compile and run.  Remaining is to formulate what checks we really do want to do at compile time and what to do for run time.  For example, if a user has provided DTIO procedures, but the format statement does not contain a 'DT' specifier, what do we do? If the format string is in a variable, the situation can only be checked at run time.
Comment 7 Jerry DeLisle 2018-01-13 20:41:31 UTC
Author: jvdelisle
Date: Sat Jan 13 20:41:00 2018
New Revision: 256649

URL: https://gcc.gnu.org/viewcvs?rev=256649&root=gcc&view=rev
Log:
2018-01-13  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

        PR fortran/82007
        * resolve.c (resolve_transfer): Delete code looking for 'DT'
        format specifiers in format strings. Set formatted to true if a
        format string or format label is present.
        * trans-io.c (get_dtio_proc): Likewise. (transfer_expr): Fix
        whitespace.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/trans-io.c
Comment 8 Jerry DeLisle 2018-01-14 23:06:22 UTC
Should I backport this to 7?
Comment 9 Dominique d'Humieres 2018-01-24 17:42:57 UTC
> Should I backport this to 7?

IMO yes.
Comment 10 Jerry DeLisle 2018-02-20 04:06:09 UTC
Author: jvdelisle
Date: Tue Feb 20 04:05:38 2018
New Revision: 257837

URL: https://gcc.gnu.org/viewcvs?rev=257837&root=gcc&view=rev
Log:
2018-02-19  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	Backport from trunk
	PR fortran/82007
	* resolve.c (resolve_transfer): Delete code looking for 'DT'
	format specifiers in format strings. Set formatted to true if a
	format string or format label is present.
	* trans-io.c (get_dtio_proc): Likewise. (transfer_expr): Fix
	whitespace.

Modified:
    branches/gcc-7-branch/gcc/fortran/ChangeLog
    branches/gcc-7-branch/gcc/fortran/resolve.c
    branches/gcc-7-branch/gcc/fortran/trans-io.c
Comment 11 Jerry DeLisle 2018-02-20 04:07:27 UTC
Fixed on 7 and closing.