This is the mail archive of the cygwin-patches mailing list for the Cygwin 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]

Re: Bug in pread/pwrite ?


On Mar  9 08:57, Christophe GRENIER wrote:
> On Thu, 8 Mar 2007, Christopher Faylor wrote:
> >All of that aside, I don't see how ignoring an lseek() failure
> >could be considered to be a good thing.
> 
> I have done more research since, have a look to this glibc
> pread implementation:
> 
> ssize_t
> __libc_pread (int fd, void *buf, size_t nbyte, off_t offset)
> {
>   /* Since we must not change the file pointer preserve the value so that
>      we can restore it later.  */
>   int save_errno;
>   ssize_t result;
>   off_t old_offset = __libc_lseek (fd, 0, SEEK_CUR);
>   if (old_offset == (off_t) -1)
>     return -1;
    ^^^^^^^^^^^^

>   /* Set to wanted position.  */
>   if (__libc_lseek (fd, offset, SEEK_SET) == (off_t) -1)
>     return -1;
    ^^^^^^^^^^^^

>   /* Write out the data.  */
>   result = __libc_read (fd, buf, nbyte);
> 
>   /* Now we have to restore the position.  If this fails we have to
>      return this as an error.  But if the writing also failed we
>      return this error.  */
>   save_errno = errno;
>   if (__libc_lseek (fd, old_offset, SEEK_SET) == (off_t) -1)
>     {
>       if (result == -1)
>         __set_errno (save_errno);
>       return -1;
        ^^^^^^^^^^

>     }
>   __set_errno (save_errno);
> 
>   return result;
> }
> 
> glibc implementation seems correct in ignoring lseek
> failure if read has been successfull.

This code does not at one point ignore the return code of lseek.
What it does is, it uses the return code of read if the read failed,
but a failing lseek always leads to pread returning -1.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat


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