From 60a0427a7385e89c87c84923dab026604e0da844 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Thu, 26 Jan 2023 18:28:47 -0800 Subject: [PATCH] Fix progress calculation divide by zero in Copy-Item --- .../namespaces/FileSystemProvider.cs | 2 +- .../Microsoft.PowerShell.Management/FileSystem.Tests.ps1 | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 60631f7478b..ddd738def75 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -3938,7 +3938,7 @@ private void CopyFileInfoItem(FileInfo file, string destinationPath, bool force, StringUtil.Format(FileSystemProviderStrings.CopyingLocalFileActivity, _copiedFiles, _totalFiles), StringUtil.Format(FileSystemProviderStrings.CopyingLocalBytesStatus, Utils.DisplayHumanReadableFileSize(_copiedBytes), Utils.DisplayHumanReadableFileSize(_totalBytes), speed) ); - var percentComplete = (int)Math.Min(_copiedBytes * 100 / _totalBytes, 100); + var percentComplete = _totalBytes != 0 ? (int)Math.Min(_copiedBytes * 100 / _totalBytes, 100) : 100; progress.PercentComplete = percentComplete; progress.RecordType = ProgressRecordType.Processing; WriteProgress(progress); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 5e55630c06c..8f13cb48662 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -317,6 +317,12 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" { Remove-Item -Path $testPath -Recurse -Force -ErrorAction SilentlyContinue } } + + It "Copy-Item can copy 0 byte length file" { + $zeroLengthFile = New-Item -Path (Join-Path $TestDrive "zeroLengthFile.txt") -ItemType File -Force + Copy-Item -Path $zeroLengthFile -Destination "$TestDrive\zeroLengthFile2.txt" -Force + "$TestDrive\zeroLengthFile2.txt" | Should -Exist + } } Context "Validate behavior when access is denied" {