Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bin_match_json): check variable type before using #6150

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion libexec/scoop-search.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ function bin_match($manifest, $query) {

function bin_match_json($json, $query) {
[System.Text.Json.JsonElement]$bin = [System.Text.Json.JsonElement]::new()
if (!$json.RootElement.TryGetProperty('bin', [ref] $bin)) { return $false }
# check if RootElement is object type
if ($json.RootElement.ValueKind -eq [System.Text.Json.JsonValueKind]::Object) {
# try to get 'bin' attribute
if (!$json.RootElement.TryGetProperty('bin', [ref] $bin)) { return $false }
} elseif ($json.RootElement.ValueKind -eq [System.Text.Json.JsonValueKind]::Array) {
# or if RootElement is an array, then do not use TryGetProperty
foreach ($item in $json.RootElement.EnumerateArray()) {
if ($item.ValueKind -eq [System.Text.Json.JsonValueKind]::Object -and $item.TryGetProperty('bin', [ref] $bin)) {
break
}
}
if ($bin.ValueKind -eq [System.Text.Json.JsonValueKind]::Undefined) { return $false }
} else {
return $false
}

$bins = @()
if ($bin.ValueKind -eq [System.Text.Json.JsonValueKind]::String -and [System.IO.Path]::GetFileNameWithoutExtension($bin) -match $query) {
$bins += [System.IO.Path]::GetFileName($bin)
Expand Down