Skip to content

Commit a2747bd

Browse files
authored
Don't use File.OpenWrite when trying to overwrite a file (#15930)
File.OpenWrite will open an existing file which means if you write data that is shorter than the existing data the file will be corrupt. This is documented on https://learn.microsoft.com/en-us/dotnet/api/system.io.file.openwrite > The OpenWrite method opens a file if one already exists for the file path, or creates a new file if one does not exist. For an existing file, it does not append the new text to the existing text. Instead, it overwrites the existing characters with the new characters. If you overwrite a longer string (such as "This is a test of the OpenWrite method") with a shorter string (such as "Second run"), the file will contain a mix of the strings ("Second runtest of the OpenWrite method"). The fix is to use `File.Create` instead in cases where the intention is to create a new file or overwrite an existing one.
1 parent 5fe4688 commit a2747bd

File tree

5 files changed

+5
-5
lines changed

5 files changed

+5
-5
lines changed

src/Microsoft.DotNet.Build.Tasks.Installers/src/CreateChangelogFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public sealed class CreateChangelogFile : BuildTask
3737

3838
public override bool Execute()
3939
{
40-
using GZipStream stream = new(File.OpenWrite(ChangelogOutputPath), CompressionLevel.Optimal);
40+
using GZipStream stream = new(File.Create(ChangelogOutputPath), CompressionLevel.Optimal);
4141
using StreamWriter writer = new(stream, Encoding.ASCII);
4242
writer.WriteLine($"{PackageName} ({PackageVersion}) unstable; urgency=low");
4343
writer.WriteLine();

src/Microsoft.DotNet.Build.Tasks.Installers/src/CreateControlFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public sealed class CreateControlFile : BuildTask
4242

4343
public override bool Execute()
4444
{
45-
using Stream stream = File.OpenWrite(ControlFileOutputPath);
45+
using Stream stream = File.Create(ControlFileOutputPath);
4646
using StreamWriter writer = new(stream, Encoding.ASCII);
4747
writer.WriteLine($"Package: {PackageName}");
4848
writer.WriteLine($"Version: {PackageVersion}");

src/Microsoft.DotNet.Build.Tasks.Installers/src/CreateMD5SumsFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public sealed class CreateMD5SumsFile : BuildTask
2929

3030
public override bool Execute()
3131
{
32-
using FileStream outputFile = File.OpenWrite(OutputFile);
32+
using FileStream outputFile = File.Create(OutputFile);
3333
using StreamWriter writer = new(outputFile, Encoding.ASCII);
3434
ulong installedSize = 0;
3535
foreach (ITaskItem file in Files)

src/Microsoft.DotNet.Build.Tasks.Installers/src/GenerateMacOSDistributionFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public override bool Execute()
117117
document.Root.Add(choiceElements);
118118
document.Root.Add(pkgRefElements);
119119
document.Root.Add(scriptElement);
120-
using XmlWriter writer = XmlWriter.Create(File.OpenWrite(DestinationFile));
120+
using XmlWriter writer = XmlWriter.Create(File.Create(DestinationFile));
121121
document.WriteTo(writer);
122122
}
123123
catch (Exception ex)

src/SignCheck/Microsoft.SignCheck/Verification/RpmVerifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override IEnumerable<ArchiveEntry> ReadArchiveEntries(string archivePa
2424

2525
using var stream = File.Open(archivePath, FileMode.Open);
2626
using RpmPackage rpmPackage = RpmPackage.Read(stream);
27-
using var dataStream = File.OpenWrite(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));
27+
using var dataStream = File.Create(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));
2828
using var archive = new CpioReader(rpmPackage.ArchiveStream, leaveOpen: false);
2929

3030
while (archive.GetNextEntry() is CpioEntry entry)

0 commit comments

Comments
 (0)