This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug fortran/31210] I/O of string with (non-constant) zero length



------- Comment #2 from fxcoudert at gcc dot gnu dot org  2007-05-04 15:06 -------
Reduced testcase:

  integer :: l = 0
  write(*,'(A,I1)') foo(), 0
contains
   function foo()
     character(len=l) :: foo
     foo = "XXXX"
   end function
end

When a function result is a string of length 0, and that length is not known at
compile time, we create it with _gfortran_internal_malloc(0), which returns
NULL. The library is gets confused when seeing that NULL pointer, even though
the length is also passed and is zero, and requests more data elements (thus
munching the next integer in the I/O list).

If it's deemed that this is a library problem, as I think, I propose the
following patch (which fixes both the original and the reduced testcases, and
regtests fine on i686-linux):


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.  */


-- 

fxcoudert at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jvdelisle at gcc dot gnu dot
                   |                            |org, fxcoudert at gcc dot
                   |                            |gnu dot org
           Keywords|                            |patch
      Known to fail|                            |4.1.3 4.2.0 4.3.0
   Last reconfirmed|2007-03-21 17:05:21         |2007-05-04 15:06:10
               date|                            |
            Summary|wrong code generated:       |I/O of string with (non-
                   |character MERGE(...) with   |constant) zero length
                   |MASK=.false.                |
   Target Milestone|---                         |4.3.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31210


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]