Skip to content

Commit a8213b5

Browse files
authored
Remove redundant local assignment in AclCommands (#14358)
1 parent f6fa262 commit a8213b5

1 file changed

Lines changed: 25 additions & 43 deletions

File tree

src/Microsoft.PowerShell.Security/security/AclCommands.cs

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -292,20 +292,17 @@ public static AuthorizationRuleCollection GetAccess(PSObject instance)
292292
}
293293

294294
// Get DACL
295-
AuthorizationRuleCollection dacl;
296295
CommonObjectSecurity cos = sd as CommonObjectSecurity;
297296
if (cos != null)
298297
{
299-
dacl = cos.GetAccessRules(true, true, typeof(NTAccount));
298+
return cos.GetAccessRules(true, true, typeof(NTAccount));
300299
}
301300
else
302301
{
303302
DirectoryObjectSecurity dos = sd as DirectoryObjectSecurity;
304303
Dbg.Diagnostics.Assert(dos != null, "Acl should be of type CommonObjectSecurity or DirectoryObjectSecurity");
305-
dacl = dos.GetAccessRules(true, true, typeof(NTAccount));
304+
return dos.GetAccessRules(true, true, typeof(NTAccount));
306305
}
307-
308-
return dacl;
309306
}
310307

311308
/// <summary>
@@ -330,20 +327,17 @@ public static AuthorizationRuleCollection GetAudit(PSObject instance)
330327
PSTraceSource.NewArgumentException(nameof(instance));
331328
}
332329

333-
AuthorizationRuleCollection sacl;
334330
CommonObjectSecurity cos = sd as CommonObjectSecurity;
335331
if (cos != null)
336332
{
337-
sacl = cos.GetAuditRules(true, true, typeof(NTAccount));
333+
return cos.GetAuditRules(true, true, typeof(NTAccount));
338334
}
339335
else
340336
{
341337
DirectoryObjectSecurity dos = sd as DirectoryObjectSecurity;
342338
Dbg.Diagnostics.Assert(dos != null, "Acl should be of type CommonObjectSecurity or DirectoryObjectSecurity");
343-
sacl = dos.GetAuditRules(true, true, typeof(NTAccount));
339+
return dos.GetAuditRules(true, true, typeof(NTAccount));
344340
}
345-
346-
return sacl;
347341
}
348342

349343
/// <summary>
@@ -360,8 +354,7 @@ public static SecurityIdentifier GetCentralAccessPolicyId(PSObject instance)
360354
SessionState sessionState = new SessionState();
361355
string path = sessionState.Path.GetUnresolvedProviderPathFromPSPath(
362356
GetPath(instance));
363-
IntPtr pOwner = IntPtr.Zero, pGroup = IntPtr.Zero;
364-
IntPtr pDacl = IntPtr.Zero, pSacl = IntPtr.Zero, pSd = IntPtr.Zero;
357+
IntPtr pSd = IntPtr.Zero;
365358

366359
try
367360
{
@@ -370,10 +363,10 @@ public static SecurityIdentifier GetCentralAccessPolicyId(PSObject instance)
370363
path,
371364
NativeMethods.SeObjectType.SE_FILE_OBJECT,
372365
NativeMethods.SecurityInformation.SCOPE_SECURITY_INFORMATION,
373-
out pOwner,
374-
out pGroup,
375-
out pDacl,
376-
out pSacl,
366+
out IntPtr pOwner,
367+
out IntPtr pGroup,
368+
out IntPtr pDacl,
369+
out IntPtr pSacl,
377370
out pSd);
378371
if (rs != NativeMethods.ERROR_SUCCESS)
379372
{
@@ -385,8 +378,7 @@ public static SecurityIdentifier GetCentralAccessPolicyId(PSObject instance)
385378
return null;
386379
}
387380

