Skip to content

Commit

Permalink
Port Access violation occurring in System.DirectoryServices.ActiveDir…
Browse files Browse the repository at this point in the history
…ectory.ForestTrustRelationshipInformation fix from .netfx to .net-core (#66726)

* foresttrust port from dotnetfx

* fixed nullable reference error
  • Loading branch information
kumarravik78c committed Mar 31, 2022
1 parent 631389f commit fe0f600
Showing 1 changed file with 30 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ForestTrustRelationshipInformation : TrustRelationshipInformation
private StringCollection _excludedNames = new StringCollection();
private ForestTrustDomainInfoCollection _domainInfo = new ForestTrustDomainInfoCollection();
private ArrayList _binaryData = new ArrayList();
private ArrayList _binaryRecordType = new ArrayList();
private Hashtable _excludedNameTime = new Hashtable();
private ArrayList _binaryDataTime = new ArrayList();
internal bool retrieved;
Expand Down Expand Up @@ -97,20 +98,14 @@ public void Save()
int toplevelNamesCount = TopLevelNames.Count;
int excludedNamesCount = ExcludedTopLevelNames.Count;
int trustedDomainCount = TrustedDomainInformation.Count;
int binaryDataCount = 0;
int binaryDataCount = _binaryData.Count;

checked
{
count += toplevelNamesCount;
count += excludedNamesCount;
count += trustedDomainCount;
if (_binaryData.Count != 0)
{
binaryDataCount = _binaryData.Count;
// for the ForestTrustRecordTypeLast record
count++;
count += binaryDataCount;
}
count += binaryDataCount;

// allocate the memory for all the records
records = Marshal.AllocHGlobal(count * IntPtr.Size);
Expand Down Expand Up @@ -212,43 +207,31 @@ public void Save()
currentCount++;
}

if (binaryDataCount > 0)
for (int i = 0; i < binaryDataCount; i++)
{
// now begin to construct ForestTrustRecordTypeLast
LSA_FOREST_TRUST_RECORD lastRecord = new LSA_FOREST_TRUST_RECORD();
lastRecord.Flags = 0;
lastRecord.ForestTrustType = LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustRecordTypeLast;
LSA_FOREST_TRUST_RECORD record = new LSA_FOREST_TRUST_RECORD();
record.Flags = 0;
record.Time = (LARGE_INTEGER)_binaryDataTime[i]!;
record.Data.Length = ((byte[])_binaryData[i]!).Length;
record.ForestTrustType = (LSA_FOREST_TRUST_RECORD_TYPE)_binaryRecordType[i]!;
record.Data = new LSA_FOREST_TRUST_BINARY_DATA();
if (record.Data.Length == 0)
{
record.Data.Buffer = (IntPtr)0;
}
else
{
record.Data.Buffer = Marshal.AllocHGlobal(record.Data.Length);
ptrList.Add(record.Data.Buffer);
Marshal.Copy((byte[])_binaryData[i]!, 0, record.Data.Buffer, record.Data.Length);
}
tmpPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LSA_FOREST_TRUST_RECORD)));
ptrList.Add(tmpPtr);
Marshal.StructureToPtr(lastRecord, tmpPtr, false);
Marshal.WriteIntPtr(records, IntPtr.Size * currentCount, tmpPtr);
currentCount++;

for (int i = 0; i < binaryDataCount; i++)
{
// now begin to construct excluded top leve name record
LSA_FOREST_TRUST_RECORD record = new LSA_FOREST_TRUST_RECORD();
record.Flags = 0;
record.Time = (LARGE_INTEGER)_binaryDataTime[i]!;
record.Data.Length = ((byte[])_binaryData[i]!).Length;
if (record.Data.Length == 0)
{
record.Data.Buffer = (IntPtr)0;
}
else
{
record.Data.Buffer = Marshal.AllocHGlobal(record.Data.Length);
ptrList.Add(record.Data.Buffer);
Marshal.Copy((byte[])_binaryData[i]!, 0, record.Data.Buffer, record.Data.Length);
}
tmpPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LSA_FOREST_TRUST_RECORD)));
ptrList.Add(tmpPtr);
Marshal.StructureToPtr(record, tmpPtr, false);
Marshal.StructureToPtr(record, tmpPtr, false);

Marshal.WriteIntPtr(records, IntPtr.Size * currentCount, tmpPtr);
Marshal.WriteIntPtr(records, IntPtr.Size * currentCount, tmpPtr);

currentCount++;
}
currentCount++;
}

// finally construct the LSA_FOREST_TRUST_INFORMATION
Expand Down Expand Up @@ -350,6 +333,7 @@ private void GetForestTrustInfoHelper()
ArrayList tmpBinaryData = new ArrayList();
Hashtable tmpExcludedNameTime = new Hashtable();
ArrayList tmpBinaryDataTime = new ArrayList();
ArrayList tmpBinaryRecordType = new ArrayList();

try
{
Expand Down Expand Up @@ -412,16 +396,15 @@ private void GetForestTrustInfoHelper()
}
else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo)
{
IntPtr myPtr = IntPtr.Add(addr, 16);
Marshal.PtrToStructure(myPtr, record.DomainInfo!);
ForestTrustDomainInformation dom = new ForestTrustDomainInformation(record.Flags, record.DomainInfo!, record.Time);
tmpDomainInformation.Add(dom);
}
else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustRecordTypeLast)
{
// enumeration is done, but we might still have some unrecognized entries after that
continue;
}
else
{
IntPtr myPtr = IntPtr.Add(addr, 16);
Marshal.PtrToStructure(myPtr, record.Data);
int length = record.Data.Length;
byte[] byteArray = new byte[length];
if ((record.Data.Buffer != (IntPtr)0) && (length != 0))
Expand All @@ -430,6 +413,7 @@ private void GetForestTrustInfoHelper()
}
tmpBinaryData.Add(byteArray);
tmpBinaryDataTime.Add(record.Time);
tmpBinaryRecordType.Add((int)record.ForestTrustType);
}
}
}
Expand All @@ -446,6 +430,7 @@ private void GetForestTrustInfoHelper()
_binaryData = tmpBinaryData;
_excludedNameTime = tmpExcludedNameTime;
_binaryDataTime = tmpBinaryDataTime;
_binaryRecordType = tmpBinaryRecordType;

// mark it as retrieved
retrieved = true;
Expand Down

0 comments on commit fe0f600

Please sign in to comment.