Skip to content

Commit

Permalink
Prevent the checked arithmetic cast from nint to void* doesn't cause …
Browse files Browse the repository at this point in the history
…an OverflowException of 32-bit architectures.

The pre-existing code compiles to:

`new Span<byte>((void*)checked((UIntPtr)handle), _size);`

because CheckForOverflowUnderflow is enabled on the project.

This causes an OverflowException on 32-bit architectures when attempting to perform a checked cast to UIntPtr, likely because the high bit of the 4-byte pointer is set, making it technically a negative number.
  • Loading branch information
enclave-alistair committed Apr 9, 2024
1 parent 40b23f4 commit 9bfa3c4
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Interop/Interop.SecureMemoryHandle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

internal static partial class Interop
Expand Down Expand Up @@ -73,7 +74,7 @@ public void CopyTo(

public unsafe Span<byte> DangerousGetSpan()
{
return new Span<byte>((void*)handle, _size);
return new Span<byte>(handle.ToPointer(), _size);
}

protected override bool ReleaseHandle()
Expand Down

0 comments on commit 9bfa3c4

Please sign in to comment.