Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract partial class WaitHandle
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern int WaitOneCore(IntPtr waitHandle, int millisecondsTimeout, bool useTrivialWaits);

private static unsafe int WaitMultipleIgnoringSyncContextCore(Span<IntPtr> waitHandles, bool waitAll, int millisecondsTimeout)
private static unsafe int WaitMultipleIgnoringSyncContextCore(ReadOnlySpan<IntPtr> waitHandles, bool waitAll, int millisecondsTimeout)
{
fixed (IntPtr* pWaitHandles = &MemoryMarshal.GetReference(waitHandles))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static partial class Sys
// This is 'raw' sysctl call, only wrapped to allocate memory if needed
// caller always needs to free returned buffer using Marshal.FreeHGlobal()

internal static unsafe void Sysctl(Span<int> name, ref byte* value, ref int len)
internal static unsafe void Sysctl(ReadOnlySpan<int> name, ref byte* value, ref int len)
{
fixed (int* ptr = &MemoryMarshal.GetReference(name))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,9 @@ public unsafe struct @kinfo_proc
/// <param name="count">The number of kinfo_proc entries returned.</param>
public static unsafe kinfo_proc* GetProcInfo(int pid, bool threads, out int count)
{
Span<int> sysctlName = stackalloc int[4];

if (pid == 0)
{
// get all processes
sysctlName[3] = 0;
sysctlName[2] = KERN_PROC_PROC;
}
else
{
// get specific process, possibly with threads
sysctlName[3] = pid;
sysctlName[2] = KERN_PROC_PID | (threads ? KERN_PROC_INC_THREAD : 0);
}
sysctlName[1] = KERN_PROC;
sysctlName[0] = CTL_KERN;
ReadOnlySpan<int> sysctlName = pid == 0 ?
[CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0] : // get all processes
[CTL_KERN, KERN_PROC, KERN_PROC_PID | (threads ? KERN_PROC_INC_THREAD : 0), pid]; // get specific process, possibly with threads

byte* pBuffer = null;
int bytesLength = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static unsafe int[] ListAllPids()
/// <param name="pid">The PID of the process</param>
public static unsafe string GetProcPath(int pid)
{
Span<int> sysctlName = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid];
ReadOnlySpan<int> sysctlName = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid];
byte* pBuffer = null;
int bytesLength = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ internal static unsafe SafeCreateHandle CFArrayCreate(IntPtr[] values, UIntPtr n
/// </summary>
/// <param name="values">The values to put in the array</param>
/// <returns>Returns a valid SafeCreateHandle to a CFArray on success; otherwise, returns an invalid SafeCreateHandle</returns>
internal static unsafe SafeCreateHandle CFArrayCreate(Span<IntPtr> values)
internal static unsafe SafeCreateHandle CFArrayCreate(ReadOnlySpan<IntPtr> values)
{
fixed (IntPtr* pValues = &MemoryMarshal.GetReference(values))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private static partial int AppleCryptoNative_SslIsHostnameMatch(
[LibraryImport(Interop.Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_SslSetCertificateAuthorities")]
internal static partial int SslSetCertificateAuthorities(SafeSslHandle sslHandle, SafeCreateHandle certificateOrArray, int replaceExisting);

internal static unsafe void SslSetCertificateAuthorities(SafeSslHandle sslHandle, Span<IntPtr> certificates, bool replaceExisting)
internal static unsafe void SslSetCertificateAuthorities(SafeSslHandle sslHandle, ReadOnlySpan<IntPtr> certificates, bool replaceExisting)
{
using (SafeCreateHandle cfCertRefs = CoreFoundation.CFArrayCreate(certificates))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static unsafe int BCryptEncryptRsa(
BCryptEncryptFlags dwFlags)
{
// BCryptEncrypt does not accept null/0, only non-null/0.
Span<byte> notNull = stackalloc byte[1];
ReadOnlySpan<byte> notNull = stackalloc byte[1];
scoped ReadOnlySpan<byte> effectiveSource;

if (source.IsEmpty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ internal static unsafe partial uint GetAdaptersAddresses(
uint* outBufLen);

[LibraryImport(Interop.Libraries.IpHlpApi)]
internal static unsafe partial uint GetBestInterfaceEx(Span<byte> ipAddress, int* index);
internal static unsafe partial uint GetBestInterfaceEx(ReadOnlySpan<byte> ipAddress, int* index);

[LibraryImport(Interop.Libraries.IpHlpApi)]
internal static partial uint GetIfEntry2(ref MibIfRow2 pIfRow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
int length = FormatMessage(flags | FORMAT_MESSAGE_ALLOCATE_BUFFER, moduleHandle, unchecked((uint)errorCode), 0, &nativeMsgPtr, 0, IntPtr.Zero);
if (length > 0)
{
return GetAndTrimString(new Span<char>((char*)nativeMsgPtr, length));
return GetAndTrimString(new ReadOnlySpan<char>((char*)nativeMsgPtr, length));
}
}
finally
Expand All @@ -74,7 +74,7 @@ internal static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
return $"Unknown error (0x{errorCode:x})";
}

private static string GetAndTrimString(Span<char> buffer)
private static string GetAndTrimString(ReadOnlySpan<char> buffer)
{
int length = buffer.Length;
while (length > 0 && buffer[length - 1] <= 32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,22 @@ internal static unsafe byte[] DeriveKeyMaterialTls(
byte[] seed,
SecretAgreementFlags flags)
{
Span<NCryptBuffer> buffers = stackalloc NCryptBuffer[2];

fixed (byte* pLabel = label, pSeed = seed)
{
NCryptBuffer labelBuffer = default;
labelBuffer.cbBuffer = label.Length;
labelBuffer.BufferType = BufferType.KdfTlsLabel;
labelBuffer.pvBuffer = new IntPtr(pLabel);
buffers[0] = labelBuffer;

NCryptBuffer seedBuffer = default;
seedBuffer.cbBuffer = seed.Length;
seedBuffer.BufferType = BufferType.KdfTlsSeed;
seedBuffer.pvBuffer = new IntPtr(pSeed);
buffers[1] = seedBuffer;

return DeriveKeyMaterial(
secretAgreement,
BCryptNative.KeyDerivationFunction.Tls,
buffers,
[labelBuffer, seedBuffer],
flags);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal bool IsMarked(int bitPosition)
uint bitArrayIndex = (uint)bitPosition / IntSize;

// Workaround for https://github.com/dotnet/runtime/issues/72004
Span<int> span = _span;
ReadOnlySpan<int> span = _span;
return
bitArrayIndex < (uint)span.Length &&
(span[(int)bitArrayIndex] & (1 << ((int)((uint)bitPosition % IntSize)))) != 0;
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Net/Security/MD4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static void HH(ref uint a, uint b, uint c, uint d, uint x, byte s)
a = BitOperations.RotateLeft(a, s);
}

private static void Encode(Span<byte> output, Span<uint> input)
private static void Encode(Span<byte> output, ReadOnlySpan<uint> input)
{
for (int i = 0, j = 0; j < output.Length; i++, j += 4)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ internal static unsafe bool IsValidAddress(string? hostname)
{
// The address is parsed as IPv6 if and only if it contains a colon. This is valid because
// we don't support/parse a port specification at the end of an IPv4 address.
Span<ushort> numbers = stackalloc ushort[IPAddressParserStatics.IPv6AddressShorts];

fixed (char* ipStringPtr = &MemoryMarshal.GetReference(ipSpan))
{
return IPv6AddressHelper.IsValidStrict(ipStringPtr, 0, ref end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private static bool TryParseVersion(string attributeValue, ref Version? version)
return false;
}

Span<ushort> versionNumbers = stackalloc ushort[4] { ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue };
Span<ushort> versionNumbers = [ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue];
for (int i = 0; i < parts.Length; i++)
{
if (!ushort.TryParse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private static void Derive(
}
}

private static void AddPlusOne(Span<byte> into, Span<byte> addend)
private static void AddPlusOne(Span<byte> into, ReadOnlySpan<byte> addend)
{
Debug.Assert(into.Length == addend.Length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private long ReadNoValidation(long offset, Span<byte> buffer)
// Adjust count based on data length
int count = Math.Min(buffer.Length, (int)(Length - offset));

Span<byte> span = _rgbBuf!.AsSpan((int)offset, count);
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(_rgbBuf, (int)offset, count);
span.CopyTo(buffer);

return span.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ internal static string GetUntruncatedProcessName(ref Interop.procfs.ParsedStat s
// stat.comm contains a possibly truncated version of the process name.
// When the program is a native executable, the process name will be in argv[0].
// When the program is a script, argv[0] contains the interpreter, and argv[1] contains the script name.
Span<byte> argRemainder = buffer.Slice(0, bytesRead);
ReadOnlySpan<byte> argRemainder = buffer.Slice(0, bytesRead);
int argEnd = argRemainder.IndexOf((byte)'\0');
if (argEnd != -1)
{
Expand Down Expand Up @@ -336,7 +336,7 @@ internal static string GetUntruncatedProcessName(ref Interop.procfs.ParsedStat s
}
}

static string? GetUntruncatedNameFromArg(Span<byte> arg, string prefix)
static string? GetUntruncatedNameFromArg(ReadOnlySpan<byte> arg, string prefix)
{
// Strip directory names from arg.
int nameStart = arg.LastIndexOf((byte)'/') + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal sealed partial class TarHeader
return header;
}

private static TarHeader? TryReadAttributes(TarEntryFormat initialFormat, Span<byte> buffer)
private static TarHeader? TryReadAttributes(TarEntryFormat initialFormat, ReadOnlySpan<byte> buffer)
{
// Confirms if v7 or pax, or tentatively selects ustar
TarHeader? header = TryReadCommonAttributes(buffer, initialFormat);
Expand Down Expand Up @@ -357,12 +357,12 @@ private async Task ProcessDataBlockAsync(Stream archiveStream, bool copyData, Ca
// Attempts to read the fields shared by all formats and stores them in their expected data type.
// Throws if any data type conversion fails.
// Returns true on success, false if checksum is zero.
private static TarHeader? TryReadCommonAttributes(Span<byte> buffer, TarEntryFormat initialFormat)
private static TarHeader? TryReadCommonAttributes(ReadOnlySpan<byte> buffer, TarEntryFormat initialFormat)
{
// Start by collecting fields that need special checks that return early when data is wrong

// Empty checksum means this is an invalid (all blank) entry, finish early
Span<byte> spanChecksum = buffer.Slice(FieldLocations.Checksum, FieldLengths.Checksum);
ReadOnlySpan<byte> spanChecksum = buffer.Slice(FieldLocations.Checksum, FieldLengths.Checksum);
if (TarHelpers.IsAllNullBytes(spanChecksum))
{
return null;
Expand Down Expand Up @@ -424,9 +424,9 @@ TarEntryType.RenamedOrSymlinked or

// Reads fields only found in ustar format or above and converts them to their expected data type.
// Throws if any conversion fails.
private void ReadMagicAttribute(Span<byte> buffer)
private void ReadMagicAttribute(ReadOnlySpan<byte> buffer)
{
Span<byte> magic = buffer.Slice(FieldLocations.Magic, FieldLengths.Magic);
ReadOnlySpan<byte> magic = buffer.Slice(FieldLocations.Magic, FieldLengths.Magic);

// If at this point the magic value is all nulls, we definitely have a V7
if (TarHelpers.IsAllNullBytes(magic))
Expand Down Expand Up @@ -458,14 +458,14 @@ private void ReadMagicAttribute(Span<byte> buffer)

// Reads the version string and determines the format depending on its value.
// Throws if converting the bytes to string fails or if an unexpected version string is found.
private void ReadVersionAttribute(Span<byte> buffer)
private void ReadVersionAttribute(ReadOnlySpan<byte> buffer)
{
if (_format == TarEntryFormat.V7)
{
return;
}

Span<byte> version = buffer.Slice(FieldLocations.Version, FieldLengths.Version);
ReadOnlySpan<byte> version = buffer.Slice(FieldLocations.Version, FieldLengths.Version);
switch (_format)
{
case TarEntryFormat.Ustar or TarEntryFormat.Pax:
Expand Down Expand Up @@ -512,7 +512,7 @@ private void ReadVersionAttribute(Span<byte> buffer)

// Reads the attributes shared by the POSIX and GNU formats.
// Throws if converting the bytes to their expected data type fails.
private void ReadPosixAndGnuSharedAttributes(Span<byte> buffer)
private void ReadPosixAndGnuSharedAttributes(ReadOnlySpan<byte> buffer)
{
// Convert the byte arrays
_uName = TarHelpers.GetTrimmedUtf8String(buffer.Slice(FieldLocations.UName, FieldLengths.UName));
Expand All @@ -532,7 +532,7 @@ private void ReadPosixAndGnuSharedAttributes(Span<byte> buffer)

// Reads attributes specific to the GNU format.
// Throws if any conversion fails.
private void ReadGnuAttributes(Span<byte> buffer)
private void ReadGnuAttributes(ReadOnlySpan<byte> buffer)
{
// Convert byte arrays
long aTime = TarHelpers.ParseNumeric<long>(buffer.Slice(FieldLocations.ATime, FieldLengths.ATime));
Expand All @@ -546,7 +546,7 @@ private void ReadGnuAttributes(Span<byte> buffer)

// Reads the ustar prefix attribute.
// Throws if a conversion to an expected data type fails.
private void ReadUstarAttributes(Span<byte> buffer)
private void ReadUstarAttributes(ReadOnlySpan<byte> buffer)
{
_prefix = TarHelpers.GetTrimmedUtf8String(buffer.Slice(FieldLocations.Prefix, FieldLengths.Prefix));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal static int CalculatePadding(long size)
}

// Returns true if all the bytes in the specified array are nulls, false otherwise.
internal static bool IsAllNullBytes(Span<byte> buffer) =>
internal static bool IsAllNullBytes(ReadOnlySpan<byte> buffer) =>
!buffer.ContainsAnyExcept((byte)0);

// Converts the specified number of seconds that have passed since the Unix Epoch to a DateTimeOffset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ internal unsafe void Start(CancellationToken cancellationToken)
}

// Take the CFStringRef and put it into an array to pass to the EventStream
arrPaths = Interop.CoreFoundation.CFArrayCreate(new CFStringRef[1] { path.DangerousGetHandle() }, (UIntPtr)1);
arrPaths = Interop.CoreFoundation.CFArrayCreate([path.DangerousGetHandle()], (UIntPtr)1);
if (arrPaths.IsInvalid)
{
throw Interop.GetExceptionForIoErrno(Interop.Sys.GetLastErrorInfo(), _fullDirectory, true);
Expand Down Expand Up @@ -423,8 +423,8 @@ private static unsafe void FileSystemEventCallback(

private unsafe void ProcessEvents(int numEvents,
byte** eventPaths,
Span<FSEventStreamEventFlags> eventFlags,
Span<FSEventStreamEventId> eventIds,
ReadOnlySpan<FSEventStreamEventFlags> eventFlags,
ReadOnlySpan<FSEventStreamEventId> eventIds,
FileSystemWatcher watcher)
{
// Since renames come in pairs, when we reach the first we need to test for the next one if it is the case. If the next one belongs into the pair,
Expand Down Expand Up @@ -624,7 +624,7 @@ private bool CheckIfPathIsNested(ReadOnlySpan<char> eventPath)

private static int? FindRenameChangePairedChange(
int currentIndex,
Span<FSEventStreamEventFlags> flags, Span<FSEventStreamEventId> ids)
ReadOnlySpan<FSEventStreamEventFlags> flags, ReadOnlySpan<FSEventStreamEventId> ids)
{
// The rename event can be composed of two events. The first contains the original file name the second contains the new file name.
// Each of the events is delivered only when the corresponding folder is watched. It means both events are delivered when the rename/move
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@ private static unsafe void CreateAndStartRunLoop()
compAnyRegexString.DangerousGetHandle(),
entNetIpv6String.DangerousGetHandle()))
using (SafeCreateHandle patterns = Interop.CoreFoundation.CFArrayCreate(
new CFStringRef[2]
{
[
ipv4Pattern.DangerousGetHandle(),
ipv6Pattern.DangerousGetHandle()
}, (UIntPtr)2))
], (UIntPtr)2))
{
// Try to register our pattern strings with the dynamic store instance.
if (patterns.IsInvalid || !Interop.SystemConfiguration.SCDynamicStoreSetNotificationKeys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static ProtocolToken Renegotiate(
ref SafeDeleteSslContext? context,
SslAuthenticationOptions sslAuthenticationOptions)
{
return AcceptSecurityContext(ref credentialsHandle, ref context, Span<byte>.Empty, out _, sslAuthenticationOptions);
return AcceptSecurityContext(ref credentialsHandle, ref context, ReadOnlySpan<byte>.Empty, out _, sslAuthenticationOptions);
}

public static SafeFreeCredentials AcquireCredentialsHandle(SslAuthenticationOptions sslAuthenticationOptions, bool newCredentialsRequested)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ private void ReleaseSendBuffer()
}
}

private static int CombineMaskBytes(Span<byte> buffer, int maskOffset) =>
private static int CombineMaskBytes(ReadOnlySpan<byte> buffer, int maskOffset) =>
BitConverter.ToInt32(buffer.Slice(maskOffset));

/// <summary>Applies a mask to a portion of a byte array.</summary>
Expand Down Expand Up @@ -1544,7 +1544,7 @@ private void ThrowIfOperationInProgress(bool operationCompleted, [CallerMemberNa
// From https://github.com/aspnet/WebSockets/blob/aa63e27fce2e9202698053620679a9a1059b501e/src/Microsoft.AspNetCore.WebSockets.Protocol/Utilities.cs#L75
// Performs a stateful validation of UTF-8 bytes.
// It checks for valid formatting, overlong encodings, surrogates, and value ranges.
private static bool TryValidateUtf8(Span<byte> span, bool endOfMessage, Utf8MessageState state)
private static bool TryValidateUtf8(ReadOnlySpan<byte> span, bool endOfMessage, Utf8MessageState state)
{
for (int i = 0; i < span.Length;)
{
Expand Down
Loading