Skip to content

Commit

Permalink
Fix NREs on netstandard2.0 while reading empty strings. (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Apr 16, 2023
1 parent ab86c36 commit 24e3e8d
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/Tmds.DBus.Protocol/Netstandard2_0Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,58 @@ static partial class NetstandardExtensions

public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan<char> chars, Span<byte> bytes)
{
fixed (char* pChars = chars)
fixed (byte* pBytes = bytes)
fixed (char* pChars = &GetNonNullPinnableReference(chars))
fixed (byte* pBytes = &GetNonNullPinnableReference(bytes))
{
return encoding.GetBytes(pChars, chars.Length, pBytes, bytes.Length);
}
}

public static unsafe int GetChars(this Encoding encoding, ReadOnlySpan<byte> bytes, Span<char> chars)
{
fixed (char* pChars = chars)
fixed (byte* pBytes = bytes)
fixed (char* pChars = &GetNonNullPinnableReference(chars))
fixed (byte* pBytes = &GetNonNullPinnableReference(bytes))
{
return encoding.GetChars(pBytes, bytes.Length, pChars, chars.Length);
}
}

public static unsafe string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes)
{
fixed (byte* pBytes = bytes)
fixed (byte* pBytes = &GetNonNullPinnableReference(bytes))
{
return encoding.GetString(pBytes, bytes.Length);
}
}

public static unsafe int GetCharCount(this Encoding encoding, ReadOnlySpan<byte> bytes)
{
fixed (byte* pBytes = bytes)
fixed (byte* pBytes = &GetNonNullPinnableReference(bytes))
{
return encoding.GetCharCount(pBytes, bytes.Length);
}
}

public static unsafe int GetByteCount(this Encoding encoding, ReadOnlySpan<char> chars)
{
fixed (char* pChars = chars)
fixed (char* pChars = &GetNonNullPinnableReference(chars))
{
return encoding.GetByteCount(pChars, chars.Length);
}
}

public static unsafe int GetByteCount(this Encoder encoder, ReadOnlySpan<char> chars, bool flush)
{
fixed (char* pChars = chars)
fixed (char* pChars = &GetNonNullPinnableReference(chars))
{
return encoder.GetByteCount(pChars, chars.Length, flush);
}
}

public static unsafe void Convert(this Encoder encoder, ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
{
fixed (char* pChars = chars)
fixed (byte* pBytes = bytes)
fixed (char* pChars = &GetNonNullPinnableReference(chars))
fixed (byte* pBytes = &GetNonNullPinnableReference(bytes))
{
encoder.Convert(pChars, chars.Length, pBytes, bytes.Length, flush, out charsUsed, out bytesUsed, out completed);
}
Expand Down Expand Up @@ -109,7 +109,22 @@ public static async ValueTask<int> SendAsync(this Socket socket, ReadOnlyMemory<

throw new NotSupportedException();
}

/// <summary>
/// Returns a reference to the 0th element of the Span. If the Span is empty, returns a reference to fake non-null pointer. Such a reference can be used
/// for pinning but must never be dereferenced. This is useful for interop with methods that do not accept null pointers for zero-sized buffers.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe ref T GetNonNullPinnableReference<T>(Span<T> span) => ref (span.Length != 0) ? ref MemoryMarshal.GetReference(span) : ref Unsafe.AsRef<T>((void*)1);

/// <summary>
/// Returns a reference to the 0th element of the ReadOnlySpan. If the ReadOnlySpan is empty, returns a reference to fake non-null pointer. Such a reference
/// can be used for pinning but must never be dereferenced. This is useful for interop with methods that do not accept null pointers for zero-sized buffers.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe ref T GetNonNullPinnableReference<T>(ReadOnlySpan<T> span) => ref (span.Length != 0) ? ref MemoryMarshal.GetReference(span) : ref Unsafe.AsRef<T>((void*)1);
}

internal sealed class UnixDomainSocketEndPoint : EndPoint
{
private const AddressFamily EndPointAddressFamily = AddressFamily.Unix;
Expand Down

0 comments on commit 24e3e8d

Please sign in to comment.