388-
NativeMethods.ACL sacl = new NativeMethods.ACL();
389-
sacl = Marshal.PtrToStructure<NativeMethods.ACL>(pSacl);
381+
NativeMethods.ACL sacl = Marshal.PtrToStructure<NativeMethods.ACL>(pSacl);
390382
if (sacl.AceCount == 0)
391383
{
392384
return null;
@@ -396,8 +388,7 @@ public static SecurityIdentifier GetCentralAccessPolicyId(PSObject instance)
396388
IntPtr pAce = pSacl + Marshal.SizeOf(new NativeMethods.ACL());
397389
for (ushort aceIdx = 0; aceIdx < sacl.AceCount; aceIdx++)
398390
{
399-
NativeMethods.ACE_HEADER ace = new NativeMethods.ACE_HEADER();
400-
ace = Marshal.PtrToStructure<NativeMethods.ACE_HEADER>(pAce);
391+
NativeMethods.ACE_HEADER ace = Marshal.PtrToStructure<NativeMethods.ACE_HEADER>(pAce);
401392
Dbg.Diagnostics.Assert(ace.AceType ==
402393
NativeMethods.SYSTEM_SCOPED_POLICY_ID_ACE_TYPE,
403394
"Unexpected ACE type: " + ace.AceType.ToString(CultureInfo.CurrentCulture));
@@ -458,12 +449,11 @@ public static string GetCentralAccessPolicyName(PSObject instance)
458449
Marshal.Copy(capIdArray, 0, pCapId, capIdSize);
459450
IntPtr[] ppCapId = new IntPtr[1];
460451
ppCapId[0] = pCapId;
461-
uint capCount = 0;
462452
uint rs = NativeMethods.LsaQueryCAPs(
463453
ppCapId,
464454
1,
465455
out caps,
466-
out capCount);
456+
out uint capCount);
467457
if (rs != NativeMethods.STATUS_SUCCESS)
468458
{
469459
throw new Win32Exception((int)rs);
@@ -475,8 +465,7 @@ public static string GetCentralAccessPolicyName(PSObject instance)
475465
}
476466

477467
// Get the CAP name.
478-
NativeMethods.CENTRAL_ACCESS_POLICY cap = new NativeMethods.CENTRAL_ACCESS_POLICY();
479-
cap = Marshal.PtrToStructure<NativeMethods.CENTRAL_ACCESS_POLICY>(caps);
468+
NativeMethods.CENTRAL_ACCESS_POLICY cap = Marshal.PtrToStructure<NativeMethods.CENTRAL_ACCESS_POLICY>(caps);
480469
// LSA_UNICODE_STRING is composed of WCHARs, but its length is given in bytes.
481470
return Marshal.PtrToStringUni(cap.Name.Buffer, cap.Name.Length / 2);
482471
}
@@ -508,12 +497,11 @@ public static string[] GetAllCentralAccessPolicies(PSObject instance)
508497
try
509498
{
510499
// Retrieve all CAPs.
511-
uint capCount = 0;
512500
uint rs = NativeMethods.LsaQueryCAPs(
513501
null,
514502
0,
515503
out caps,
516-
out capCount);
504+
out uint capCount);
517505
if (rs != NativeMethods.STATUS_SUCCESS)
518506
{
519507
throw new Win32Exception((int)rs);
@@ -528,14 +516,13 @@ public static string[] GetAllCentralAccessPolicies(PSObject instance)
528516

529517
// Add CAP names and IDs to a string array.
530518
string[] policies = new string[capCount];
531-
NativeMethods.CENTRAL_ACCESS_POLICY cap = new NativeMethods.CENTRAL_ACCESS_POLICY();
532519
IntPtr capPtr = caps;
533520
for (uint capIdx = 0; capIdx < capCount; capIdx++)
534521
{
535522
// Retrieve CAP name.
536523
Dbg.Diagnostics.Assert(capPtr != IntPtr.Zero,
537524
"Invalid central access policies array");
538-
cap = Marshal.PtrToStructure<NativeMethods.CENTRAL_ACCESS_POLICY>(capPtr);
525+
NativeMethods.CENTRAL_ACCESS_POLICY cap = Marshal.PtrToStructure<NativeMethods.CENTRAL_ACCESS_POLICY>(capPtr);
539526
// LSA_UNICODE_STRING is composed of WCHARs, but its length is given in bytes.
540527
policies[capIdx] = "\"" + Marshal.PtrToStringUni(
541528
cap.Name.Buffer,
@@ -653,7 +640,7 @@ public string[] Path
653640
}
654641
}
655642

656-
private PSObject _inputObject = null;
643+
private PSObject _inputObject;
657644

658645
/// <summary>
659646
/// InputObject Parameter
@@ -695,7 +682,7 @@ public string[] LiteralPath
695682
}
696683
}
697684

698-
private bool _isLiteralPath = false;
685+
private bool _isLiteralPath;
699686

