Bug 29452 - Keyword check for specifiers in WRITE and READ
Summary: Keyword check for specifiers in WRITE and READ
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-10-13 10:01 UTC by tobias.burnus
Modified: 2007-10-18 13:39 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-10-30 14:19:57


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tobias.burnus 2006-10-13 10:01:23 UTC
http://gcc.gnu.org/ml/fortran/2006-10/msg00241.html adds compile-time checking for the specifier arguments of CLOSE and OPEN.

This should be done analogously for
(cf. 9.5.1 in Fortran 2003)

WRITE/READ (some only in READ allowed; some sre not yet implemented in gfortran):
- ADVANCE: 'YES', 'NO'
- ASYNCHRONOUS: 'YES', 'NO'
- BLANK: 'NULL', 'ZERO'
- DECIMAL: 'COMMA', 'POINT'
- DELIM: 'APOSTROPHE', 'QUOTE', 'NONE
- PAD: 'YES', 'NO'
- ROUND: 'UP', 'DOWN', 'ZERO', 'NEAREST', 'COMPATIBLE'. 'PROCESSOR_DEFINED'
- SIGN: PLUS, SUPPRESS, PROCESSOR_DEFINED
Comment 1 tobias.burnus 2006-10-13 10:13:30 UTC
Some tests show:

  open(13,file='foo',form='format')
  close(13,status='del')

compiles and runs in gfortran.
Expected:
- A run-time error is shown.
- A compile-time-error is shown (probably fixed by FX patch, not checked)

* * *

  write(13,'(a)',advance='N') 'Hello:'

This gives a compile-time error, whereas
  str = 'N'
  write(13,'(a)',advance=str) 'Hello:'
gives not a run-time error.

  write(13,'(a)',advance='Not') 'Hello:'

gives no compile-time error, but a run-time error.
Comment 2 tobias.burnus 2006-10-17 15:31:00 UTC
The library problems are due to an error in the string comparison; one only compares the first (fortran string length) characters, but never checks whether the strings are of identical length.

Patch:

Index: libgfortran/runtime/string.c
===================================================================
--- libgfortran/runtime/string.c	(revision 117796)
+++ libgfortran/runtime/string.c	(working copy)
@@ -44,6 +44,7 @@
 
   /* Strip trailing blanks from the Fortran string.  */
   len = fstrlen (s1, s1_len);
+  if(len != strlen(s2)) return 0; /* don't match */
   return strncasecmp (s1, s2, len) == 0;
 }


Similarly for WRITE(*,*,ADVANCE='YES/NO'):

