Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
fix #179 AccessViolation in sourcelink test (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctaggart authored Mar 26, 2017
1 parent 47589a7 commit 91b5c77
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
1 change: 1 addition & 0 deletions dotnet-sourcelink-git/dotnet-sourcelink-git.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PackageType>DotnetCliTool</PackageType>
<RootNamespace>SourceLink.Git</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\dotnet-sourcelink\SourceLinkJson.cs" Link="SourceLinkJson.cs" />
Expand Down
55 changes: 55 additions & 0 deletions dotnet-sourcelink/DebugReaderProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;

namespace SourceLink
{
public class DebugReaderProvider : IDisposable
{
private Stream stream;
private MetadataReaderProvider provider;

public DebugReaderProvider(string path)
{
stream = File.OpenRead(path);
if (path.EndsWith(".dll"))
{
var reader = new PEReader(stream);
if (reader.HasMetadata)
{
// https://github.com/dotnet/corefx/blob/master/src/System.Reflection.Metadata/tests/PortableExecutable/PEReaderTests.cs#L392
var debugDirectoryEntries = reader.ReadDebugDirectory();
if (debugDirectoryEntries.Length < 3) return;
provider = reader.ReadEmbeddedPortablePdbDebugDirectoryData(debugDirectoryEntries[2]);
}
}
else
{
provider = MetadataReaderProvider.FromPortablePdbStream(stream);
}
}

public MetadataReader GetMetaDataReader()
{
return provider.GetMetadataReader();
}

// Basic Dispose Pattern https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx#Anchor_0
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (provider != null) provider.Dispose();
if (stream != null) stream.Dispose();
}
}
}


}
33 changes: 6 additions & 27 deletions dotnet-sourcelink/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Reflection.PortableExecutable;

namespace SourceLink {
public class Program
Expand Down Expand Up @@ -234,33 +233,11 @@ public static void Test(CommandLineApplication command)

public static readonly Guid SourceLinkId = new Guid("CC110556-A091-4D38-9FEC-25AB9A351A6A");

public static MetadataReader GetMetaDataReader(string path, Stream stream)
{
if (path.EndsWith(".dll"))
{
var reader = new PEReader(stream);
if (reader.HasMetadata)
{
// https://github.com/dotnet/corefx/blob/master/src/System.Reflection.Metadata/tests/PortableExecutable/PEReaderTests.cs#L392
var debugDirectoryEntries = reader.ReadDebugDirectory();
if (debugDirectoryEntries.Length < 3) return null;
var embeddedProvider = reader.ReadEmbeddedPortablePdbDebugDirectoryData(debugDirectoryEntries[2]);
return embeddedProvider.GetMetadataReader();
}
}
else
{
var mrp = MetadataReaderProvider.FromPortablePdbStream(stream);
return mrp.GetMetadataReader();
}
return null;
}

public static byte[] GetSourceLinkBytes(string path)
{
using (var file = File.OpenRead(path))
using (var drp = new DebugReaderProvider(path))
{
var mr = GetMetaDataReader(path, file);
var mr = drp.GetMetaDataReader();
if (mr == null) return null;
var blobh = default(BlobHandle);
foreach (var cdih in mr.GetCustomDebugInformation(EntityHandle.ModuleDefinition))
Expand Down Expand Up @@ -289,12 +266,14 @@ public static bool IsEmbedded(MetadataReader mr, DocumentHandle dh)

public static IEnumerable<Document> GetDocuments(string path)
{
using (var file = File.OpenRead(path))
using (var drp = new DebugReaderProvider(path))
{
var mr = GetMetaDataReader(path, file);
var mr = drp.GetMetaDataReader();
foreach (var dh in mr.Documents)
{
if (dh.IsNil) continue;
var d = mr.GetDocument(dh);
if (d.Name.IsNil || d.Language.IsNil || d.HashAlgorithm.IsNil || d.Hash.IsNil) continue;
yield return new Document
{
Name = mr.GetString(d.Name),
Expand Down
1 change: 1 addition & 0 deletions dotnet-sourcelink/dotnet-sourcelink.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PackageType>DotnetCliTool</PackageType>
<RootNamespace>SourceLink</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.0" />
Expand Down

0 comments on commit 91b5c77

Please sign in to comment.