Skip to content

Commit 0f98974

Browse files
authored
Fix incorrect definition of LSA_FOREST_TRUST_RECORD in System.DirectoryServices (#68274)
1 parent 215b39a commit 0f98974

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ public void Save()
187187
{
188188
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
189189
}
190-
record.DomainInfo = new LSA_FOREST_TRUST_DOMAIN_INFO();
191190
record.DomainInfo.sid = pSid;
192191
sidList.Add(pSid);
193192
record.DomainInfo.DNSNameBuffer = Marshal.StringToHGlobalUni(tmp.DnsName);
@@ -214,7 +213,6 @@ public void Save()
214213
record.Time = (LARGE_INTEGER)_binaryDataTime[i]!;
215214
record.Data.Length = ((byte[])_binaryData[i]!).Length;
216215
record.ForestTrustType = (LSA_FOREST_TRUST_RECORD_TYPE)_binaryRecordType[i]!;
217-
record.Data = new LSA_FOREST_TRUST_BINARY_DATA();
218216
if (record.Data.Length == 0)
219217
{
220218
record.Data.Buffer = (IntPtr)0;
@@ -317,7 +315,7 @@ public void Save()
317315
catch { throw; }
318316
}
319317

320-
private void GetForestTrustInfoHelper()
318+
private unsafe void GetForestTrustInfoHelper()
321319
{
322320
IntPtr forestTrustInfo = (IntPtr)0;
323321
SafeLsaPolicyHandle? handle = null;
@@ -381,30 +379,30 @@ private void GetForestTrustInfoHelper()
381379
if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelName)
382380
{
383381
IntPtr myPtr = IntPtr.Add(addr, 16);
384-
Marshal.PtrToStructure(myPtr, record.TopLevelName);
382+
record.TopLevelName = *(global::Interop.UNICODE_STRING*)myPtr;
385383
TopLevelName TLN = new TopLevelName(record.Flags, record.TopLevelName, record.Time);
386384
tmpTLNs.Add(TLN);
387385
}
388386
else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelNameEx)
389387
{
390388
// get the excluded TLN and put it in our collection
391389
IntPtr myPtr = IntPtr.Add(addr, 16);
392-
Marshal.PtrToStructure(myPtr, record.TopLevelName);
390+
record.TopLevelName = *(global::Interop.UNICODE_STRING*)myPtr;
393391
string excludedName = Marshal.PtrToStringUni(record.TopLevelName.Buffer, record.TopLevelName.Length / 2);
394392
tmpExcludedTLNs.Add(excludedName);
395393
tmpExcludedNameTime.Add(excludedName, record.Time);
396394
}
397395
else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo)
398396
{
399397
IntPtr myPtr = IntPtr.Add(addr, 16);
400-
Marshal.PtrToStructure(myPtr, record.DomainInfo!);
398+
record.DomainInfo = *(LSA_FOREST_TRUST_DOMAIN_INFO*)myPtr;
401399
ForestTrustDomainInformation dom = new ForestTrustDomainInformation(record.Flags, record.DomainInfo!, record.Time);
402400
tmpDomainInformation.Add(dom);
403401
}
404402
else
405403
{
406404
IntPtr myPtr = IntPtr.Add(addr, 16);
407-
Marshal.PtrToStructure(myPtr, record.Data);
405+
record.Data = *(LSA_FOREST_TRUST_BINARY_DATA*)myPtr;
408406
int length = record.Data.Length;
409407
byte[] byteArray = new byte[length];
410408
if ((record.Data.Buffer != (IntPtr)0) && (length != 0))

src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@ internal sealed class LSA_FOREST_TRUST_RECORD
414414
[FieldOffset(16)]
415415
public global::Interop.UNICODE_STRING TopLevelName;
416416
[FieldOffset(16)]
417-
public LSA_FOREST_TRUST_BINARY_DATA Data = null!;
417+
public LSA_FOREST_TRUST_BINARY_DATA Data;
418418
[FieldOffset(16)]
419-
public LSA_FOREST_TRUST_DOMAIN_INFO? DomainInfo;
419+
public LSA_FOREST_TRUST_DOMAIN_INFO DomainInfo;
420420
}
421421

422422
[StructLayout(LayoutKind.Sequential)]
@@ -432,8 +432,7 @@ public LARGE_INTEGER()
432432
}
433433
}
434434

435-
[StructLayout(LayoutKind.Sequential)]
436-
internal sealed class LSA_FOREST_TRUST_DOMAIN_INFO
435+
internal struct LSA_FOREST_TRUST_DOMAIN_INFO
437436
{
438437
public IntPtr sid;
439438
public short DNSNameLength;
@@ -444,8 +443,7 @@ internal sealed class LSA_FOREST_TRUST_DOMAIN_INFO
444443
public IntPtr NetBIOSNameBuffer;
445444
}
446445

447-
[StructLayout(LayoutKind.Sequential)]
448-
internal sealed class LSA_FOREST_TRUST_BINARY_DATA
446+
internal struct LSA_FOREST_TRUST_BINARY_DATA
449447
{
450448
public int Length;
451449
public IntPtr Buffer;

src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections;
77
using Xunit;
88
using Xunit.Sdk;
9+
using System.Reflection;
910

1011
namespace System.DirectoryServices.Tests
1112
{
@@ -14,6 +15,13 @@ public partial class DirectoryServicesTests
1415
internal static bool IsLdapConfigurationExist => LdapConfiguration.Configuration != null;
1516
internal static bool IsActiveDirectoryServer => IsLdapConfigurationExist && LdapConfiguration.Configuration.IsActiveDirectoryServer;
1617

18+
[Fact]
19+
public void TestGetAllTypes()
20+
{
21+
Type[] allTypes = typeof(DirectoryEntry).Assembly.GetTypes();
22+
Assert.Contains(typeof(DirectoryEntry), allTypes);
23+
}
24+
1725
[ConditionalFact(nameof(IsLdapConfigurationExist))]
1826
public void TestOU() // adding and removing organization unit
1927
{

0 commit comments

Comments
 (0)