From 485be0285c4f1b20194a72639517f0c3512bfe5c Mon Sep 17 00:00:00 2001 From: jantari Date: Fri, 12 Jan 2024 17:14:44 +0100 Subject: [PATCH 1/3] fail out of AND tests as soon as 1 came back false This could fix / work around issue #103 because the problematic package: https://download.lenovo.com/pccbbs/mobiles/n3acv41w_2_.xml has lots of idependency tests in a long AND-clause and the one that's hanging is the very last one. So if any previous test had already failed we could just skip all the others, thus avoiding the problematic ExternalDetection with MCUFWRevCheck.exe. --- private/Resolve-XMLDependencies.ps1 | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/private/Resolve-XMLDependencies.ps1 b/private/Resolve-XMLDependencies.ps1 index dd3d4ce..c498569 100644 --- a/private/Resolve-XMLDependencies.ps1 +++ b/private/Resolve-XMLDependencies.ps1 @@ -6,13 +6,16 @@ [Parameter( Mandatory = $true )] [string]$PackagePath, [switch]$TreatUnsupportedAsPassed, - [switch]$FailInboxDrivers + [switch]$FailInboxDrivers, + [switch]$ParentNodeIsAnd ) $XMLTreeDepth++ [DependencyParserState]$ParserState = 0 + $i = 0 foreach ($XMLTREE in $XMLIN) { + $i++ Write-Debug "$('- ' * $XMLTreeDepth )|> Node: $($XMLTREE.SchemaInfo.Name)" if ($XMLTREE.SchemaInfo.Name -eq 'Not') { @@ -34,7 +37,7 @@ } } } else { - $SubtreeResults = Resolve-XMLDependencies -XMLIN $XMLTREE.ChildNodes -PackagePath $PackagePath -TreatUnsupportedAsPassed:$TreatUnsupportedAsPassed -FailInboxDrivers:$FailInboxDrivers + $SubtreeResults = Resolve-XMLDependencies -XMLIN $XMLTREE.ChildNodes -PackagePath $PackagePath -TreatUnsupportedAsPassed:$TreatUnsupportedAsPassed -FailInboxDrivers:$FailInboxDrivers -ParentNodeIsAnd:($XMLTREE.SchemaInfo.Name -eq 'And') switch ($XMLTREE.SchemaInfo.Name) { 'And' { Write-Debug "$('- ' * $XMLTreeDepth)Tree was AND: Results: $subtreeresults" @@ -50,6 +53,11 @@ Write-Debug "$('- ' * $XMLTreeDepth)< Returning $($Result -bxor $ParserState) from node $($XMLTREE.SchemaInfo.Name)" $Result -bxor $ParserState + if ($ParentNodeIsAnd -and $i -ne $XMLIN.Count -and -not ($Result -bxor $ParserState)) { + Write-Debug "$('- ' * $XMLTreeDepth)Quitting AND evaluation early because we already got one FALSE." + $ParserState = 0 # DO_HAVE + break; + } $ParserState = 0 # DO_HAVE } From a5cf2d39ce26e3e701bc82d8166cd057659c4ae4 Mon Sep 17 00:00:00 2001 From: jantari Date: Fri, 19 Jan 2024 22:31:32 +0100 Subject: [PATCH 2/3] close StdIn stream of external executables (fix #103) --- private/Invoke-PackageCommand.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/private/Invoke-PackageCommand.ps1 b/private/Invoke-PackageCommand.ps1 index b938a7d..eb166c2 100644 --- a/private/Invoke-PackageCommand.ps1 +++ b/private/Invoke-PackageCommand.ps1 @@ -115,11 +115,13 @@ $process.StartInfo.WorkingDirectory = $WorkingDirectory $process.StartInfo.FileName = $Executable $process.StartInfo.Arguments = $Arguments + $process.StartInfo.RedirectStandardInput = $true $process.StartInfo.RedirectStandardOutput = $true $process.StartInfo.RedirectStandardError = $true if ($FallbackToShellExecute) { $process.StartInfo.UseShellExecute = $true + $process.StartInfo.RedirectStandardInput = $false $process.StartInfo.RedirectStandardOutput = $false $process.StartInfo.RedirectStandardError = $false } @@ -154,6 +156,8 @@ # See issue #25 and https://stackoverflow.com/questions/11531068/powershell-capturing-standard-out-and-error-with-process-object $StdOutAsync = $process.StandardOutput.ReadToEndAsync() $StdErrAsync = $process.StandardError.ReadToEndAsync() + # https://github.com/jantari/LSUClient/issues/103 + $process.StandardInput.Close() } $process.WaitForExit() From 2381f5a10434c2d243c0fad1ec2826f632c6e062 Mon Sep 17 00:00:00 2001 From: jantari Date: Sat, 20 Jan 2024 16:42:09 +0100 Subject: [PATCH 3/3] comment and simplify flow a bit after 485be02 --- private/Resolve-XMLDependencies.ps1 | 50 ++++++++++++++++------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/private/Resolve-XMLDependencies.ps1 b/private/Resolve-XMLDependencies.ps1 index c498569..9f847db 100644 --- a/private/Resolve-XMLDependencies.ps1 +++ b/private/Resolve-XMLDependencies.ps1 @@ -23,38 +23,42 @@ Write-Debug "$('- ' * $XMLTreeDepth)Switched state to: $ParserState" } - $Result = if ($XMLTREE.SchemaInfo.Name -like "_*") { - switch (Test-MachineSatisfiesDependency -Dependency $XMLTREE -PackagePath $PackagePath -DebugIndent $XMLTreeDepth -FailInboxDrivers:$FailInboxDrivers) { - 0 { - $true - } - -1 { - $false - } - -2 { - Write-Debug "$('- ' * $XMLTreeDepth)Something unsupported encountered in: $($XMLTREE.SchemaInfo.Name)" - if ($TreatUnsupportedAsPassed) { $true } else { $false } + $Result = switch -Wildcard ($XMLTREE.SchemaInfo.Name) { + '_*' { + switch (Test-MachineSatisfiesDependency -Dependency $XMLTREE -PackagePath $PackagePath -DebugIndent $XMLTreeDepth -FailInboxDrivers:$FailInboxDrivers) { + 0 { + $true + } + -1 { + $false + } + -2 { + Write-Debug "$('- ' * $XMLTreeDepth)Something unsupported encountered in: $($XMLTREE.SchemaInfo.Name)" + if ($TreatUnsupportedAsPassed) { $true } else { $false } + } } } - } else { - $SubtreeResults = Resolve-XMLDependencies -XMLIN $XMLTREE.ChildNodes -PackagePath $PackagePath -TreatUnsupportedAsPassed:$TreatUnsupportedAsPassed -FailInboxDrivers:$FailInboxDrivers -ParentNodeIsAnd:($XMLTREE.SchemaInfo.Name -eq 'And') - switch ($XMLTREE.SchemaInfo.Name) { - 'And' { - Write-Debug "$('- ' * $XMLTreeDepth)Tree was AND: Results: $subtreeresults" - if ($subtreeresults -contains $false) { $false } else { $true } - } - default { - Write-Debug "$('- ' * $XMLTreeDepth)Tree was OR: Results: $subtreeresults" - if ($subtreeresults -contains $true ) { $true } else { $false } - } + 'And' { + $SubtreeResults = Resolve-XMLDependencies -XMLIN $XMLTREE.ChildNodes -PackagePath $PackagePath -TreatUnsupportedAsPassed:$TreatUnsupportedAsPassed -FailInboxDrivers:$FailInboxDrivers -ParentNodeIsAnd + Write-Debug "$('- ' * $XMLTreeDepth)Tree was AND: Results: $SubtreeResults" + if ($SubtreeResults -contains $false) { $false } else { $true } + } + default { + $SubtreeResults = Resolve-XMLDependencies -XMLIN $XMLTREE.ChildNodes -PackagePath $PackagePath -TreatUnsupportedAsPassed:$TreatUnsupportedAsPassed -FailInboxDrivers:$FailInboxDrivers + Write-Debug "$('- ' * $XMLTreeDepth)Tree was OR: Results: $SubtreeResults" + if ($SubtreeResults -contains $true ) { $true } else { $false } } } Write-Debug "$('- ' * $XMLTreeDepth)< Returning $($Result -bxor $ParserState) from node $($XMLTREE.SchemaInfo.Name)" $Result -bxor $ParserState + + # If we're evaluating the children of an And-node, and we get a negative result before the last child-element, + # we can stop and don't have to process the remaining children anymore as the And-result will always be false. + # This speeds things up but it can also avoid even running problematic tests, e.g. some ExternalDetections. if ($ParentNodeIsAnd -and $i -ne $XMLIN.Count -and -not ($Result -bxor $ParserState)) { - Write-Debug "$('- ' * $XMLTreeDepth)Quitting AND evaluation early because we already got one FALSE." + Write-Debug "$('- ' * $XMLTreeDepth)Stopping AND evaluation early" $ParserState = 0 # DO_HAVE break; }