Skip to content

Commit

Permalink
Fixed: A case of leaked handle in RegisterBf and RegisterSpd
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Jan 12, 2024
1 parent a2e7b8e commit c5c9c0a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
6 changes: 5 additions & 1 deletion Emulator/BF.File.Emulator/BfEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ public void OnModLoading(string modFolder)
/// Invalidates a BF file with a specified name.
/// </summary>
/// <param name="bfPath">Full path to the file.</param>
public void UnregisterFile(string bfPath) => _pathToStream!.Remove(bfPath, out _);
public void UnregisterFile(string bfPath)
{
_pathToStream!.Remove(bfPath, out var stream);
stream?.Dispose();
}

public void RegisterFile(string destinationPath, Stream stream)
{
Expand Down
10 changes: 3 additions & 7 deletions Emulator/BF.File.Emulator/BfEmulatorApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,9 @@ public void RegisterBf(string sourcePath, string destinationPath)
}

Native.SetFilePointerEx(handle, 0, IntPtr.Zero, 0);

var fileStream = new FileStream(new SafeFileHandle(handle, false), FileAccess.Read);
var stream = StreamUtils.CreateMemoryStream(fileStream.Length);
fileStream.CopyTo(stream);

var emulated = new EmulatedFile<Stream>(stream);
_bfEmulator.RegisterFile(destinationPath, stream);
var fileStream = new FileStream(new SafeFileHandle(handle, true), FileAccess.Read);
var emulated = new EmulatedFile<FileStream>(fileStream);
_bfEmulator.RegisterFile(destinationPath, fileStream);
_framework.RegisterVirtualFile(destinationPath, emulated, false);

_logger.Info("[BfEmulatorApi] Registered bf {0} at {1}", sourcePath, destinationPath);
Expand Down
6 changes: 5 additions & 1 deletion Emulator/SPD.File.Emulator/SpdEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ private void DumpFile(string filepath, MultiStream stream)
/// Invalidates a SPD file with a specified name.
/// </summary>
/// <param name="spdPath">Full path to the file.</param>
public void UnregisterFile(string spdPath) => _pathToStream.Remove(spdPath, out _);
public void UnregisterFile(string spdPath)
{
_pathToStream.Remove(spdPath, out var stream);
stream?.Dispose();
}

public void RegisterFile(string destinationPath, Stream stream)
{
Expand Down
8 changes: 4 additions & 4 deletions Emulator/SPD.File.Emulator/SpdEmulatorApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public void RegisterSpd(string sourcePath, string destinationPath)

_ = Native.SetFilePointerEx(handle, 0, IntPtr.Zero, 0);

var fileStream = new FileStream(new SafeFileHandle(handle, false), FileAccess.Read);

var emulated = new EmulatedFile<Stream>(fileStream);
Native.SetFilePointerEx(handle, 0, IntPtr.Zero, 0);
var fileStream = new FileStream(new SafeFileHandle(handle, true), FileAccess.Read);
var emulated = new EmulatedFile<FileStream>(fileStream);
_spdEmulator.RegisterFile(destinationPath, fileStream);
_framework.RegisterVirtualFile(destinationPath, emulated);
_framework.RegisterVirtualFile(destinationPath, emulated, false);

_logger.Info("[SpdEmulatorApi] Registered spd {0} at {1}", sourcePath, destinationPath);
}
Expand Down

0 comments on commit c5c9c0a

Please sign in to comment.