diff --git a/src/libraries/Common/src/Interop/BSD/System.Native/Interop.TcpConnectionInfo.cs b/src/libraries/Common/src/Interop/BSD/System.Native/Interop.TcpConnectionInfo.cs index 58f6501a825c9e..946059dc870934 100644 --- a/src/libraries/Common/src/Interop/BSD/System.Native/Interop.TcpConnectionInfo.cs +++ b/src/libraries/Common/src/Interop/BSD/System.Native/Interop.TcpConnectionInfo.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Net.NetworkInformation; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -9,9 +10,9 @@ internal static partial class Interop internal static partial class Sys { [StructLayout(LayoutKind.Sequential)] - public unsafe struct IPEndPointInfo + public struct IPEndPointInfo { - public fixed byte AddressBytes[16]; + public InlineArray16 AddressBytes; public uint NumAddressBytes; public uint Port; private uint __padding; // For native struct-size padding. Does not contain useful data. diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.EnumerateInterfaceAddresses.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.EnumerateInterfaceAddresses.cs index cb237ff1f1b2cc..99bfb7cba730b5 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.EnumerateInterfaceAddresses.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.EnumerateInterfaceAddresses.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -8,38 +9,38 @@ internal static partial class Interop internal static partial class Sys { [StructLayout(LayoutKind.Sequential)] - public unsafe struct LinkLayerAddressInfo + public struct LinkLayerAddressInfo { public int InterfaceIndex; - public fixed byte AddressBytes[8]; + public InlineArray8 AddressBytes; public byte NumAddressBytes; private byte __padding; // For native struct-size padding. Does not contain useful data. public ushort HardwareType; } [StructLayout(LayoutKind.Sequential)] - public unsafe struct IpAddressInfo + public struct IpAddressInfo { public int InterfaceIndex; - public fixed byte AddressBytes[16]; + public InlineArray16 AddressBytes; public byte NumAddressBytes; public byte PrefixLength; - private fixed byte __padding[2]; + private InlineArray2 __padding; } [StructLayout(LayoutKind.Sequential)] - public unsafe struct NetworkInterfaceInfo + public struct NetworkInterfaceInfo { - public fixed byte Name[16]; + public InlineArray16 Name; public long Speed; public int InterfaceIndex; public int Mtu; public ushort HardwareType; public byte OperationalState; public byte NumAddressBytes; - public fixed byte AddressBytes[8]; + public InlineArray8 AddressBytes; public byte SupportsMulticast; - private fixed byte __padding[3]; + private InlineArray3 __padding; } [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_EnumerateInterfaceAddresses")] diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.TimeZone.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.TimeZone.cs index 705d97ac3f48d2..f9cd8549db16c1 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.TimeZone.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.TimeZone.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -30,37 +32,56 @@ internal bool Equals(in SYSTEMTIME other) => } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - internal unsafe struct TIME_DYNAMIC_ZONE_INFORMATION + internal struct TIME_DYNAMIC_ZONE_INFORMATION { + [InlineArray(32)] + internal struct NameBuffer + { + private char _element0; + } + + [InlineArray(128)] + internal struct TimeZoneKeyNameBuffer + { + private char _element0; + } + internal int Bias; - internal fixed char StandardName[32]; + internal NameBuffer StandardName; internal SYSTEMTIME StandardDate; internal int StandardBias; - internal fixed char DaylightName[32]; + internal NameBuffer DaylightName; internal SYSTEMTIME DaylightDate; internal int DaylightBias; - internal fixed char TimeZoneKeyName[128]; + internal TimeZoneKeyNameBuffer TimeZoneKeyName; internal byte DynamicDaylightTimeDisabled; internal string GetTimeZoneKeyName() { - fixed (char* p = TimeZoneKeyName) - return new string(p); + ReadOnlySpan span = TimeZoneKeyName; + int idx = span.IndexOf('\0'); + return new string(idx >= 0 ? span[..idx] : span); } } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - internal unsafe struct TIME_ZONE_INFORMATION + internal struct TIME_ZONE_INFORMATION { + [InlineArray(32)] + internal struct NameBuffer + { + private char _element0; + } + internal int Bias; - internal fixed char StandardName[32]; + internal NameBuffer StandardName; internal SYSTEMTIME StandardDate; internal int StandardBias; - internal fixed char DaylightName[32]; + internal NameBuffer DaylightName; internal SYSTEMTIME DaylightDate; internal int DaylightBias; - internal TIME_ZONE_INFORMATION(in TIME_DYNAMIC_ZONE_INFORMATION dtzi) + internal unsafe TIME_ZONE_INFORMATION(in TIME_DYNAMIC_ZONE_INFORMATION dtzi) { // The start of TIME_DYNAMIC_ZONE_INFORMATION has identical layout as TIME_ZONE_INFORMATION fixed (TIME_ZONE_INFORMATION* pTo = &this) @@ -70,14 +91,16 @@ internal TIME_ZONE_INFORMATION(in TIME_DYNAMIC_ZONE_INFORMATION dtzi) internal string GetStandardName() { - fixed (char* p = StandardName) - return new string(p); + ReadOnlySpan span = StandardName; + int idx = span.IndexOf('\0'); + return new string(idx >= 0 ? span[..idx] : span); } internal string GetDaylightName() { - fixed (char* p = DaylightName) - return new string(p); + ReadOnlySpan span = DaylightName; + int idx = span.IndexOf('\0'); + return new string(idx >= 0 ? span[..idx] : span); } } diff --git a/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RtlGetVersion.cs b/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RtlGetVersion.cs index 3fb9740a7f2490..7e9a8f5cb7a679 100644 --- a/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RtlGetVersion.cs +++ b/src/libraries/Common/src/Interop/Windows/NtDll/Interop.RtlGetVersion.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -8,24 +9,35 @@ internal static partial class Interop internal static partial class NtDll { [LibraryImport(Libraries.NtDll)] - private static partial int RtlGetVersion(ref RTL_OSVERSIONINFOEX lpVersionInformation); + private static unsafe partial int RtlGetVersion(RTL_OSVERSIONINFOEX* lpVersionInformation); internal static unsafe int RtlGetVersionEx(out RTL_OSVERSIONINFOEX osvi) { osvi = default; osvi.dwOSVersionInfoSize = (uint)sizeof(RTL_OSVERSIONINFOEX); - return RtlGetVersion(ref osvi); + fixed (RTL_OSVERSIONINFOEX* p = &osvi) + return RtlGetVersion(p); } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - internal unsafe struct RTL_OSVERSIONINFOEX + internal struct RTL_OSVERSIONINFOEX { internal uint dwOSVersionInfoSize; internal uint dwMajorVersion; internal uint dwMinorVersion; internal uint dwBuildNumber; internal uint dwPlatformId; - internal fixed char szCSDVersion[128]; +#if NET + internal CSDVersionBuffer szCSDVersion; + + [InlineArray(128)] + internal struct CSDVersionBuffer + { + private char _element0; + } +#else + internal unsafe fixed char szCSDVersion[128]; +#endif } } } diff --git a/src/libraries/Common/src/Interop/Windows/WinHttp/Interop.winhttp_types.cs b/src/libraries/Common/src/Interop/Windows/WinHttp/Interop.winhttp_types.cs index 42b58b7df198d7..cc8fbfeb926d9d 100644 --- a/src/libraries/Common/src/Interop/Windows/WinHttp/Interop.winhttp_types.cs +++ b/src/libraries/Common/src/Interop/Windows/WinHttp/Interop.winhttp_types.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; #if NET using System.Runtime.InteropServices.Marshalling; @@ -337,14 +338,25 @@ public struct WINHTTP_ASYNC_RESULT } [StructLayout(LayoutKind.Sequential)] - public unsafe struct WINHTTP_CONNECTION_INFO + public struct WINHTTP_CONNECTION_INFO { // This field is actually 4 bytes, but we use nuint to avoid alignment issues for x64. // If we want to read this field in the future, we need to change type and make sure // alignment is correct for necessary archs. public nuint cbSize; - public fixed byte LocalAddress[128]; - public fixed byte RemoteAddress[128]; +#if NET + public AddressBuffer LocalAddress; + public AddressBuffer RemoteAddress; + + [InlineArray(128)] + public struct AddressBuffer + { + private byte _element0; + } +#else + public unsafe fixed byte LocalAddress[128]; + public unsafe fixed byte RemoteAddress[128]; +#endif } [StructLayout(LayoutKind.Sequential)] diff --git a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestCallback.cs b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestCallback.cs index 4ce5a952b45253..f90327e07cd994 100644 --- a/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestCallback.cs +++ b/src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestCallback.cs @@ -330,7 +330,11 @@ private static void OnRequestSendingRequest(WinHttpRequestState state) // See: https://learn.microsoft.com/en-us/windows/win32/api/winhttp/ns-winhttp-winhttp_connection_info // SOCKADDR_STORAGE can hold either IPv4 or IPv6 address. // For offset numbers: https://learn.microsoft.com/en-us/windows/win32/winsock/sockaddr-2 +#if NET + ReadOnlySpan remoteAddressSpan = connectionInfo.RemoteAddress; +#else ReadOnlySpan remoteAddressSpan = new ReadOnlySpan(connectionInfo.RemoteAddress, 128); +#endif AddressFamily addressFamily = (AddressFamily)(remoteAddressSpan[0] + (remoteAddressSpan[1] << 8)); ipAddress = addressFamily switch { diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/AndroidNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/AndroidNetworkInterface.cs index dcf66895aea7b1..7a331560c69932 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/AndroidNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/AndroidNetworkInterface.cs @@ -19,7 +19,7 @@ internal unsafe AndroidNetworkInterface(string name, Interop.Sys.NetworkInterfac _index = networkInterfaceInfo->InterfaceIndex; if (networkInterfaceInfo->NumAddressBytes > 0) { - _physicalAddress = new PhysicalAddress(new ReadOnlySpan(networkInterfaceInfo->AddressBytes, networkInterfaceInfo->NumAddressBytes).ToArray()); + _physicalAddress = new PhysicalAddress(((ReadOnlySpan)networkInterfaceInfo->AddressBytes)[..networkInterfaceInfo->NumAddressBytes].ToArray()); } _mtu = networkInterfaceInfo->Mtu; @@ -33,7 +33,7 @@ internal unsafe AndroidNetworkInterface(string name, Interop.Sys.NetworkInterfac internal unsafe void AddAddress(Interop.Sys.IpAddressInfo *addressInfo) { - var address = new IPAddress(new ReadOnlySpan(addressInfo->AddressBytes, addressInfo->NumAddressBytes)); + var address = new IPAddress(((ReadOnlySpan)addressInfo->AddressBytes)[..addressInfo->NumAddressBytes]); if (address.IsIPv6LinkLocal) { address.ScopeId = addressInfo->InterfaceIndex; diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs index 448d2181e9b074..c01b320866b282 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPGlobalProperties.cs @@ -32,12 +32,12 @@ private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners) continue; } - IPAddress localIPAddress = new IPAddress(new ReadOnlySpan(nativeInfo.LocalEndPoint.AddressBytes, checked((int)nativeInfo.LocalEndPoint.NumAddressBytes))); + IPAddress localIPAddress = new IPAddress(((ReadOnlySpan)nativeInfo.LocalEndPoint.AddressBytes)[..checked((int)nativeInfo.LocalEndPoint.NumAddressBytes)]); IPEndPoint local = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port); IPAddress remoteIPAddress = nativeInfo.RemoteEndPoint.NumAddressBytes == 0 ? IPAddress.Any : - new IPAddress(new ReadOnlySpan(nativeInfo.RemoteEndPoint.AddressBytes, checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes))); + new IPAddress(((ReadOnlySpan)nativeInfo.RemoteEndPoint.AddressBytes)[..checked((int)nativeInfo.RemoteEndPoint.NumAddressBytes)]); IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port); connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state); @@ -87,7 +87,7 @@ public override unsafe IPEndPoint[] GetActiveUdpListeners() int port = (int)endPointInfo.Port; IPAddress ipAddress = endPointInfo.NumAddressBytes == 0 ? IPAddress.Any : - new IPAddress(new ReadOnlySpan(endPointInfo.AddressBytes, checked((int)endPointInfo.NumAddressBytes))); + new IPAddress(((ReadOnlySpan)endPointInfo.AddressBytes)[..checked((int)endPointInfo.NumAddressBytes)]); endPoints[i] = new IPEndPoint(ipAddress, port); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs index 8ce6f541bf0de9..0b25c83c83465e 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs @@ -93,7 +93,7 @@ private static unsafe void OnGatewayFound(void* pContext, Interop.Sys.IpAddressI { Context* context = (Context*)pContext; - IPAddress ipAddress = new IPAddress(new ReadOnlySpan(gatewayAddressInfo->AddressBytes, gatewayAddressInfo->NumAddressBytes)); + IPAddress ipAddress = new IPAddress(((ReadOnlySpan)gatewayAddressInfo->AddressBytes)[..gatewayAddressInfo->NumAddressBytes]); if (ipAddress.IsIPv6LinkLocal) { // For Link-Local addresses add ScopeId as that is not part of the route entry. diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/IPAddressUtil.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/IPAddressUtil.cs index c2e776a07bee0c..9a2ef06a50d40e 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/IPAddressUtil.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/IPAddressUtil.cs @@ -34,7 +34,7 @@ public static bool IsMulticast(IPAddress address) /// A new IPAddress created with the information in the native structure. public static unsafe IPAddress GetIPAddressFromNativeInfo(Interop.Sys.IpAddressInfo* addressInfo) { - IPAddress ipAddress = new IPAddress(new ReadOnlySpan(addressInfo->AddressBytes, addressInfo->NumAddressBytes)); + IPAddress ipAddress = new IPAddress(((ReadOnlySpan)addressInfo->AddressBytes)[..addressInfo->NumAddressBytes]); return ipAddress; } } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs index 71330d0eb9ddc7..0310e630b7cda9 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/LinuxNetworkInterface.cs @@ -95,7 +95,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces() for (int i = 0; i < interfaceCount; i++) { - var lni = new LinuxNetworkInterface(Utf8StringMarshaller.ConvertToManaged(nii->Name)!, nii->InterfaceIndex, systemProperties); + var lni = new LinuxNetworkInterface(Utf8StringMarshaller.ConvertToManaged((byte*)&nii->Name)!, nii->InterfaceIndex, systemProperties); lni._interfaceType = (NetworkInterfaceType)nii->HardwareType; lni._speed = nii->Speed; lni._operationalStatus = (OperationalStatus)nii->OperationalState; @@ -104,7 +104,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces() if (nii->NumAddressBytes > 0) { - lni._physicalAddress = new PhysicalAddress(new ReadOnlySpan(nii->AddressBytes, nii->NumAddressBytes).ToArray()); + lni._physicalAddress = new PhysicalAddress(((ReadOnlySpan)nii->AddressBytes)[..nii->NumAddressBytes].ToArray()); } interfaces[i] = lni; @@ -114,7 +114,7 @@ public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces() while (addressCount != 0) { - var address = new IPAddress(new ReadOnlySpan(ai->AddressBytes, ai->NumAddressBytes)); + var address = new IPAddress(((ReadOnlySpan)ai->AddressBytes)[..ai->NumAddressBytes]); if (address.IsIPv6LinkLocal) { address.ScopeId = ai->InterfaceIndex; diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkInterfacePal.Android.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkInterfacePal.Android.cs index 565d8df9ab4b6e..8bc48a35dd3318 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkInterfacePal.Android.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkInterfacePal.Android.cs @@ -52,7 +52,7 @@ private static unsafe AndroidNetworkInterface[] ToAndroidNetworkInterfaceArray(i var networkInterfaceInfo = (Interop.Sys.NetworkInterfaceInfo*)networkInterfacesPtr; for (int i = 0; i < interfaceCount; i++, networkInterfaceInfo++) { - var name = Utf8StringMarshaller.ConvertToManaged(networkInterfaceInfo->Name); + var name = Utf8StringMarshaller.ConvertToManaged((byte*)&networkInterfaceInfo->Name); networkInterfaces[i] = new AndroidNetworkInterface(name!, networkInterfaceInfo); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs index 444d19bf01a42b..78b655131860f7 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixNetworkInterface.cs @@ -98,11 +98,7 @@ protected unsafe void ProcessIpv6Address(Interop.Sys.IpAddressInfo* addressInfo, protected unsafe void ProcessLinkLayerAddress(Interop.Sys.LinkLayerAddressInfo* llAddr) { - byte[] macAddress = new byte[llAddr->NumAddressBytes]; - fixed (byte* macAddressPtr = macAddress) - { - Buffer.MemoryCopy(llAddr->AddressBytes, macAddressPtr, llAddr->NumAddressBytes, llAddr->NumAddressBytes); - } + byte[] macAddress = ((ReadOnlySpan)llAddr->AddressBytes)[..llAddr->NumAddressBytes].ToArray(); PhysicalAddress physicalAddress = new PhysicalAddress(macAddress); _index = llAddr->InterfaceIndex; diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs index 80624afcb23a44..83ab7e5b8ee0da 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs @@ -141,7 +141,7 @@ private static unsafe bool IsPrivilegedProcessCore() return builder.ToString(); } - private static unsafe OperatingSystem GetOSVersion() + private static OperatingSystem GetOSVersion() { if (Interop.NtDll.RtlGetVersionEx(out Interop.NtDll.RTL_OSVERSIONINFOEX osvi) != 0) { @@ -150,9 +150,14 @@ private static unsafe OperatingSystem GetOSVersion() var version = new Version((int)osvi.dwMajorVersion, (int)osvi.dwMinorVersion, (int)osvi.dwBuildNumber, 0); - return osvi.szCSDVersion[0] != '\0' ? - new OperatingSystem(PlatformID.Win32NT, version, new string(&osvi.szCSDVersion[0])) : - new OperatingSystem(PlatformID.Win32NT, version); + if (osvi.szCSDVersion[0] != '\0') + { + ReadOnlySpan csd = osvi.szCSDVersion; + int idx = csd.IndexOf('\0'); + return new OperatingSystem(PlatformID.Win32NT, version, new string(idx >= 0 ? csd[..idx] : csd)); + } + + return new OperatingSystem(PlatformID.Win32NT, version); } private static string? s_systemDirectory; diff --git a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs index d66560a5286dda..d11ad896b86966 100644 --- a/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs +++ b/src/libraries/System.Text.Encodings.Web/src/System/Text/Encodings/Web/OptimizedInboxTextEncoder.Ascii.cs @@ -21,7 +21,17 @@ internal sealed partial class OptimizedInboxTextEncoder /// private unsafe struct AsciiPreescapedData { - private fixed ulong Data[128]; +#if NET + [InlineArray(128)] + private struct DataBuffer + { + private ulong _element0; + } + + private DataBuffer Data; +#else + private unsafe fixed ulong Data[128]; +#endif internal void PopulatePreescapedData(in AllowedBmpCodePointsBitmap allowedCodePointsBmp, ScalarEscaperBase innerEncoder) { @@ -63,7 +73,7 @@ internal readonly bool TryGetPreescapedData(uint codePoint, out ulong preescaped { if (codePoint <= 0x7F) { - preescapedData = Data[codePoint]; + preescapedData = Data[(int)codePoint]; return true; } else diff --git a/src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilConverter.cs b/src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilConverter.cs index 820d38631706e0..8afebfe53b7952 100644 --- a/src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilConverter.cs +++ b/src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilConverter.cs @@ -115,7 +115,7 @@ public unsafe void GatherInfo(PEReader peReader, out WCFileInfo wcInfo, out PEFi var peHeader = headers.PEHeader!; var coffHeader = headers.CoffHeader!; var sections = headers.SectionHeaders; - WebcilHeader header; + WebcilHeader header = default; header.Id[0] = (byte)'W'; header.Id[1] = (byte)'b'; header.Id[2] = (byte)'I'; diff --git a/src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilHeader.cs b/src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilHeader.cs index 419e058bd180f2..9705e584951bdc 100644 --- a/src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilHeader.cs +++ b/src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilHeader.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Microsoft.NET.WebAssembly.Webcil; @@ -13,9 +14,13 @@ namespace Microsoft.NET.WebAssembly.Webcil; /// The header is a subset of the PE, COFF and CLI headers that are needed by the mono runtime to load managed assemblies. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] -public unsafe struct WebcilHeader +public struct WebcilHeader { - public fixed byte Id[4]; +#if NET + public InlineArray4 Id; +#else + public unsafe fixed byte Id[4]; +#endif // 4 bytes public ushort VersionMajor; public ushort VersionMinor;