Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid leaving file handles open #65

Merged
merged 1 commit into from
Apr 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/coverlet.core/Helpers/InstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static string[] GetDependencies(string module)

public static bool HasPdb(string module)
{
using (var peReader = new PEReader(File.OpenRead(module)))
using (var moduleStream = File.OpenRead(module))
using (var peReader = new PEReader(moduleStream))
{
foreach (var entry in peReader.ReadDebugDirectory())
{
Expand Down
37 changes: 18 additions & 19 deletions src/coverlet.core/Instrumentation/Instrumenter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Coverlet.Core.Helpers;
using Coverlet.Core.Extensions;

using Mono.Cecil;
using Mono.Cecil.Cil;
Expand All @@ -13,8 +11,8 @@ namespace Coverlet.Core.Instrumentation
{
internal class Instrumenter
{
private string _module;
private string _identifier;
private readonly string _module;
private readonly string _identifier;
private InstrumenterResult _result;

public Instrumenter(string module, string identifier)
Expand Down Expand Up @@ -48,25 +46,26 @@ public InstrumenterResult Instrument()
private void InstrumentModule()
{
using (var stream = new FileStream(_module, FileMode.Open, FileAccess.ReadWrite))
using (var resolver = new DefaultAssemblyResolver())
{
var resolver = new DefaultAssemblyResolver();
resolver.AddSearchDirectory(Path.GetDirectoryName(_module));
var parameters = new ReaderParameters { ReadSymbols = true, AssemblyResolver = resolver };
ModuleDefinition module = ModuleDefinition.ReadModule(stream, parameters);

foreach (var type in module.GetTypes())
using (var module = ModuleDefinition.ReadModule(stream, parameters))
{
if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
continue;

foreach (var method in type.Methods)
foreach (var type in module.GetTypes())
{
if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
InstrumentMethod(method);
if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
continue;

foreach (var method in type.Methods)
{
if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
InstrumentMethod(method);
}
}
}

module.Write(stream);
module.Write(stream);
}
}
}

Expand Down Expand Up @@ -138,7 +137,7 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
return pathInstr;
}

private bool IsBranchTarget(ILProcessor processor, Instruction instruction)
private static bool IsBranchTarget(ILProcessor processor, Instruction instruction)
{
foreach (var _instruction in processor.Body.Instructions)
{
Expand All @@ -155,7 +154,7 @@ private bool IsBranchTarget(ILProcessor processor, Instruction instruction)
return false;
}

private void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget)
private static void ReplaceInstructionTarget(Instruction instruction, Instruction oldTarget, Instruction newTarget)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason you made this static?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason aside from them not relying on any fields in the class. It's not a functional change in behavior, mostly cosmetic.

{
if (instruction.Operand is Instruction _instruction)
{
Expand All @@ -175,7 +174,7 @@ private void ReplaceInstructionTarget(Instruction instruction, Instruction oldTa
}
}

private void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget)
private static void ReplaceExceptionHandlerBoundary(ExceptionHandler handler, Instruction oldTarget, Instruction newTarget)
{
if (handler.FilterStart == oldTarget)
handler.FilterStart = newTarget;
Expand Down