[filesystem] Possible problem in copy_file

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[filesystem] Possible problem in copy_file

G. Wade Johnson-2
I was looking in the code for copy_file() and noticed what may be a bug. In
the loop that handles partial writes, the code makes multiple attempts
reducing the amount of data written each time. Unfortunately, the offset into
the data is not changed as well. See below.

        sz_write = 0;
        do
        {
          if ( (sz = ::write( outfile, buf.get(), sz_read - sz_write )) < 0 )
          {
            sz_read = sz; // cause read loop termination
            break;        //  and error to be thrown after closes
          }
          sz_write += sz;
        } while ( sz_write < sz_read );

Unless I'm missing something, it looks like a partial write will result in the
first part of the buffer being written multiple times. I checked the version
in CVS and it appears to have the same code.

Am I missing something or is this incorrect?

G. Wade
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [filesystem] Possible problem in copy_file

Beman Dawes

"G. Wade Johnson" <[hidden email]> wrote in message
news:20060118204932.37feb318@sovvan...

>I was looking in the code for copy_file() and noticed what may be a bug. In
> the loop that handles partial writes, the code makes multiple attempts
> reducing the amount of data written each time. Unfortunately, the offset
> into
> the data is not changed as well. See below.
>
>        sz_write = 0;
>        do
>        {
>          if ( (sz = ::write( outfile, buf.get(), sz_read - sz_write )) <
> 0 )
>          {
>            sz_read = sz; // cause read loop termination
>            break;        //  and error to be thrown after closes
>          }
>          sz_write += sz;
>        } while ( sz_write < sz_read );
>
> Unless I'm missing something, it looks like a partial write will result in
> the
> first part of the buffer being written multiple times. I checked the
> version
> in CVS and it appears to have the same code.
>
> Am I missing something or is this incorrect?

Thanks for the report. It will few more days before I can investigate.

--Beman



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [filesystem] Possible problem in copy_file

Beman Dawes
In reply to this post by G. Wade Johnson-2
G. Wade Johnson wrote:

> I was looking in the code for copy_file() and noticed what may be a bug. In
> the loop that handles partial writes, the code makes multiple attempts
> reducing the amount of data written each time. Unfortunately, the offset into
> the data is not changed as well. See below.
>
>         sz_write = 0;
>         do
>         {
>           if ( (sz = ::write( outfile, buf.get(), sz_read - sz_write )) < 0 )
>           {
>             sz_read = sz; // cause read loop termination
>             break;        //  and error to be thrown after closes
>           }
>           sz_write += sz;
>         } while ( sz_write < sz_read );
>
> Unless I'm missing something, it looks like a partial write will result in the
> first part of the buffer being written multiple times. I checked the version
> in CVS and it appears to have the same code.
>
> Am I missing something or is this incorrect?

A bug for sure. Nice catch. Fixed in CVS.

At least I hope it is fixed. I don't see an easy way to test it.

The code now reads:

           sz_write = 0;
           do
           {
             if ( (sz = ::write( outfile, buf.get()+sz_write,
               sz_read - sz_write )) < 0 )
             {
               sz_read = sz; // cause read loop termination
               break;        //  and error to be thrown after closes
             }
             sz_write += sz;
           } while ( sz_write < sz_read );

Thanks,

--Beman
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost