From 08d9d4ad8d07369292a5ea3e4e14f991086fc190 Mon Sep 17 00:00:00 2001 From: Nati Date: Sun, 15 Nov 2026 01:24:47 +0800 Subject: [PATCH 1/2] validate option values in Prepare-NativePrebuilt.sh A value-taking option used to read $2 and shift unconditionally, so '--sysroot --disconnected' stored --disconnected as the sysroot and left disconnected mode off, and a trailing value-less option died on set -u's unbound-variable error instead of naming the option. Every value-taking option now goes through require_value, which rejects missing, empty, and option-like values with an error naming the option. Flag-only options are unchanged. Launcher/Test-NativePrebuiltParser.sh exercises the parser: 19 cases covering each option, the review's --sysroot case, and valid command lines (which always run with --print-fingerprint-only so the test never triggers a real harvest). Fixes #274 --- Launcher/Prepare-NativePrebuilt.sh | 34 +++++--- Launcher/Test-NativePrebuiltParser.sh | 109 ++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 9 deletions(-) create mode 100755 Launcher/Test-NativePrebuiltParser.sh diff --git a/Launcher/Prepare-NativePrebuilt.sh b/Launcher/Prepare-NativePrebuilt.sh index ed3b7c109..bbf57a57f 100755 --- a/Launcher/Prepare-NativePrebuilt.sh +++ b/Launcher/Prepare-NativePrebuilt.sh @@ -53,20 +53,36 @@ Usage: Prepare-NativePrebuilt.sh --arch {x86_64|aarch64} [options] EOF } +# Every value-taking option goes through this before touching $2 or shifting: without it, +# "--sysroot --disconnected" stored "--disconnected" as the sysroot value and silently left +# disconnected mode off, and a trailing value-less option died on set -u's unbound-variable error +# instead of naming the option. Values that genuinely start with "--" are not supported; pass a +# path that does not collide with option syntax (https://github.com/patchzyy/Wiicompiled/issues/274). +require_value() { + if [[ $# -lt 2 || -z "$2" ]]; then + echo "Prepare-NativePrebuilt.sh: $1 requires a value" >&2 + exit 1 + fi + if [[ "$2" == --* ]]; then + echo "Prepare-NativePrebuilt.sh: $1 requires a value; \"$2\" looks like another option" >&2 + exit 1 + fi +} + while [[ $# -gt 0 ]]; do case "$1" in - --arch) arch=$2; shift 2 ;; - --output-dir) output_dir=$2; shift 2 ;; - --stage-dir) stage_dir=$2; shift 2 ;; + --arch) require_value "$@"; arch=$2; shift 2 ;; + --output-dir) require_value "$@"; output_dir=$2; shift 2 ;; + --stage-dir) require_value "$@"; stage_dir=$2; shift 2 ;; --keep-stage) keep_stage=1; shift ;; --reuse-stage) reuse_stage=1; shift ;; - --parallel) parallel=$2; shift 2 ;; + --parallel) require_value "$@"; parallel=$2; shift 2 ;; --print-fingerprint-only) print_fingerprint_only=1; shift ;; - --toolchain-dir) toolchain_dir=$2; shift 2 ;; - --ninja-bin) ninja_bin=$2; shift 2 ;; - --cmake-bin) cmake_bin=$2; shift 2 ;; - --llvm-dir) llvm_dir=$2; shift 2 ;; - --sysroot) sysroot=$2; shift 2 ;; + --toolchain-dir) require_value "$@"; toolchain_dir=$2; shift 2 ;; + --ninja-bin) require_value "$@"; ninja_bin=$2; shift 2 ;; + --cmake-bin) require_value "$@"; cmake_bin=$2; shift 2 ;; + --llvm-dir) require_value "$@"; llvm_dir=$2; shift 2 ;; + --sysroot) require_value "$@"; sysroot=$2; shift 2 ;; --disconnected) disconnected=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Prepare-NativePrebuilt.sh: unknown argument: $1" >&2; exit 1 ;; diff --git a/Launcher/Test-NativePrebuiltParser.sh b/Launcher/Test-NativePrebuiltParser.sh new file mode 100755 index 000000000..d98412f32 --- /dev/null +++ b/Launcher/Test-NativePrebuiltParser.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env bash +# Parser tests for Prepare-NativePrebuilt.sh +# (https://github.com/patchzyy/Wiicompiled/issues/274). +# +# Rejection cases must fail during argument parsing, before any filesystem check, configure, or +# build side effect. Acceptance cases always run with --print-fingerprint-only so that even on a +# maintainer machine with the portable toolchain present the script exits at the fingerprint +# print instead of harvesting a package; on machines without the toolchain the run still fails +# later at the portable-tools assertion, which is not a parser error. +# +# No network, no toolchain, and no write access outside this checkout's build paths is needed: +# every command line under test reaches its expected outcome before the first side effect. +set -uo pipefail + +script_under_test=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/Prepare-NativePrebuilt.sh + +passed=0 +failed=0 + +# expect_reject +# The script must exit nonzero and its error must both name the affected option and complain +# about the value; a bare set -u unbound-variable crash or an unnamed failure does not count. +expect_reject() { + local description=$1 expected_option=$2 + shift 2 + local output status + output=$("$script_under_test" "$@" 2>&1) + status=$? + if [[ $status -eq 0 ]]; then + echo "FAIL: $description: expected rejection, exited 0" + failed=$((failed + 1)) + return + fi + if [[ "$output" != *"$expected_option"* || "$output" != *"requires a value"* ]]; then + echo "FAIL: $description: error does not name $expected_option with a value complaint:" + echo "$output" + failed=$((failed + 1)) + return + fi + echo "PASS: $description" + passed=$((passed + 1)) +} + +# expect_parse_ok +# Parsing must accept the command line. The run may still fail afterward (missing portable +# toolchain), but never with one of the parser errors. +expect_parse_ok() { + local description=$1 + shift + local output status + output=$("$script_under_test" "$@" 2>&1) + status=$? + if [[ "$output" == *"requires a value"* || "$output" == *"unknown argument"* \ + || "$output" == *"must be x86_64 or aarch64"* ]]; then + echo "FAIL: $description: rejected a valid command line:" + echo "$output" + failed=$((failed + 1)) + return + fi + echo "PASS: $description (exit $status)" + passed=$((passed + 1)) +} + +# The case from the PR #270 review: --disconnected must not be eaten as the sysroot value while +# disconnected mode silently stays off. +expect_reject "--sysroot swallowing --disconnected" "--sysroot" \ + --arch x86_64 --sysroot --disconnected + +# A following flag is not a valid value for any value-taking option. +for option in --arch --output-dir --stage-dir --parallel --toolchain-dir \ + --ninja-bin --cmake-bin --llvm-dir --sysroot; do + expect_reject "$option with an option-like value" "$option" \ + --arch x86_64 "$option" --keep-stage +done + +# A trailing value-less option must fail naming the option, not on set -u's unbound variable. +expect_reject "--arch without a value" "--arch" --arch +expect_reject "--parallel without a value" "--parallel" --arch x86_64 --parallel +expect_reject "--sysroot without a value" "--sysroot" --arch x86_64 --sysroot + +# An explicitly empty value is as useless as a missing one. +expect_reject "--arch with an empty value" "--arch" --arch "" +expect_reject "--output-dir with an empty value" "--output-dir" --arch x86_64 --output-dir "" + +# Valid command lines and flag-only options keep working. +expect_parse_ok "full valid option set parses" \ + --arch x86_64 --parallel 4 --keep-stage --reuse-stage --disconnected --print-fingerprint-only +expect_parse_ok "explicit paths parse" \ + --arch aarch64 --output-dir /tmp/np-out --stage-dir /tmp/np-stage \ + --toolchain-dir /tmp/np-tools --ninja-bin /tmp/np-ninja --cmake-bin /tmp/np-cmake \ + --llvm-dir /tmp/np-llvm --sysroot /tmp/np-sysroot --print-fingerprint-only +expect_parse_ok "flag-only options parse" \ + --arch x86_64 --keep-stage --reuse-stage --disconnected --print-fingerprint-only + +# -h stays a clean exit-0 usage print. +help_output=$("$script_under_test" -h 2>&1) +help_status=$? +if [[ $help_status -eq 0 && "$help_output" == *"Usage:"* ]]; then + echo "PASS: -h prints usage and exits 0" + passed=$((passed + 1)) +else + echo "FAIL: -h no longer prints usage with exit 0:" + echo "$help_output" + failed=$((failed + 1)) +fi + +echo +echo "$passed passed, $failed failed" +[[ $failed -eq 0 ]] From 31849c78b340f6bede9a52efc395b6f015e8168f Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:15:37 +0100 Subject: [PATCH 2/2] reject -h as an option value in Prepare-NativePrebuilt.sh --- Launcher/Prepare-NativePrebuilt.sh | 6 +++--- Launcher/Test-NativePrebuiltParser.sh | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Launcher/Prepare-NativePrebuilt.sh b/Launcher/Prepare-NativePrebuilt.sh index bbf57a57f..61a94e36d 100755 --- a/Launcher/Prepare-NativePrebuilt.sh +++ b/Launcher/Prepare-NativePrebuilt.sh @@ -56,14 +56,14 @@ EOF # Every value-taking option goes through this before touching $2 or shifting: without it, # "--sysroot --disconnected" stored "--disconnected" as the sysroot value and silently left # disconnected mode off, and a trailing value-less option died on set -u's unbound-variable error -# instead of naming the option. Values that genuinely start with "--" are not supported; pass a -# path that does not collide with option syntax (https://github.com/patchzyy/Wiicompiled/issues/274). +# instead of naming the option. Values that genuinely start with "--", or are exactly "-h", are not +# supported; pass a path that does not collide with option syntax (https://github.com/patchzyy/Wiicompiled/issues/274). require_value() { if [[ $# -lt 2 || -z "$2" ]]; then echo "Prepare-NativePrebuilt.sh: $1 requires a value" >&2 exit 1 fi - if [[ "$2" == --* ]]; then + if [[ "$2" == --* || "$2" == -h ]]; then echo "Prepare-NativePrebuilt.sh: $1 requires a value; \"$2\" looks like another option" >&2 exit 1 fi diff --git a/Launcher/Test-NativePrebuiltParser.sh b/Launcher/Test-NativePrebuiltParser.sh index d98412f32..f1da4f247 100755 --- a/Launcher/Test-NativePrebuiltParser.sh +++ b/Launcher/Test-NativePrebuiltParser.sh @@ -81,6 +81,7 @@ expect_reject "--sysroot without a value" "--sysroot" --arch x86_64 --sysroot # An explicitly empty value is as useless as a missing one. expect_reject "--arch with an empty value" "--arch" --arch "" expect_reject "--output-dir with an empty value" "--output-dir" --arch x86_64 --output-dir "" +expect_reject "--sysroot swallowing -h" "--sysroot" --arch x86_64 --sysroot -h # Valid command lines and flag-only options keep working. expect_parse_ok "full valid option set parses" \