700687
/// <summary>
701688
/// Gets or sets the audit flag of the command. This flag
@@ -758,7 +745,6 @@ public SwitchParameter AllCentralAccessPolicies
758745
/// </summary>
759746
protected override void ProcessRecord()
760747
{
761-
Collection<PSObject> sd = null;
762748
AccessControlSections sections =
763749
AccessControlSections.Owner |
764750
AccessControlSections.Group |
@@ -855,7 +841,7 @@ protected override void ProcessRecord()
855841

856842
InvokeProvider.SecurityDescriptor.Get(rp, sections, context);
857843

858-
sd = context.GetAccumulatedObjects();
844+
Collection<PSObject> sd = context.GetAccumulatedObjects();
859845
if (sd != null)
860846
{
861847
AddBrokeredProperties(
@@ -921,7 +907,7 @@ public string[] Path
921907
}
922908
}
923909

924-
private PSObject _inputObject = null;
910+
private PSObject _inputObject;
925911

926912
/// <summary>
927913
/// InputObject Parameter
@@ -962,7 +948,7 @@ public string[] LiteralPath
962948
}
963949
}
964950

965-
private bool _isLiteralPath = false;
951+
private bool _isLiteralPath;
966952

967953
private object _securityDescriptor;
968954

@@ -1125,12 +1111,11 @@ private IntPtr GetSaclWithCapId(string capStr)
11251111
// be deallocated separately (but with the entire buffer
11261112
// returned by LsaQueryCAPs).
11271113
freeCapId = false;
1128-
uint capCount = 0;
11291114
rs = NativeMethods.LsaQueryCAPs(
11301115
null,
11311116
0,
11321117
out caps,
1133-
out capCount);
1118+
out uint capCount);
11341119
if (rs != NativeMethods.STATUS_SUCCESS)
11351120
{
11361121
throw new Win32Exception((int)rs);
@@ -1144,13 +1129,12 @@ private IntPtr GetSaclWithCapId(string capStr)
11441129
}
11451130

11461131
// Find the supplied string among available CAP names, use the corresponding CAPID.
1147-
NativeMethods.CENTRAL_ACCESS_POLICY cap = new NativeMethods.CENTRAL_ACCESS_POLICY();
11481132
IntPtr capPtr = caps;
11491133
for (uint capIdx = 0; capIdx < capCount; capIdx++)
11501134
{
11511135
Dbg.Diagnostics.Assert(capPtr != IntPtr.Zero,
11521136
"Invalid central access policies array");
1153-
cap = Marshal.PtrToStructure<NativeMethods.CENTRAL_ACCESS_POLICY>(capPtr);
1137+
NativeMethods.CENTRAL_ACCESS_POLICY cap = Marshal.PtrToStructure<NativeMethods.CENTRAL_ACCESS_POLICY>(capPtr);
11541138
// LSA_UNICODE_STRING is composed of WCHARs, but its length is given in bytes.
11551139
string capName = Marshal.PtrToStringUni(
11561140
cap.Name.Buffer,
@@ -1334,14 +1318,13 @@ protected override void ProcessRecord()
13341318

13351319
if (methodInfo != null)
13361320
{
1337-
CommonSecurityDescriptor aclCommonSD = _securityDescriptor as CommonSecurityDescriptor;
13381321
string sddl;
13391322

13401323
if (aclObjectSecurity != null)
13411324
{
13421325
sddl = aclObjectSecurity.GetSecurityDescriptorSddlForm(AccessControlSections.All);
13431326
}
1344-
else if (aclCommonSD != null)
1327+
else if (_securityDescriptor is CommonSecurityDescriptor aclCommonSD)
13451328
{
13461329
sddl = aclCommonSD.GetSddlForm(AccessControlSections.All);
13471330
}
@@ -1476,9 +1459,8 @@ protected override void ProcessRecord()
14761459
context.PassThru = Passthru;
14771460
if (_isLiteralPath)
14781461
{
1479-
ProviderInfo Provider = null;
1480-
PSDriveInfo Drive = null;
1481-
string pathStr = SessionState.Path.GetUnresolvedProviderPathFromPSPath(p, out Provider, out Drive);
1462+
string pathStr = SessionState.Path.GetUnresolvedProviderPathFromPSPath(
1463+
p, out ProviderInfo Provider, out PSDriveInfo Drive);
14821464
pathsToProcess.Add(new PathInfo(Drive, Provider, pathStr, SessionState));
14831465
context.SuppressWildcardExpansion = true;
14841466
}

0 commit comments

Comments
 (0)