Skip to content

Commit

Permalink
Apply feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Apr 1, 2024
1 parent 4c20968 commit 4dbf108
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ internal static ComTypes.ITypeInfo GetITypeInfoFromIDispatch(IDispatch dispatch)
return typeInfo;
}

internal static ComTypes.TYPEATTR GetTypeAttrForTypeInfo(ComTypes.ITypeInfo typeInfo)
internal static unsafe ComTypes.TYPEATTR GetTypeAttrForTypeInfo(ComTypes.ITypeInfo typeInfo)
{
IntPtr pAttrs;
typeInfo.GetTypeAttr(out pAttrs);
Expand All @@ -179,15 +179,15 @@ internal static ComTypes.TYPEATTR GetTypeAttrForTypeInfo(ComTypes.ITypeInfo type

try
{
return Marshal.PtrToStructure<ComTypes.TYPEATTR>(pAttrs);
return *(ComTypes.TYPEATTR*)pAttrs;
}
finally
{
typeInfo.ReleaseTypeAttr(pAttrs);
}
}

internal static ComTypes.TYPELIBATTR GetTypeAttrForTypeLib(ComTypes.ITypeLib typeLib)
internal static unsafe ComTypes.TYPELIBATTR GetTypeAttrForTypeLib(ComTypes.ITypeLib typeLib)
{
IntPtr pAttrs;
typeLib.GetLibAttr(out pAttrs);
Expand All @@ -200,7 +200,7 @@ internal static ComTypes.TYPELIBATTR GetTypeAttrForTypeLib(ComTypes.ITypeLib typ

try
{
return Marshal.PtrToStructure<ComTypes.TYPELIBATTR>(pAttrs);
return *(ComTypes.TYPELIBATTR*)pAttrs;
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
return new IDispatchMetaObject(parameter, this);
}

private static void GetFuncDescForDescIndex(ComTypes.ITypeInfo typeInfo, int funcIndex, out ComTypes.FUNCDESC funcDesc, out IntPtr funcDescHandle)
private static unsafe void GetFuncDescForDescIndex(ComTypes.ITypeInfo typeInfo, int funcIndex, out ComTypes.FUNCDESC funcDesc, out IntPtr funcDescHandle)
{
IntPtr pFuncDesc;
typeInfo.GetFuncDesc(funcIndex, out pFuncDesc);
Expand All @@ -339,7 +339,7 @@ private static void GetFuncDescForDescIndex(ComTypes.ITypeInfo typeInfo, int fun
throw Error.CannotRetrieveTypeInformation();
}

funcDesc = Marshal.PtrToStructure<ComTypes.FUNCDESC>(pFuncDesc);
funcDesc = *(ComTypes.FUNCDESC*)pFuncDesc;
funcDescHandle = pFuncDesc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static void TryAddKeyedTransient(
{
ThrowHelper.ThrowIfNull(collection);

TryAddKeyedTransient<TService, TService>(collection, serviceKey);
TryAddKeyedTransient(collection, typeof(TService), serviceKey, typeof(TService));
}

/// <summary>
Expand Down Expand Up @@ -202,7 +202,7 @@ public static void TryAddKeyedScoped(
{
ThrowHelper.ThrowIfNull(collection);

TryAddKeyedScoped<TService, TService>(collection, serviceKey);
TryAddKeyedScoped(collection, typeof(TService), serviceKey, typeof(TService));
}

/// <summary>
Expand Down Expand Up @@ -318,7 +318,7 @@ public static void TryAddKeyedSingleton(
{
ThrowHelper.ThrowIfNull(collection);

TryAddKeyedSingleton<TService, TService>(collection, serviceKey);
TryAddKeyedSingleton(collection, typeof(TService), serviceKey, typeof(TService));
}

/// <summary>
Expand Down Expand Up @@ -354,7 +354,7 @@ public static void TryAddKeyedSingleton<TService>(this IServiceCollection collec
ThrowHelper.ThrowIfNull(collection);
ThrowHelper.ThrowIfNull(instance);

var descriptor = ServiceDescriptor.KeyedSingleton(serviceKey, implementationInstance: instance);
var descriptor = ServiceDescriptor.KeyedSingleton(serviceType: typeof(TService), serviceKey, implementationInstance: instance);
ServiceCollectionDescriptorExtensions.TryAdd(collection, descriptor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static void TryAddTransient(
{
ThrowHelper.ThrowIfNull(collection);

TryAddTransient<TService, TService>(collection);
TryAddTransient(collection, typeof(TService), typeof(TService));
}

/// <summary>
Expand Down Expand Up @@ -272,7 +272,7 @@ public static void TryAddScoped(
{
ThrowHelper.ThrowIfNull(collection);

TryAddScoped<TService, TService>(collection);
TryAddScoped(collection, typeof(TService), typeof(TService));
}

/// <summary>
Expand Down Expand Up @@ -378,7 +378,7 @@ public static void TryAddSingleton(
{
ThrowHelper.ThrowIfNull(collection);

TryAddSingleton<TService, TService>(collection);
TryAddSingleton(collection, typeof(TService), typeof(TService));
}

/// <summary>
Expand Down Expand Up @@ -412,7 +412,7 @@ public static void TryAddSingleton<TService>(this IServiceCollection collection,
ThrowHelper.ThrowIfNull(collection);
ThrowHelper.ThrowIfNull(instance);

var descriptor = ServiceDescriptor.Singleton(implementationInstance: instance);
var descriptor = ServiceDescriptor.Singleton(serviceType: typeof(TService), implementationInstance: instance);
TryAdd(collection, descriptor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static IServiceCollection AddKeyedTransient<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddKeyedTransient<TService>(serviceKey, implementationFactory);
return services.AddKeyedTransient(typeof(TService), serviceKey, implementationFactory);
}

/// <summary>
Expand Down Expand Up @@ -324,10 +324,9 @@ public static IServiceCollection AddKeyedScoped<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddKeyedScoped<TService>(serviceKey, implementationFactory);
return services.AddKeyedScoped(typeof(TService), serviceKey, implementationFactory);
}


/// <summary>
/// Adds a singleton service of the type specified in <paramref name="serviceType"/> with an
/// implementation of the type specified in <paramref name="implementationType"/> to the
Expand Down Expand Up @@ -434,7 +433,7 @@ public static IServiceCollection AddKeyedSingleton(
{
ThrowHelper.ThrowIfNull(services);

return services.AddKeyedSingleton<TService, TService>(serviceKey);
return services.AddKeyedSingleton(typeof(TService), serviceKey, typeof(TService));
}

/// <summary>
Expand Down Expand Up @@ -483,7 +482,7 @@ public static IServiceCollection AddKeyedSingleton<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddKeyedSingleton<TService>(serviceKey, implementationFactory);
return services.AddKeyedSingleton(typeof(TService), serviceKey, implementationFactory);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static IServiceCollection AddTransient<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddTransient<TService>(implementationFactory);
return services.AddTransient(typeof(TService), implementationFactory);
}

/// <summary>
Expand Down Expand Up @@ -292,7 +292,7 @@ public static IServiceCollection AddScoped<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddScoped<TService>(implementationFactory);
return services.AddScoped(typeof(TService), implementationFactory);
}


Expand Down Expand Up @@ -435,7 +435,7 @@ public static IServiceCollection AddSingleton<TService, TImplementation>(
ThrowHelper.ThrowIfNull(services);
ThrowHelper.ThrowIfNull(implementationFactory);

return services.AddSingleton<TService>(implementationFactory);
return services.AddSingleton(typeof(TService), implementationFactory);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal SidList(Interop.SID_AND_ATTRIBUTES[] sidAndAttr)
TranslateSids(null, pSids);
}

private void TranslateSids(string target, IntPtr[] pSids)
private unsafe void TranslateSids(string target, IntPtr[] pSids)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "AuthZSet", "SidList: processing {0} SIDs", pSids.Length);

Expand Down Expand Up @@ -157,8 +157,8 @@ private void TranslateSids(string target, IntPtr[] pSids)

for (int i = 0; i < domainCount; i++)
{
domains[i] = Marshal.PtrToStructure<Interop.LSA_TRUST_INFORMATION>(pCurrentDomain);
pCurrentDomain = new IntPtr(pCurrentDomain.ToInt64() + Marshal.SizeOf<Interop.LSA_TRUST_INFORMATION>());
domains[i] = *(Interop.LSA_TRUST_INFORMATION*)pCurrentDomain;
pCurrentDomain = new IntPtr(pCurrentDomain.ToInt64() + sizeof(Interop.LSA_TRUST_INFORMATION));
}

GlobalDebug.WriteLineIf(GlobalDebug.Info, "AuthZSet", "SidList: got {0} groups in {1} domains", sidCount, domainCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ out pClientContext

// Extract TOKEN_GROUPS.GroupCount

Interop.TOKEN_GROUPS tokenGroups = Marshal.PtrToStructure<Interop.TOKEN_GROUPS>(pBuffer);
Interop.TOKEN_GROUPS tokenGroups = *(Interop.TOKEN_GROUPS*)pBuffer;

uint groupCount = tokenGroups.GroupCount;

Expand All @@ -141,13 +141,13 @@ out pClientContext
// each native SID_AND_ATTRIBUTES into a managed SID_AND_ATTR.
Interop.SID_AND_ATTRIBUTES[] groups = new Interop.SID_AND_ATTRIBUTES[groupCount];

IntPtr currentItem = new IntPtr(pBuffer.ToInt64() + Marshal.SizeOf<Interop.TOKEN_GROUPS>() - sizeof(Interop.SID_AND_ATTRIBUTES));
IntPtr currentItem = new IntPtr(pBuffer.ToInt64() + sizeof(Interop.TOKEN_GROUPS) - sizeof(Interop.SID_AND_ATTRIBUTES));

for (int i = 0; i < groupCount; i++)
{
groups[i] = Marshal.PtrToStructure<Interop.SID_AND_ATTRIBUTES>(currentItem);
groups[i] = *(Interop.SID_AND_ATTRIBUTES*)currentItem;

currentItem = new IntPtr(currentItem.ToInt64() + Marshal.SizeOf<Interop.SID_AND_ATTRIBUTES>());
currentItem += sizeof(Interop.SID_AND_ATTRIBUTES);
}

_groupSidList = new SidList(groups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,14 @@ internal static SidType ClassifySID(byte[] sid)
}


internal static SidType ClassifySID(IntPtr pSid)
internal static unsafe SidType ClassifySID(IntPtr pSid)
{
Debug.Assert(Interop.Advapi32.IsValidSid(pSid));

// Get the issuing authority and the first RID
IntPtr pIdentAuth = Interop.Advapi32.GetSidIdentifierAuthority(pSid);

Interop.Advapi32.SID_IDENTIFIER_AUTHORITY identAuth =
Marshal.PtrToStructure<Interop.Advapi32.SID_IDENTIFIER_AUTHORITY>(pIdentAuth);
Interop.Advapi32.SID_IDENTIFIER_AUTHORITY identAuth = *(Interop.Advapi32.SID_IDENTIFIER_AUTHORITY*)pIdentAuth;

IntPtr pRid = Interop.Advapi32.GetSidSubAuthority(pSid, 0);
int rid = Marshal.ReadInt32(pRid);
Expand Down Expand Up @@ -333,7 +332,7 @@ internal static bool IsSamUser()
}


internal static IntPtr GetCurrentUserSid()
internal static unsafe IntPtr GetCurrentUserSid()
{
SafeTokenHandle tokenHandle = null;
IntPtr pBuffer = IntPtr.Zero;
Expand Down Expand Up @@ -425,7 +424,7 @@ out tokenHandle
}

// Retrieve the user's SID from the user info
Interop.TOKEN_USER tokenUser = Marshal.PtrToStructure<Interop.TOKEN_USER>(pBuffer);
Interop.TOKEN_USER tokenUser = *(Interop.TOKEN_USER*)pBuffer;
IntPtr pUserSid = tokenUser.sidAndAttributes.Sid; // this is a reference into the NATIVE memory (into pBuffer)

Debug.Assert(Interop.Advapi32.IsValidSid(pUserSid));
Expand Down Expand Up @@ -457,7 +456,7 @@ out tokenHandle
}


internal static IntPtr GetMachineDomainSid()
internal static unsafe IntPtr GetMachineDomainSid()
{
SafeLsaPolicyHandle policyHandle = null;
IntPtr pBuffer = IntPtr.Zero;
Expand Down Expand Up @@ -496,7 +495,7 @@ internal static IntPtr GetMachineDomainSid()
}

Debug.Assert(pBuffer != IntPtr.Zero);
UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO info = Marshal.PtrToStructure<UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO>(pBuffer);
UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO info = *(UnsafeNativeMethods.POLICY_ACCOUNT_DOMAIN_INFO*)pBuffer;

Debug.Assert(Interop.Advapi32.IsValidSid(info.DomainSid));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,12 +918,12 @@ private unsafe Interop.BOOL ProcessClientCertificate(IntPtr ldapHandle, IntPtr C
var list = new ArrayList();
if (CAs != IntPtr.Zero)
{
SecPkgContext_IssuerListInfoEx trustedCAs = Marshal.PtrToStructure<SecPkgContext_IssuerListInfoEx>(CAs);
SecPkgContext_IssuerListInfoEx trustedCAs = *(SecPkgContext_IssuerListInfoEx*)CAs;
int issuerNumber = trustedCAs.cIssuers;
for (int i = 0; i < issuerNumber; i++)
{
IntPtr tempPtr = (IntPtr)((byte*)trustedCAs.aIssuers + Marshal.SizeOf<CRYPTOAPI_BLOB>() * (nint)i);
CRYPTOAPI_BLOB info = Marshal.PtrToStructure<CRYPTOAPI_BLOB>(tempPtr);
IntPtr tempPtr = (IntPtr)((byte*)trustedCAs.aIssuers + sizeof(CRYPTOAPI_BLOB) * (nint)i);
CRYPTOAPI_BLOB info = *(CRYPTOAPI_BLOB*)tempPtr;
int dataLength = info.cbData;

byte[] context = new byte[dataLength];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ private unsafe void FreeReplicaInfo(DS_REPL_INFO_TYPE type, IntPtr value, SafeLi

internal unsafe void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string? sourceServer, int option, SafeLibraryHandle libHandle)
{
int structSize = Marshal.SizeOf<Guid>();
IntPtr unmanagedGuid = (IntPtr)0;
int structSize = sizeof(Guid);
IntPtr unmanagedGuid = 0;
Guid guid = Guid.Empty;
AdamInstance? adamServer = null;
DomainController? dcServer = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public unsafe void Save()
{
try
{
IntPtr ptr = (IntPtr)0;
IntPtr ptr = 0;
fileTime = Marshal.AllocHGlobal(Marshal.SizeOf<FileTime>());
Interop.Kernel32.GetSystemTimeAsFileTime(fileTime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ internal static unsafe void SetTrustedDomainInfoStatus(DirectoryContext context,
}

// reconstruct the unmanaged structure to set it back
newInfo = Marshal.AllocHGlobal(Marshal.SizeOf<Interop.Advapi32.TRUSTED_DOMAIN_INFORMATION_EX>());
newInfo = Marshal.AllocHGlobal(sizeof(Interop.Advapi32.TRUSTED_DOMAIN_INFORMATION_EX));
Marshal.StructureToPtr(domainInfo, newInfo, false);

result = Interop.Advapi32.LsaSetTrustedDomainInfoByName(handle, trustedDomainName, Interop.Advapi32.TRUSTED_INFORMATION_CLASS.TrustedDomainInformationEx, newInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@ internal static bool IsSamUser()
}


internal static IntPtr GetCurrentUserSid()
internal static unsafe IntPtr GetCurrentUserSid()
{
SafeTokenHandle? tokenHandle = null;
IntPtr pBuffer = IntPtr.Zero;
Expand Down Expand Up @@ -2120,7 +2120,7 @@ out tokenHandle
}

// Retrieve the user's SID from the user info
global::Interop.TOKEN_USER tokenUser = Marshal.PtrToStructure<Interop.TOKEN_USER>(pBuffer)!;
global::Interop.TOKEN_USER tokenUser = *(Interop.TOKEN_USER*)pBuffer;
IntPtr pUserSid = tokenUser.sidAndAttributes.Sid; // this is a reference into the NATIVE memory (into pBuffer)

Debug.Assert(global::Interop.Advapi32.IsValidSid(pUserSid));
Expand All @@ -2147,7 +2147,7 @@ out tokenHandle
}
}

internal static IntPtr GetMachineDomainSid()
internal static unsafe IntPtr GetMachineDomainSid()
{
SafeLsaPolicyHandle? policyHandle = null;
IntPtr pBuffer = IntPtr.Zero;
Expand Down Expand Up @@ -2178,7 +2178,7 @@ internal static IntPtr GetMachineDomainSid()
}

Debug.Assert(pBuffer != IntPtr.Zero);
POLICY_ACCOUNT_DOMAIN_INFO info = Marshal.PtrToStructure<POLICY_ACCOUNT_DOMAIN_INFO>(pBuffer)!;
POLICY_ACCOUNT_DOMAIN_INFO info = *(POLICY_ACCOUNT_DOMAIN_INFO*)pBuffer;

Debug.Assert(global::Interop.Advapi32.IsValidSid(info.DomainSid));

Expand Down Expand Up @@ -2237,15 +2237,14 @@ internal static bool IsMachineDC(string? computerName)
}
}

internal static SidType ClassifySID(IntPtr pSid)
internal static unsafe SidType ClassifySID(IntPtr pSid)
{
Debug.Assert(global::Interop.Advapi32.IsValidSid(pSid));

// Get the issuing authority and the first RID
IntPtr pIdentAuth = global::Interop.Advapi32.GetSidIdentifierAuthority(pSid);

global::Interop.Advapi32.SID_IDENTIFIER_AUTHORITY identAuth =
Marshal.PtrToStructure<Interop.Advapi32.SID_IDENTIFIER_AUTHORITY>(pIdentAuth)!;
global::Interop.Advapi32.SID_IDENTIFIER_AUTHORITY identAuth = *(Interop.Advapi32.SID_IDENTIFIER_AUTHORITY*)pIdentAuth;

IntPtr pRid = global::Interop.Advapi32.GetSidSubAuthority(pSid, 0);
int rid = Marshal.ReadInt32(pRid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public unsafe void ToJS(object?[] value)
return;
}
slot.Length = value.Length;
int bytes = value.Length * Marshal.SizeOf<JSMarshalerArgument>();
int bytes = value.Length * sizeof(JSMarshalerArgument);
slot.Type = MarshalerType.Array;
JSMarshalerArgument* payload = (JSMarshalerArgument*)Marshal.AllocHGlobal(bytes);
Unsafe.InitBlock(payload, 0, (uint)bytes);
Expand Down
Loading

0 comments on commit 4dbf108

Please sign in to comment.