diff --git a/src/libraries/Common/src/Interop/Interop.Ldap.cs b/src/libraries/Common/src/Interop/Interop.Ldap.cs index 4020dd0fe89bb..90c0ba997cd96 100644 --- a/src/libraries/Common/src/Interop/Interop.Ldap.cs +++ b/src/libraries/Common/src/Interop/Interop.Ldap.cs @@ -182,14 +182,14 @@ internal struct LDAP_TIMEVAL [StructLayout(LayoutKind.Sequential)] internal sealed class BerVal { - public int bv_len; - public IntPtr bv_val = IntPtr.Zero; + public CLong bv_len; + public nint bv_val = nint.Zero; #if NET [CustomMarshaller(typeof(BerVal), MarshalMode.ManagedToUnmanagedIn, typeof(PinningMarshaller))] internal static unsafe class PinningMarshaller { - public static ref int GetPinnableReference(BerVal managed) => ref (managed is null ? ref Unsafe.NullRef() : ref managed.bv_len); + public static ref CLong GetPinnableReference(BerVal managed) => ref (managed is null ? ref Unsafe.NullRef() : ref managed.bv_len); // All usages in our currently supported scenarios will always go through GetPinnableReference public static int* ConvertToUnmanaged(BerVal _) => throw new UnreachableException(); diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs index 15b3cd86effaf..f364b8e44e19e 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs @@ -128,7 +128,7 @@ internal static unsafe int BindToDirectory(ConnectionHandle ld, string who, stri passwordPtr = LdapPal.StringToPtr(passwd); BerVal passwordBerval = new BerVal { - bv_len = MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)passwordPtr).Length, + bv_len = new CLong(MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)passwordPtr).Length), bv_val = passwordPtr, }; diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/SafeHandles.Linux.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/SafeHandles.Linux.cs index 24d660de645fd..70d25cb99f234 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/SafeHandles.Linux.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/SafeHandles.Linux.cs @@ -65,7 +65,7 @@ internal SafeBerHandle(BerVal value) : base(true) // In Linux if bv_val is null ber_init will segFault instead of returning IntPtr.Zero. // In Linux if bv_len is 0 ber_init returns a valid pointer which will then fail when trying to use it, // so we fail early by throwing exception if this is the case. - if (value.bv_val == IntPtr.Zero || value.bv_len == 0) + if (value.bv_val == IntPtr.Zero || value.bv_len.Value == 0) { throw new BerConversionException(); } diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/BerConverter.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/BerConverter.cs index 735018bc8bb09..a0d09e2712921 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/BerConverter.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/BerConverter.cs @@ -269,15 +269,15 @@ public static byte[] Encode(string format, params object[] value) Marshal.PtrToStructure(flattenptr, binaryValue); } - if (binaryValue == null || binaryValue.bv_len == 0) + if (binaryValue == null || binaryValue.bv_len.Value == 0) { encodingResult = Array.Empty(); } else { - encodingResult = new byte[binaryValue.bv_len]; + encodingResult = new byte[binaryValue.bv_len.Value]; - Marshal.Copy(binaryValue.bv_val, encodingResult, 0, binaryValue.bv_len); + Marshal.Copy(binaryValue.bv_val, encodingResult, 0, (int)binaryValue.bv_len.Value); } } finally @@ -315,12 +315,12 @@ internal static object[] TryDecode(string format, byte[] value, out bool decodeS if (value == null) { - berValue.bv_len = 0; + berValue.bv_len = new CLong(0); berValue.bv_val = IntPtr.Zero; } else { - berValue.bv_len = value.Length; + berValue.bv_len = new CLong(value.Length); berValue.bv_val = Marshal.AllocHGlobal(value.Length); Marshal.Copy(value, 0, berValue.bv_val, value.Length); } @@ -498,8 +498,8 @@ private static byte[] DecodingByteArrayHelper(SafeBerHandle berElement, char fmt { Marshal.PtrToStructure(result, binaryValue); - byteArray = new byte[binaryValue.bv_len]; - Marshal.Copy(binaryValue.bv_val, byteArray, 0, binaryValue.bv_len); + byteArray = new byte[binaryValue.bv_len.Value]; + Marshal.Copy(binaryValue.bv_val, byteArray, 0, (int)binaryValue.bv_len.Value); } } else @@ -539,7 +539,7 @@ private static unsafe int EncodingMultiByteArrayHelper(SafeBerHandle berElement, if (byteArray != null) { - managedBervalArray[i].bv_len = byteArray.Length; + managedBervalArray[i].bv_len = new CLong(byteArray.Length); managedBervalArray[i].bv_val = Marshal.AllocHGlobal(byteArray.Length); Marshal.Copy(byteArray, 0, managedBervalArray[i].bv_val, byteArray.Length); } @@ -606,8 +606,8 @@ private static byte[][] DecodingMultiByteArrayHelper(SafeBerHandle berElement, c BerVal ber = new BerVal(); Marshal.PtrToStructure(tempPtr, ber); - byte[] berArray = new byte[ber.bv_len]; - Marshal.Copy(ber.bv_val, berArray, 0, ber.bv_len); + byte[] berArray = new byte[ber.bv_len.Value]; + Marshal.Copy(ber.bv_val, berArray, 0, (int)ber.bv_len.Value); binaryList.Add(berArray); diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/DirectoryControl.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/DirectoryControl.cs index f47bc50318b94..ba0f714b2b02f 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/DirectoryControl.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/DirectoryControl.cs @@ -756,8 +756,8 @@ public override unsafe byte[] GetValue() _directoryControlValue = null; if (value != null) { - _directoryControlValue = new byte[value.bv_len]; - Marshal.Copy(value.bv_val, _directoryControlValue, 0, value.bv_len); + _directoryControlValue = new byte[value.bv_len.Value]; + Marshal.Copy(value.bv_val, _directoryControlValue, 0, (int)value.bv_len.Value); } } finally diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs index 4be0407a9eea9..1125bfd568d38 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs @@ -629,7 +629,7 @@ private unsafe int SendRequestHelper(DirectoryRequest request, ref int messageID berValuePtr = new BerVal { - bv_len = byteArray.Length, + bv_len = new CLong(byteArray.Length), bv_val = Marshal.AllocHGlobal(byteArray.Length) }; Marshal.Copy(byteArray, 0, berValuePtr.bv_val, byteArray.Length); @@ -695,7 +695,7 @@ private unsafe int SendRequestHelper(DirectoryRequest request, ref int messageID { berValuePtr = new BerVal() { - bv_len = val.Length, + bv_len = new CLong(val.Length), bv_val = Marshal.AllocHGlobal(val.Length) }; Marshal.Copy(val, 0, berValuePtr.bv_val, val.Length); @@ -1222,7 +1222,7 @@ internal static LdapControl[] BuildControlArray(DirectoryControlCollection contr // Get the control type. ldctl_oid = LdapPal.StringToPtr(((DirectoryControl)controlList[i]).Type), - // Get the control cricality. + // Get the control criticality. ldctl_iscritical = ((DirectoryControl)controlList[i]).IsCritical }; @@ -1234,7 +1234,7 @@ internal static LdapControl[] BuildControlArray(DirectoryControlCollection contr // Treat the control value as null. managedControls[i].ldctl_value = new BerVal { - bv_len = 0, + bv_len = new CLong(0), bv_val = IntPtr.Zero }; } @@ -1242,10 +1242,10 @@ internal static LdapControl[] BuildControlArray(DirectoryControlCollection contr { managedControls[i].ldctl_value = new BerVal { - bv_len = byteControlValue.Length, + bv_len = new CLong(byteControlValue.Length), bv_val = Marshal.AllocHGlobal(sizeof(byte) * byteControlValue.Length) }; - Marshal.Copy(byteControlValue, 0, managedControls[i].ldctl_value.bv_val, managedControls[i].ldctl_value.bv_len); + Marshal.Copy(byteControlValue, 0, managedControls[i].ldctl_value.bv_val, (int)managedControls[i].ldctl_value.bv_len.Value); } } } @@ -1330,13 +1330,13 @@ internal static unsafe LdapMod[] BuildAttributes(CollectionBase directoryAttribu berValues[j] = new BerVal() { - bv_len = byteArray.Length, + bv_len = new CLong(byteArray.Length), bv_val = Marshal.AllocHGlobal(byteArray.Length) }; // need to free the memory allocated on the heap when we are done ptrToFree.Add(berValues[j].bv_val); - Marshal.Copy(byteArray, 0, berValues[j].bv_val, berValues[j].bv_len); + Marshal.Copy(byteArray, 0, berValues[j].bv_val, (int)berValues[j].bv_len.Value); } } @@ -1485,10 +1485,10 @@ internal async ValueTask ConstructResponseAsync(int messageId { val = new BerVal(); Marshal.PtrToStructure(requestValue, val); - if (val.bv_len != 0 && val.bv_val != IntPtr.Zero) + if (val.bv_len.Value != 0 && val.bv_val != IntPtr.Zero) { - requestValueArray = new byte[val.bv_len]; - Marshal.Copy(val.bv_val, requestValueArray, 0, val.bv_len); + requestValueArray = new byte[val.bv_len.Value]; + Marshal.Copy(val.bv_val, requestValueArray, 0, (int)val.bv_len.Value); } } @@ -1806,10 +1806,10 @@ internal DirectoryAttribute ConstructAttribute(IntPtr entryMessage, IntPtr attri BerVal bervalue = new BerVal(); Marshal.PtrToStructure(tempPtr, bervalue); byte[] byteArray; - if (bervalue.bv_len > 0 && bervalue.bv_val != IntPtr.Zero) + if (bervalue.bv_len.Value > 0 && bervalue.bv_val != IntPtr.Zero) { - byteArray = new byte[bervalue.bv_len]; - Marshal.Copy(bervalue.bv_val, byteArray, 0, bervalue.bv_len); + byteArray = new byte[bervalue.bv_len.Value]; + Marshal.Copy(bervalue.bv_val, byteArray, 0, (int)bervalue.bv_len.Value); attribute.Add(byteArray); } @@ -1944,8 +1944,8 @@ private static DirectoryControl ConstructControl(IntPtr controlPtr) Debug.Assert(control.ldctl_oid != IntPtr.Zero); string controlType = LdapPal.PtrToString(control.ldctl_oid); - byte[] bytes = new byte[control.ldctl_value.bv_len]; - Marshal.Copy(control.ldctl_value.bv_val, bytes, 0, control.ldctl_value.bv_len); + byte[] bytes = new byte[control.ldctl_value.bv_len.Value]; + Marshal.Copy(control.ldctl_value.bv_val, bytes, 0, (int)control.ldctl_value.bv_len.Value); bool criticality = control.ldctl_iscritical;