This is the mail archive of the gcc-patches@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]

[patch,libfortran] PR34876 can't read/write zero length array sections


Hi,

This patch fixes this bug by writing an "empty" record for the case of a "zero sized" array.

Fairly self explanatory.

Thanks Paul for helping me 'see' what the problem was. I have never used a zero sized array for anything.

Test case provided.

Regression tested on x86-64-linux-gnu.

OK for Trunk, I assume waiting for 4.4 since this is not a regression fix.

Jerry

2008-01-24 Jerry DeLisle <jvdelisle@gcc.gnu.org>

	PR libfortran/34876
	* io/transfer.c (write_buf): Handle case of zero sized array.
	(transfer_array): Set data pointer to NULL and size to zero.  Then
	make a data transfer and return.
Index: transfer.c
===================================================================
--- transfer.c	(revision 131816)
+++ transfer.c	(working copy)
@@ -638,6 +638,14 @@ write_buf (st_parameter_dt *dtp, void *b
 	  return FAILURE;
 	}
 
+      if (buf == NULL && nbytes == 0)
+	{
+	   char *p;
+	   p = write_block (dtp, dtp->u.p.current_unit->recl);
+	   memset (p, 0, dtp->u.p.current_unit->recl);
+	   return SUCCESS;
+	}
+
       if (swrite (dtp->u.p.current_unit->s, buf, &nbytes) != 0)
 	{
 	  generate_error (&dtp->common, LIBERROR_OS, NULL);
@@ -648,7 +656,6 @@ write_buf (st_parameter_dt *dtp, void *b
       dtp->u.p.current_unit->bytes_left -= (gfc_offset) nbytes;
 
       return SUCCESS;
-
     }
 
   /* Unformatted sequential.  */
@@ -1507,9 +1514,15 @@ transfer_array (st_parameter_dt *dtp, gf
       extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
 
       /* If the extent of even one dimension is zero, then the entire
-	 array section contains zero elements, so we return.  */
+	 array section contains zero elements, so we return after writing
+	 a zero array record.  */
       if (extent[n] <= 0)
-	return;
+	{
+	  data = NULL;
+	  tsize = 0;
+	  dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
+	  return;
+	}
     }
 
   stride0 = stride[0];
! { dg-do run }
! PR34876 can't read/write zero length array sections
! Test case from PR by Dick Hendrikson
      program qi0011
      character(9) bda(10)
      character(9) bda1(10)
      integer  j_len
      istat = -314

      inquire(iolength = j_len) bda1

      istat = -314
      open (unit=48,
     $      status='scratch',
     $      access='direct',
     $      recl = j_len,
     $      iostat = istat,
     $      form='unformatted',
     $      action='readwrite')


      if (istat /= 0) call abort

      bda  = 'xxxxxxxxx'
      bda1 = 'yyyyyyyyy'
      write (48,iostat = istat, rec = 10) bda1(4:3)
      if ( istat .ne. 0) then
        call abort
      endif

      istat = -314
      read (48,iostat = istat, rec=10) bda(4:3)
      if ( istat .ne. 0) then
        call abort
      endif
      if (any(bda1.ne.'yyyyyyyyy')) call abort
      if (any(bda.ne.'xxxxxxxxx')) call abort
      end


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