Skip to content

Commit

Permalink
Update size of LDAP BerVal.bv_len struct on unix (dotnet#107142)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveharter authored Sep 3, 2024
1 parent 34d2f17 commit 0b96740
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/libraries/Common/src/Interop/Interop.Ldap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>() : ref managed.bv_len);
public static ref CLong GetPinnableReference(BerVal managed) => ref (managed is null ? ref Unsafe.NullRef<CLong>() : ref managed.bv_len);

// All usages in our currently supported scenarios will always go through GetPinnableReference
public static int* ConvertToUnmanaged(BerVal _) => throw new UnreachableException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>();
}
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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
};

Expand All @@ -1234,18 +1234,18 @@ 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
};
}
else
{
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);
}
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -1485,10 +1485,10 @@ internal async ValueTask<DirectoryResponse> 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);
}
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 0b96740

Please sign in to comment.