I found that in read_buf where raw_read is called, no checks for errors were
being made,  raw_read returns the number of bytes read or an error code.  In the
test case, an error occurs and we proceeded to use the resulting error code as
if it were the number of bytes read.

The attached patch fixes this.  Regression tested on x86_64.  New test case
provided.

OK for trunk?

Regards,

Jerry

2015-08-28 Jerry DeLisle  <jvdeli...@gcc.gnu.org>

        PR libgfortran/67367
        * io/unix.c (buf_read): Check for error condition and if found
        return the error code.



Index: unix.c
===================================================================
--- unix.c	(revision 227314)
+++ unix.c	(working copy)
@@ -529,16 +529,26 @@ buf_read (unix_stream * s, void * buf, ssize_t nby
       if (to_read <= BUFFER_SIZE/2)
         {
           did_read = raw_read (s, s->buffer, BUFFER_SIZE);
-          s->physical_offset += did_read;
-          s->active = did_read;
-          did_read = (did_read > to_read) ? to_read : did_read;
-          memcpy (p, s->buffer, did_read);
+	  if (likely (did_read >= 0))
+	    {
+	      s->physical_offset += did_read;
+	      s->active = did_read;
+	      did_read = (did_read > to_read) ? to_read : did_read;
+	      memcpy (p, s->buffer, did_read);
+	    }
+	  else
+	    return did_read;
         }
       else
         {
           did_read = raw_read (s, p, to_read);
-          s->physical_offset += did_read;
-          s->active = 0;
+	  if (likely (did_read >= 0))
+	    {
+	      s->physical_offset += did_read;
+	      s->active = 0;
+	    }
+	  else
+	    return did_read;
         }
       nbyte = did_read + nread;
     }
! { dg-do run }
! PR67367
program bug
   implicit none
   character(len=1) :: c
   character(len=256) :: message
   integer ios
   call system('[ -d junko.dir ] || mkdir junko.dir')
   open(unit=10, file='junko.dir',iostat=ios,action='read',access='stream')
   if (ios.ne.0) call abort
   read(10, iostat=ios) c
   if (ios.ne.21) call abort
   call system('rmdir junko.dir')
end program bug

Reply via email to