Skip to content

Commit

Permalink
修复WinDivertPacket创建时申请两次内存的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 28, 2022
1 parent d1b97c8 commit 64ceda0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
3 changes: 3 additions & 0 deletions WindivertDotnet.Test/WinDivertAddressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public void CloneTest()

using var clone = addr.Clone();
Assert.Equal(addr.Flags, clone.Flags);

addr.Flags = WinDivertAddressFlag.IPv6;
Assert.NotEqual(addr.Flags, clone.Flags);
}
}
}
22 changes: 10 additions & 12 deletions WindivertDotnet/WinDivertPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public int Length
/// </summary>
/// <param name="capacity">最大容量</param>
public WinDivertPacket(int capacity = MTU_MAX)
: this(MemoryNative.AllocZeroed(capacity), capacity, ownsHandle: true)
: base(ownsHandle: true)
{
this.Capacity = capacity;
this.handle = MemoryNative.AllocZeroed(capacity);
Expand All @@ -69,14 +69,14 @@ public WinDivertPacket(int capacity = MTU_MAX)
/// <summary>
/// WinDivert的数据包
/// </summary>
/// <param name="handle"></param>
/// <param name="capacity"></param>
/// <param name="ownsHandle"></param>
private unsafe WinDivertPacket(IntPtr handle, int capacity, bool ownsHandle)
: base(ownsHandle)
/// <param name="handle">外部创建的句柄</param>
/// <param name="length">字节长度</param>
private unsafe WinDivertPacket(IntPtr handle, int length)
: base(ownsHandle: false)
{
this.handle = handle;
this.Capacity = capacity;
this.length = length;
this.Capacity = length;
}

/// <summary>
Expand Down Expand Up @@ -119,11 +119,7 @@ public unsafe Span<byte> GetSpan(int offset, int count)
public unsafe WinDivertPacket Slice(int offset, int count)
{
var pointer = this.GetPointer(offset, count);
var handle = new IntPtr(pointer);
return new WinDivertPacket(handle, count, ownsHandle: false)
{
length = count
};
return new WinDivertPacket(new IntPtr(pointer), count);
}


Expand All @@ -134,6 +130,7 @@ public unsafe WinDivertPacket Slice(int offset, int count)
/// <param name="count"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void* GetPointer(int offset, int count)
{
if (offset < 0 || offset > this.Capacity)
Expand Down Expand Up @@ -238,6 +235,7 @@ public bool CalcLoopbackFlag(WinDivertAddress addr)
/// <param name="srcAddr"></param>
/// <param name="dstAddr"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe bool TryParseIPAddress(
[MaybeNullWhen(false)] out IPAddress srcAddr,
[MaybeNullWhen(false)] out IPAddress dstAddr)
Expand Down
2 changes: 1 addition & 1 deletion WindivertDotnet/WindivertDotnet.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<Nullable>enable</Nullable>
<NoWarn>CA2012;IDE0079</NoWarn>
Expand Down

0 comments on commit 64ceda0

Please sign in to comment.