Index: gcc/fortran/io.c
===================================================================
--- gcc/fortran/io.c	(revision 117796)
+++ gcc/fortran/io.c	(working copy)
@@ -2697,8 +2697,8 @@
       if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
 	{
 	  const char * advance = expr->value.character.string;
-	  not_no = strncasecmp (advance, "no", 2) != 0;
-	  not_yes = strncasecmp (advance, "yes", 2) != 0;
+	  not_no = strcasecmp (advance, "no") != 0;
+	  not_yes = strcasecmp (advance, "yes") != 0;
 	}
       else
 	{

I will submit a patch in the next days.
(The other suggested checks for READ/WRITE are not yet needed as those options are not supported, yet.
I don't know in how far strcasencmp problems occurre at other places as well.)
Comment 3 Francois-Xavier Coudert 2006-10-22 07:15:23 UTC
(In reply to comment #1)
> - A compile-time-error is shown (probably fixed by FX patch, not checked)

Yes, it's fixed by my patch. Confirming this bug.

Tobias, if you want to submit a global patch to resolve these issues (both compile-time and run-time), I'll be happy to review it. 4.3 is in stage 1, it's time for change! :)

PS: for the compile-time checks, you can probably use the compare_to_allowed helper routine I added in my "OPEN/CLOSE checks" patch; most of the work is probably reading both F95 and F2003 to spot the differences.
Comment 4 patchapp@dberlin.org 2006-10-26 08:27:56 UTC
Subject: Bug number PR29452

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-10/msg01327.html
Comment 5 Tobias Burnus 2006-10-30 14:17:26 UTC
Subject: Bug 29452

Author: burnus
Date: Mon Oct 30 14:17:15 2006
New Revision: 118184

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118184
Log:
fortran/
2006-10-26  Tobias Burnus  <burnus@net-b.de>

       	PR fortran/29452
       	* io.c (check_io_constraints): Fix keyword string comparison.

libgfortran/
2006-10-26  Tobias Burnus  <burnus@net-b.de>

       	PR fortran/29452
	* runtime/string.c (compare0): Check whether string lengths match.

testsuite/
2006-10-26  Tobias Burnus  <burnus@net-b.de>

	PR fortran/29452
	* gfortran.dg/write_check.f90: Check run-time keyword checking.
	* gfortran.dg/write_check2.f90: Check compile-time keyword checking.


Added:
    branches/gcc-4_2-branch/gcc/testsuite/gfortran.dg/write_check.f90
    branches/gcc-4_2-branch/gcc/testsuite/gfortran.dg/write_check2.f90
Modified:
    branches/gcc-4_2-branch/gcc/fortran/ChangeLog
    branches/gcc-4_2-branch/gcc/fortran/io.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_2-branch/libgfortran/ChangeLog
    branches/gcc-4_2-branch/libgfortran/runtime/string.c

Comment 6 Tobias Burnus 2006-10-30 14:19:57 UTC
Accept bug
Comment 7 Tobias Burnus 2006-10-30 14:21:39 UTC
And mark as fixed.

Hopefully, we won't forget to add checks also to the following specifiers as soon as we implement them.

WRITE/READ (some only in READ allowed; those are not yet implemented in
gfortran):
- ASYNCHRONOUS: 'YES', 'NO'
- BLANK: 'NULL', 'ZERO'
- DECIMAL: 'COMMA', 'POINT'
- DELIM: 'APOSTROPHE', 'QUOTE', 'NONE
- PAD: 'YES', 'NO'
- ROUND: 'UP', 'DOWN', 'ZERO', 'NEAREST', 'COMPATIBLE'. 'PROCESSOR_DEFINED'
- SIGN: PLUS, SUPPRESS, PROCESSOR_DEFINED
Comment 8 Francois-Xavier Coudert 2006-10-30 14:24:06 UTC
(In reply to comment #7)
> - ASYNCHRONOUS: 'YES', 'NO'
> - BLANK: 'NULL', 'ZERO'
> - DECIMAL: 'COMMA', 'POINT'
> - DELIM: 'APOSTROPHE', 'QUOTE', 'NONE
> - PAD: 'YES', 'NO'
> - ROUND: 'UP', 'DOWN', 'ZERO', 'NEAREST', 'COMPATIBLE'. 'PROCESSOR_DEFINED'
> - SIGN: PLUS, SUPPRESS, PROCESSOR_DEFINED

I'm planning to implement those soon. I'll keep that PR open and assigned to me to be sure I remember :)
Comment 9 Tobias Burnus 2006-10-30 18:23:02 UTC
Subject: Bug 29452

Author: burnus
Date: Mon Oct 30 18:22:47 2006
New Revision: 118191

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=118191
Log:
fortran/
2006-10-30  Tobias Burnus  <burnus@net-b.de>

       	PR fortran/29452
       	* io.c (check_io_constraints): Fix keyword string comparison.

libgfortran/
2006-10-30  Tobias Burnus  <burnus@net-b.de>

       	PR fortran/29452
	* runtime/string.c (compare0): Check whether string lengths match.

testsuite/
2006-10-30  Tobias Burnus  <burnus@net-b.de>

	PR fortran/29452
	* gfortran.dg/write_check.f90: Check run-time keyword checking.
	* gfortran.dg/write_check2.f90: Check compile-time keyword checking


Added:
    trunk/gcc/testsuite/gfortran.dg/write_check.f90
    trunk/gcc/testsuite/gfortran.dg/write_check2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/io.c
    trunk/gcc/testsuite/ChangeLog
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/runtime/string.c

Comment 10 Francois-Xavier Coudert 2007-10-18 13:39:09 UTC
I think it's best to close this one. I only wanted to keep it open because I thought I'd add the rest of the F2003 specifiers quickly, which didn't happen.