Skip to content

Commit 583d5e0

Browse files
steveharterjtschuster
authored andcommitted
Update size of LDAP BerVal.bv_len struct on unix (dotnet#107142)
1 parent 0954828 commit 583d5e0

File tree

6 files changed

+33
-33
lines changed

6 files changed

+33
-33
lines changed

src/libraries/Common/src/Interop/Interop.Ldap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@ internal struct LDAP_TIMEVAL
182182
[StructLayout(LayoutKind.Sequential)]
183183
internal sealed class BerVal
184184
{
185-
public int bv_len;
186-
public IntPtr bv_val = IntPtr.Zero;
185+
public CLong bv_len;
186+
public nint bv_val = nint.Zero;
187187

188188
#if NET
189189
[CustomMarshaller(typeof(BerVal), MarshalMode.ManagedToUnmanagedIn, typeof(PinningMarshaller))]
190190
internal static unsafe class PinningMarshaller
191191
{
192-
public static ref int GetPinnableReference(BerVal managed) => ref (managed is null ? ref Unsafe.NullRef<int>() : ref managed.bv_len);
192+
public static ref CLong GetPinnableReference(BerVal managed) => ref (managed is null ? ref Unsafe.NullRef<CLong>() : ref managed.bv_len);
193193

194194
// All usages in our currently supported scenarios will always go through GetPinnableReference
195195
public static int* ConvertToUnmanaged(BerVal _) => throw new UnreachableException();

src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/LdapPal.Linux.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ internal static unsafe int BindToDirectory(ConnectionHandle ld, string who, stri
128128
passwordPtr = LdapPal.StringToPtr(passwd);
129129
BerVal passwordBerval = new BerVal
130130
{
131-
bv_len = MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)passwordPtr).Length,
131+
bv_len = new CLong(MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)passwordPtr).Length),
132132
bv_val = passwordPtr,
133133
};
134134

src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/Interop/SafeHandles.Linux.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal SafeBerHandle(BerVal value) : base(true)
6565
// In Linux if bv_val is null ber_init will segFault instead of returning IntPtr.Zero.
6666
// In Linux if bv_len is 0 ber_init returns a valid pointer which will then fail when trying to use it,
6767
// so we fail early by throwing exception if this is the case.
68-
if (value.bv_val == IntPtr.Zero || value.bv_len == 0)
68+
if (value.bv_val == IntPtr.Zero || value.bv_len.Value == 0)
6969
{
7070
throw new BerConversionException();
7171
}

src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/BerConverter.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,15 @@ public static byte[] Encode(string format, params object[] value)
269269
Marshal.PtrToStructure(flattenptr, binaryValue);
270270
}
271271

272-
if (binaryValue == null || binaryValue.bv_len == 0)
272+
if (binaryValue == null || binaryValue.bv_len.Value == 0)
273273
{
274274
encodingResult = Array.Empty<byte>();
275275
}
276276
else
277277
{
278-
encodingResult = new byte[binaryValue.bv_len];
278+
encodingResult = new byte[binaryValue.bv_len.Value];
279279

280-
Marshal.Copy(binaryValue.bv_val, encodingResult, 0, binaryValue.bv_len);
280+
Marshal.Copy(binaryValue.bv_val, encodingResult, 0, (int)binaryValue.bv_len.Value);
281281
}
282282
}
283283
finally
@@ -315,12 +315,12 @@ internal static object[] TryDecode(string format, byte[] value, out bool decodeS
315315

316316
if (value == null)
317317
{
318-
berValue.bv_len = 0;
318+
berValue.bv_len = new CLong(0);
319319
berValue.bv_val = IntPtr.Zero;
320320
}
321321
else
322322
{
323-
berValue.bv_len = value.Length;
323+
berValue.bv_len = new CLong(value.Length);
324324
berValue.bv_val = Marshal.AllocHGlobal(value.Length);
325325
Marshal.Copy(value, 0, berValue.bv_val, value.Length);
326326
}
@@ -498,8 +498,8 @@ private static byte[] DecodingByteArrayHelper(SafeBerHandle berElement, char fmt
498498
{
499499
Marshal.PtrToStructure(result, binaryValue);
500500

501-
byteArray = new byte[binaryValue.bv_len];
502-
Marshal.Copy(binaryValue.bv_val, byteArray, 0, binaryValue.bv_len);
501+
byteArray = new byte[binaryValue.bv_len.Value];
502+
Marshal.Copy(binaryValue.bv_val, byteArray, 0, (int)binaryValue.bv_len.Value);
503503
}
504504
}
505505
else
@@ -539,7 +539,7 @@ private static unsafe int EncodingMultiByteArrayHelper(SafeBerHandle berElement,
539539

540540
if (byteArray != null)
541541
{
542-
managedBervalArray[i].bv_len = byteArray.Length;
542+
managedBervalArray[i].bv_len = new CLong(byteArray.Length);
543543
managedBervalArray[i].bv_val = Marshal.AllocHGlobal(byteArray.Length);
544544
Marshal.Copy(byteArray, 0, managedBervalArray[i].bv_val, byteArray.Length);
545545
}
@@ -606,8 +606,8 @@ private static byte[][] DecodingMultiByteArrayHelper(SafeBerHandle berElement, c
606606
BerVal ber = new BerVal();
607607
Marshal.PtrToStructure(tempPtr, ber);
608608

609-
byte[] berArray = new byte[ber.bv_len];
610-
Marshal.Copy(ber.bv_val, berArray, 0, ber.bv_len);
609+
byte[] berArray = new byte[ber.bv_len.Value];
610+
Marshal.Copy(ber.bv_val, berArray, 0, (int)ber.bv_len.Value);
611611

612612
binaryList.Add(berArray);
613613

src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/common/DirectoryControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,8 @@ public override unsafe byte[] GetValue()
756756
_directoryControlValue = null;
757757
if (value != null)
758758
{
759-
_directoryControlValue = new byte[value.bv_len];
760-
Marshal.Copy(value.bv_val, _directoryControlValue, 0, value.bv_len);
759+
_directoryControlValue = new byte[value.bv_len.Value];
760+
Marshal.Copy(value.bv_val, _directoryControlValue, 0, (int)value.bv_len.Value);
761761
}
762762
}
763763
finally

src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapConnection.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ private unsafe int SendRequestHelper(DirectoryRequest request, ref int messageID
629629

630630
berValuePtr = new BerVal
631631
{
632-
bv_len = byteArray.Length,
632+
bv_len = new CLong(byteArray.Length),
633633
bv_val = Marshal.AllocHGlobal(byteArray.Length)
634634
};
635635
Marshal.Copy(byteArray, 0, berValuePtr.bv_val, byteArray.Length);
@@ -695,7 +695,7 @@ private unsafe int SendRequestHelper(DirectoryRequest request, ref int messageID
695695
{
696696
berValuePtr = new BerVal()
697697
{
698-
bv_len = val.Length,
698+
bv_len = new CLong(val.Length),
699699
bv_val = Marshal.AllocHGlobal(val.Length)
700700
};
701701
Marshal.Copy(val, 0, berValuePtr.bv_val, val.Length);
@@ -1222,7 +1222,7 @@ internal static LdapControl[] BuildControlArray(DirectoryControlCollection contr
12221222
// Get the control type.
12231223
ldctl_oid = LdapPal.StringToPtr(((DirectoryControl)controlList[i]).Type),
12241224

1225-
// Get the control cricality.
1225+
// Get the control criticality.
12261226
ldctl_iscritical = ((DirectoryControl)controlList[i]).IsCritical
12271227
};
12281228

@@ -1234,18 +1234,18 @@ internal static LdapControl[] BuildControlArray(DirectoryControlCollection contr
12341234
// Treat the control value as null.
12351235
managedControls[i].ldctl_value = new BerVal
12361236
{
1237-
bv_len = 0,
1237+
bv_len = new CLong(0),
12381238
bv_val = IntPtr.Zero
12391239
};
12401240
}
12411241
else
12421242
{
12431243
managedControls[i].ldctl_value = new BerVal
12441244
{
1245-
bv_len = byteControlValue.Length,
1245+
bv_len = new CLong(byteControlValue.Length),
12461246
bv_val = Marshal.AllocHGlobal(sizeof(byte) * byteControlValue.Length)
12471247
};
1248-
Marshal.Copy(byteControlValue, 0, managedControls[i].ldctl_value.bv_val, managedControls[i].ldctl_value.bv_len);
1248+
Marshal.Copy(byteControlValue, 0, managedControls[i].ldctl_value.bv_val, (int)managedControls[i].ldctl_value.bv_len.Value);
12491249
}
12501250
}
12511251
}
@@ -1330,13 +1330,13 @@ internal static unsafe LdapMod[] BuildAttributes(CollectionBase directoryAttribu
13301330

13311331
berValues[j] = new BerVal()
13321332
{
1333-
bv_len = byteArray.Length,
1333+
bv_len = new CLong(byteArray.Length),
13341334
bv_val = Marshal.AllocHGlobal(byteArray.Length)
13351335
};
13361336

13371337
// need to free the memory allocated on the heap when we are done
13381338
ptrToFree.Add(berValues[j].bv_val);
1339-
Marshal.Copy(byteArray, 0, berValues[j].bv_val, berValues[j].bv_len);
1339+
Marshal.Copy(byteArray, 0, berValues[j].bv_val, (int)berValues[j].bv_len.Value);
13401340
}
13411341
}
13421342

@@ -1485,10 +1485,10 @@ internal async ValueTask<DirectoryResponse> ConstructResponseAsync(int messageId
14851485
{
14861486
val = new BerVal();
14871487
Marshal.PtrToStructure(requestValue, val);
1488-
if (val.bv_len != 0 && val.bv_val != IntPtr.Zero)
1488+
if (val.bv_len.Value != 0 && val.bv_val != IntPtr.Zero)
14891489
{
1490-
requestValueArray = new byte[val.bv_len];
1491-
Marshal.Copy(val.bv_val, requestValueArray, 0, val.bv_len);
1490+
requestValueArray = new byte[val.bv_len.Value];
1491+
Marshal.Copy(val.bv_val, requestValueArray, 0, (int)val.bv_len.Value);
14921492
}
14931493
}
14941494

@@ -1806,10 +1806,10 @@ internal DirectoryAttribute ConstructAttribute(IntPtr entryMessage, IntPtr attri
18061806
BerVal bervalue = new BerVal();
18071807
Marshal.PtrToStructure(tempPtr, bervalue);
18081808
byte[] byteArray;
1809-
if (bervalue.bv_len > 0 && bervalue.bv_val != IntPtr.Zero)
1809+
if (bervalue.bv_len.Value > 0 && bervalue.bv_val != IntPtr.Zero)
18101810
{
1811-
byteArray = new byte[bervalue.bv_len];
1812-
Marshal.Copy(bervalue.bv_val, byteArray, 0, bervalue.bv_len);
1811+
byteArray = new byte[bervalue.bv_len.Value];
1812+
Marshal.Copy(bervalue.bv_val, byteArray, 0, (int)bervalue.bv_len.Value);
18131813
attribute.Add(byteArray);
18141814
}
18151815

@@ -1944,8 +1944,8 @@ private static DirectoryControl ConstructControl(IntPtr controlPtr)
19441944
Debug.Assert(control.ldctl_oid != IntPtr.Zero);
19451945
string controlType = LdapPal.PtrToString(control.ldctl_oid);
19461946

1947-
byte[] bytes = new byte[control.ldctl_value.bv_len];
1948-
Marshal.Copy(control.ldctl_value.bv_val, bytes, 0, control.ldctl_value.bv_len);
1947+
byte[] bytes = new byte[control.ldctl_value.bv_len.Value];
1948+
Marshal.Copy(control.ldctl_value.bv_val, bytes, 0, (int)control.ldctl_value.bv_len.Value);
19491949

19501950
bool criticality = control.ldctl_iscritical;
19511951

0 commit comments

Comments
 (0)