From d115794bf73566a1d401795b8512281951fe26d0 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 31 Jul 2018 08:33:49 +1000 Subject: [PATCH] PrivilegeUtil: use native methods to get pointer offset (#43461) --- .../Ansible.ModuleUtils.PrivilegeUtil.psm1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.PrivilegeUtil.psm1 b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.PrivilegeUtil.psm1 index 97eb44d4bf5..9152d514113 100644 --- a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.PrivilegeUtil.psm1 +++ b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.PrivilegeUtil.psm1 @@ -166,7 +166,7 @@ namespace Ansible.PrivilegeUtil NativeHelpers.TOKEN_PRIVILEGES privilegeInfo = (NativeHelpers.TOKEN_PRIVILEGES)Marshal.PtrToStructure(privilegesPtr, typeof(NativeHelpers.TOKEN_PRIVILEGES)); privileges = new NativeHelpers.LUID_AND_ATTRIBUTES[privilegeInfo.PrivilegeCount]; - PtrToStructureArray(privileges, privilegesPtr.ToInt64() + Marshal.SizeOf(privilegeInfo.PrivilegeCount)); + PtrToStructureArray(privileges, IntPtr.Add(privilegesPtr, Marshal.SizeOf(privilegeInfo.PrivilegeCount))); } finally { @@ -301,7 +301,7 @@ namespace Ansible.PrivilegeUtil // Marshal the oldStatePtr to the struct NativeHelpers.TOKEN_PRIVILEGES oldState = (NativeHelpers.TOKEN_PRIVILEGES)Marshal.PtrToStructure(oldStatePtr, typeof(NativeHelpers.TOKEN_PRIVILEGES)); oldStatePrivileges = new NativeHelpers.LUID_AND_ATTRIBUTES[oldState.PrivilegeCount]; - PtrToStructureArray(oldStatePrivileges, oldStatePtr.ToInt64() + Marshal.SizeOf(oldState.PrivilegeCount)); + PtrToStructureArray(oldStatePrivileges, IntPtr.Add(oldStatePtr, Marshal.SizeOf(oldState.PrivilegeCount))); } finally { @@ -334,11 +334,11 @@ namespace Ansible.PrivilegeUtil return name.ToString(); } - private static void PtrToStructureArray(T[] array, Int64 pointerAddress) + private static void PtrToStructureArray(T[] array, IntPtr ptr) { - Int64 pointerOffset = pointerAddress; - for (int i = 0; i < array.Length; i++, pointerOffset += Marshal.SizeOf(typeof(T))) - array[i] = (T)Marshal.PtrToStructure(new IntPtr(pointerOffset), typeof(T)); + IntPtr ptrOffset = ptr; + for (int i = 0; i < array.Length; i++, ptrOffset = IntPtr.Add(ptrOffset, Marshal.SizeOf(typeof(T)))) + array[i] = (T)Marshal.PtrToStructure(ptrOffset, typeof(T)); } private static int StructureToBytes(T structure, byte[] array, int offset)