Bug in SCP code

2008-09-04
2013-05-28
  • Bob Lamoureux

    Bob Lamoureux - 2008-09-04

    I believe I have found a bug in the SCP.cs code

    In the SCP_receiveFile method, there is a loop that does

    ...
    ...
    int len = server.Read(buf, 0, foo);
    ...
    ...

    where foo is either the length of the buffer (1024) or the remaining bytes of the file.

    The remaining code in the loop assumes that len will alwyas be equal to foo which in some network latency cases, this is not true.

    So instead of subtracting foo from the filesize and also in the fos.Write(...) call, len should be used instead of foo

     
  • Jon Knight

    Jon Knight - 2011-06-06

    I also found this, which was corrupting certain files.  Changed the related procedure as follows:

            /// <summary>
            /// Transfer a file from the remote server
            /// </summary>
            /// <param name="server">A connected server I/O stream</param>
            /// <param name="rfile">The remote file to copy</param>
            /// <param name="lfile">The local destination path</param>
            protected void SCP_ReceiveFile(Stream server, string rfile, string lfile, int size)
            {
                int copied = 0;
                SendStartMessage(rfile, lfile, size, "Connected, starting transfer.");
                // read a content of lfile
                FileStream fos=File.OpenWrite(lfile);
                int filesize=size;
                byte[] buf = new byte[1024];
                int bufferLength = buf.Length;
                while(!m_cancelled)
                {
                    if (bufferLength > filesize)
                        bufferLength = filesize;
                    int len=server.Read(buf, 0, bufferLength);
                    copied += len;
                    fos.Write(buf, 0, len);
                    SendProgressMessage(rfile, lfile, copied, size, "Transferring...");
                    filesize-=len;
                    if(filesize==0) break;
                }
                fos.Close();
                if(m_cancelled)
                    return;
                SCP_CheckAck(server);
                SendEndMessage(rfile, lfile, copied, size, "Transfer completed successfully ("+filesize+" bytes).");
    

    }

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks