Skip to content

Commit 2801aa0

Browse files
fix (DfuInstallation::OnProgressChanged()): fix a corner-case bug caused by division by zero during progress-estimation
[SKNG-4818]
1 parent df785eb commit 2801aa0

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Laerdal.Dfu/DfuInstallation.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,15 @@ protected SharedDfuInstallation(string deviceId, string fileUrl) : this()
178178
internal void OnProgressChanged(double progress, double currentSpeedBytesPerSecond, double avgSpeedBytesPerSecond)
179179
{
180180
Progress = progress;
181-
CurrentSpeedBytesPerSecond = currentSpeedBytesPerSecond;
182181
AvgSpeedBytesPerSecond = avgSpeedBytesPerSecond;
183-
182+
CurrentSpeedBytesPerSecond = currentSpeedBytesPerSecond;
183+
184+
var roundedProgress = (long)Math.Round(Progress * 100); //can in fact turn out to be zero if progress is 0.0001
184185
if (Progress >= 1 || State == DfuState.Aborted || Error != DfuError.NoError) // Done or Error
185186
{
186187
EstimatedTimeLeft = TimeSpan.Zero;
187188
}
188-
else if (Progress <= 0) // Not started
189+
else if (roundedProgress <= 0) // Not started
189190
{
190191
EstimatedTimeLeft = TimeSpan.Zero;
191192
}
@@ -194,7 +195,7 @@ internal void OnProgressChanged(double progress, double currentSpeedBytesPerSeco
194195
StartTime = StartTime == default ? DateTime.UtcNow : StartTime;
195196
var startTime = StartTime; // Force initialization of field by calling Get
196197
Duration = DateTime.UtcNow - startTime;
197-
var ticksPerProgressPercent = Duration.Ticks / (long) Math.Round(Progress * 100);
198+
var ticksPerProgressPercent = Duration.Ticks / roundedProgress;
198199
var ticksTotal = ticksPerProgressPercent * 100;
199200
var ticksLeft = ticksTotal - Duration.Ticks;
200201
EstimatedTimeLeft = TimeSpan.FromTicks(ticksLeft);

0 commit comments

Comments
 (0)