1
Vote

Percentage overflow in TftpTransferProgress.cs

description

Hi,
I found an issue when transferring large files, than an overflow occurs in the calculating of the progress percentage.

The problem is on line 29 in TftpTransferProgress.cs:
return (TransferredBytes * 100) / TotalBytes + "% completed"
TransferredBytes *100 can cause an int overflow.


My fix:
        /// <summary>
        /// Gets transfer progress in percent.
        /// </summary>
        /// <returns>The progress percentage.</returns>
        public int ProgressPercentage()
        {
            if (TotalBytes > 0)
            {
                return unchecked((int)(TransferredBytes * 100L / TotalBytes));
            }

            return 0;
        }

        public override string ToString()
        {
            if (TotalBytes > 0)
            {
                return ProgressPercentage() + "% completed";
            }
            
            return TransferredBytes + " bytes transferred";
        }

comments