Don't dispose internal binary reader until PeUtility is disposed too.
MODIFY
WalletWasabi.Packager/NSubsys/PeUtility.cs
WalletWasabi.Packager/NSubsys/PeUtility.cs
@@ -7,7 +7,7 @@ namespace NSubsys
{
internal class PeUtility : IDisposable
{
- public enum SubSystemType : UInt16
+ public enum SubSystemType : ushort
{
IMAGE_SUBSYSTEM_WINDOWS_GUI = 2,
IMAGE_SUBSYSTEM_WINDOWS_CUI = 3,
@@ -17,34 +17,47 @@ namespace NSubsys
public struct IMAGE_DOS_HEADER
{
[FieldOffset(60)]
- public UInt32 e_lfanew;
+ public uint e_lfanew;
}
[StructLayout(LayoutKind.Explicit)]
public struct IMAGE_OPTIONAL_HEADER
{
[FieldOffset(68)]
- public UInt16 Subsystem;
+ public ushort Subsystem;
}
- private readonly long fileHeaderOffset;
- private IMAGE_OPTIONAL_HEADER optionalHeader;
- private readonly FileStream curFileStream;
+
+ /// <summary>
+ /// Gets the optional header
+ /// </summary>
+ public IMAGE_OPTIONAL_HEADER OptionalHeader { get; }
+
+ /// <summary>
+ /// Gets the PE file stream for R/W functions.
+ /// </summary>
+ public FileStream Stream { get; }
+
+ public long MainHeaderOffset { get; }
+
+ private readonly IDisposable _internalBinReader;
public PeUtility(string filePath)
{
- curFileStream = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
+ Stream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
+
+ var reader = new BinaryReader(Stream);
- using (var reader = new BinaryReader(curFileStream))
- {
var dosHeader = FromBinaryReader<IMAGE_DOS_HEADER>(reader);
// Seek the new PE Header and skip NtHeadersSignature (4 bytes) & IMAGE_FILE_HEADER struct (20bytes).
- curFileStream.Seek(dosHeader.e_lfanew + 4 + 20, SeekOrigin.Begin);
+ Stream.Seek(dosHeader.e_lfanew + 4 + 20, SeekOrigin.Begin);
- fileHeaderOffset = curFileStream.Position;
- optionalHeader = FromBinaryReader<IMAGE_OPTIONAL_HEADER>(reader);
- }
+ MainHeaderOffset = Stream.Position;
+
+ OptionalHeader = FromBinaryReader<IMAGE_OPTIONAL_HEADER>(reader);
+
+ _internalBinReader = reader;
}
/// <summary>
@@ -69,28 +82,8 @@ namespace NSubsys
public void Dispose()
{
- curFileStream?.Dispose();
- }
-
- /// <summary>
- /// Gets the optional header
- /// </summary>
- public IMAGE_OPTIONAL_HEADER OptionalHeader
- {
- get => optionalHeader;
- }
-
- /// <summary>
- /// Gets the PE file stream for R/W functions.
- /// </summary>
- public FileStream Stream
- {
- get => curFileStream;
- }
-
- public long MainHeaderOffset
- {
- get => fileHeaderOffset;
+ Stream?.Dispose();
+ _internalBinReader?.Dispose();
}
}
}
\ No newline at end of file

Don't dispose internal binary reader until PeUtility is disposed too.