[gfortran,committed] Fix PR31210: I/O of string with (non-constant) zero length

François-Xavier Coudert fxcoudert@gmail.com
Fri May 4 15:21:00 GMT 2007


Committed to mainline after approval by Jerry in bugzilla. Regtested
on i686-linux, comes with a testcase.


2007-05-04  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

        PR libfortran/31210
        * io/transfer.c (transfer_character): Avoid passing a NULL
        pointer as source to the transfer routines, if the string length
        is zero.

2007-05-04  Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>

        PR libfortran/31210
        * gfortran.dg/zero_length_1.f90: New test.


Index: libgfortran/io/transfer.c
===================================================================
--- libgfortran/io/transfer.c   (revision 124285)
+++ libgfortran/io/transfer.c   (working copy)
@@ -1401,8 +1401,17 @@ transfer_logical (st_parameter_dt *dtp,
 void
 transfer_character (st_parameter_dt *dtp, void *p, int len)
 {
+  static char *empty_string[0];
+
   if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
     return;
+
+  /* Strings of zero length can have p == NULL, which confuses the
+     transfer routines into thinking we need more data elements.  To avoid
+     this, we give them a nice pointer.  */
+  if (len == 0 && p == NULL)
+    p = empty_string;
+
   /* Currently we support only 1 byte chars, and the library is a bit
      confused of character kind vs. length, so we kludge it by setting
      kind = length.  */
Index: gcc/testsuite/gfortran.dg/zero_length_1.f90
===================================================================
--- gcc/testsuite/gfortran.dg/zero_length_1.f90 (revision 0)
+++ gcc/testsuite/gfortran.dg/zero_length_1.f90 (revision 0)
@@ -0,0 +1,18 @@
+! { dg-do run }
+! PR libfortran/31210
+program test
+  implicit none
+  integer :: l = 0
+  character(len=20) :: s
+
+  write(s,'(A,I1)') foo(), 0
+  if (trim(s) /= "0") call abort
+
+contains
+
+  function foo()
+    character(len=l) :: foo
+    foo = "XXXX"
+  end function
+
+end program test



More information about the Fortran mailing list