Skip to content

Commit

Permalink
Don't use File.OpenWrite when trying to overwrite a file (dotnet#93744)
Browse files Browse the repository at this point in the history
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 the `FileMode.Create` option in cases where the intention is to create a new file or overwrite an existing one.

I noticed this recently when using the illink feature to dump a dependency analysis xml, the next time I ran the build after making a change that'd reduce dependencies the xml would get corrupted since it wrote less data than the existing file had.

There were a couple instances across the codebase, this fixes them.
  • Loading branch information
akoeplinger authored Oct 20, 2023
1 parent 7d471fa commit 111b089
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ILAssemblyGeneratingMethodDebugInfoProvider(string fileName, DebugInforma
{
_wrappedProvider = wrappedProvider;
_fileName = fileName;
_tw = new StreamWriter(File.OpenWrite(fileName));
_tw = new StreamWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None));
}

public override MethodDebugInformation GetDebugInfo(MethodIL methodIL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override bool Execute()
ConvertDictionaryToBlob(configProperties, blobBuilder);

Directory.CreateDirectory(Path.GetDirectoryName(OutputFile!)!);
using var stream = File.OpenWrite(OutputFile);
using var stream = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None);
blobBuilder.WriteContentTo(stream);

return !Log.HasLoggedErrors;
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/WasmAppBuilder/WasmAppBuilderBaseTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected virtual void UpdateRuntimeConfigJson()
AddToRuntimeConfig(wasmHostProperties: wasmHostProperties, runtimeArgsArray: runtimeArgsArray, perHostConfigs: perHostConfigs);

string dstPath = Path.Combine(AppDir!, Path.GetFileName(runtimeConfigPath));
using FileStream? fs = File.OpenWrite(dstPath);
using FileStream? fs = new FileStream(dstPath, FileMode.Create, FileAccess.Write, FileShare.None);
using var writer = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true });
rootObject.WriteTo(writer);
_fileWrites.Add(dstPath);
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/WasmBuildTasks/GetChromeVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private async Task<Stream> GetDownloadFileStreamAsync(string filename, string ur
{
Log.LogMessage(MessageImportance.Low, $"Downloading {url} ...");
using Stream stream = await s_httpClient.GetStreamAsync(url).ConfigureAwait(false);
using FileStream fs = File.OpenWrite(filePath);
using FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
await stream.CopyToAsync(fs).ConfigureAwait(false);
}
catch (HttpRequestException hre)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DgmlDependencyRecorder (LinkContext context, string? fileName = null)
Directory.CreateDirectory (context.OutputDirectory);
}

var depsFile = File.OpenWrite (fileName);
var depsFile = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
stream = depsFile;

writer = XmlWriter.Create (stream, settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public XmlDependencyRecorder (LinkContext context, string? fileName = null)
Directory.CreateDirectory (context.OutputDirectory);
}

var depsFile = File.OpenWrite (fileName);
var depsFile = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
stream = depsFile;

writer = XmlWriter.Create (stream, settings);
Expand Down

0 comments on commit 111b089

Please sign in to comment.