Skip to content

Commit

Permalink
Merge pull request #84 from 0xced/machO-shouldOwnStream
Browse files Browse the repository at this point in the history
Fix shouldOwnStream argument of MachO instances
  • Loading branch information
konrad-kruczynski authored Apr 7, 2022
2 parents 4e3ccb6 + 6de944f commit 8255a90
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ELFSharp/MachO/MachO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal MachO(Stream stream, bool is64, Endianess endianess, bool ownsStream)
{
this.is64 = is64;

using var reader = new SimpleEndianessAwareReader(stream, endianess, ownsStream);
using var reader = new SimpleEndianessAwareReader(stream, endianess, !ownsStream);

Machine = (Machine)reader.ReadInt32();
reader.ReadBytes(4); // we don't support the cpu subtype now
Expand Down
18 changes: 18 additions & 0 deletions Tests/ELF/OpeningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ public void ShouldOpenEmptyStringTableElf()
var section = elf.GetSection(".dynstr");
Assert.AreEqual(SectionType.NoBits, section.Type);
}

[Test]
public void ShouldDisposeStream()
{
var isDisposed = false;
var stream = new StreamWrapper(Utilities.GetBinaryStream("hello32le"), () => isDisposed = true);
ELFReader.Load(stream, shouldOwnStream: true).Dispose();
Assert.True(isDisposed);
}

[Test]
public void ShouldNotDisposeStream()
{
var isDisposed = false;
var stream = new StreamWrapper(Utilities.GetBinaryStream("hello32le"), () => isDisposed = true);
ELFReader.Load(stream, shouldOwnStream: false).Dispose();
Assert.False(isDisposed);
}
}
}

18 changes: 18 additions & 0 deletions Tests/MachO/OpeningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ public void ShouldOpenMachOObjectFile()
Assert.AreEqual(machO.FileType, FileType.Object);
Assert.AreEqual(machO.GetCommandsOfType<Segment>().Count(), 1);
}

[Test]
public void ShouldDisposeStream()
{
var isDisposed = false;
var stream = new StreamWrapper(Utilities.GetBinaryStream("simple-mach-o"), () => isDisposed = true);
MachOReader.Load(stream, shouldOwnStream: true);
Assert.True(isDisposed);
}

[Test]
public void ShouldNotDisposeStream()
{
var isDisposed = false;
var stream = new StreamWrapper(Utilities.GetBinaryStream("simple-mach-o"), () => isDisposed = true);
MachOReader.Load(stream, shouldOwnStream: false);
Assert.False(isDisposed);
}
}
}

47 changes: 47 additions & 0 deletions Tests/StreamWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.IO;

namespace Tests
{
public class StreamWrapper : Stream
{
private readonly Stream stream;
private readonly Action onDispose;

public StreamWrapper(Stream stream, Action onDispose)
{
this.stream = stream;
this.onDispose = onDispose;
}

public override void Flush() => stream.Flush();

public override int Read(byte[] buffer, int offset, int count) => stream.Read(buffer, offset, count);

public override long Seek(long offset, SeekOrigin origin) => stream.Seek(offset, origin);

public override void SetLength(long value) => stream.SetLength(value);

public override void Write(byte[] buffer, int offset, int count) => stream.Write(buffer, offset, count);

public override bool CanRead => stream.CanRead;

public override bool CanSeek => stream.CanSeek;

public override bool CanWrite => stream.CanWrite;

public override long Length => stream.Length;

public override long Position
{
get => stream.Position;
set => stream.Position = value;
}

protected override void Dispose(bool disposing)
{
onDispose();
base.Dispose(disposing);
}
}
}

0 comments on commit 8255a90

Please sign in to comment.