CLI: xcl rpf and xcl shaders silently ignore unknown options and mistyped values, and still exit 0 #2266

Closed
opened 2026-11-26 05:25:00 +00:00 by github-bot · 3 comments
Owner

@⁠itsonewi on GitHub · 2026-11-25 11:53 UTC · original

Summary

xcl rpf and xcl shaders read their options by position and drop whatever they do not recognise, so a typo or a forgotten value changes what the command does and the command still exits 0. xcl disasm is the one that reports it, so the fix has a precedent right next to the bug.

crates/xcl/src/rpf.rs (1a0bbe3, line 100):

let opt = |n: &str| args.iter().position(|a| a == n).and_then(|i| args.get(i + 1)).cloned();
let top: usize = opt("--top").and_then(|v| v.parse().ok()).unwrap_or(30);

crates/xcl/src/shaders.rs (line 799, and again at 1996 with u64):

let opt = |name: &str| args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).and_then(|v| v.parse::<usize>().ok());

crates/xcl/src/disasm.rs (line 17) does it the other way:

parse_num(value).map(Some).ok_or_else(|| format!("invalid value for {name}: {value}"))

What it does to a user

  1. xcl rpf pkg.rpf --extract - the value is forgotten. opt() returns None, extract is None, so nothing is extracted; the record listing prints and the exit code is 0. The user concludes the record is not in the archive.
  2. xcl rpf pkg.rpf --fnid foo - an unknown option is not looked at at all, so there is no filter: every record is listed, exit 0.
  3. xcl shaders pack --hash 0xZZ (line 2026, u64::from_str_radix(...).ok()) - an unparseable hash becomes None, which means no filter: the whole corpus is translated instead of the one record. With the defaults the translator runs over the sample; with --sample 0 that is a full sweep that takes hours and looks like it is working.
  4. --top abc, --threads abc, --timeout abc - all silently take the default.

Nothing here is a crash; the problem is that the tool answers a different question than the one asked, quietly. For --hash the wrong answer is expensive.

Suggested fix

One helper in main.rs (or a small cli.rs), used by both subcommands, modelled on disasm::number_option:

fn number(args: &[String], name: &str) -> Result<Option<usize>, String> {
    let Some(index) = args.iter().position(|arg| arg == name) else { return Ok(None) };
    let value = args.get(index + 1).ok_or_else(|| format!("{name} requires a value"))?;
    value.parse::<usize>().map(Some).ok_or_else(|| format!("invalid value for {name}: {value}"))
}

returning 2 with the message on stderr, plus a pass over args that rejects an entry starting with - that no subcommand consumed ("unknown option: --fnid; see xcl --help"). The Option-returning local closures then become number(args, "--top")?.

Not #2216

#2216 is the usage text not listing --precompile / --pipelines, which is the opposite direction: documented flags missing from --help, not flags that are accepted and ignored.

Not tested

  • Nothing run: read from 1a0bbe3 without a checkout. Each case above is a direct reading of the closure and of the opt() call sites.

AI use

  • Claude (Opus 5.5) read crates/xcl/src/rpf.rs, crates/xcl/src/shaders.rs, crates/xcl/src/disasm.rs and crates/xcl/src/main.rs on 1a0bbe3 and wrote this report; the human account checked the call sites before opening it.
**@⁠itsonewi** on GitHub · 2026-11-25 11:53 UTC · [original](https://github.com/xcl-dev/xcl/issues/2266) ### Summary `xcl rpf` and `xcl shaders` read their options by position and drop whatever they do not recognise, so a typo or a forgotten value changes what the command does and the command still exits 0. `xcl disasm` is the one that reports it, so the fix has a precedent right next to the bug. `crates/xcl/src/rpf.rs` (`1a0bbe3`, line 100): ```rust let opt = |n: &str| args.iter().position(|a| a == n).and_then(|i| args.get(i + 1)).cloned(); let top: usize = opt("--top").and_then(|v| v.parse().ok()).unwrap_or(30); ``` `crates/xcl/src/shaders.rs` (line 799, and again at 1996 with `u64`): ```rust let opt = |name: &str| args.iter().position(|a| a == name).and_then(|i| args.get(i + 1)).and_then(|v| v.parse::<usize>().ok()); ``` `crates/xcl/src/disasm.rs` (line 17) does it the other way: ```rust parse_num(value).map(Some).ok_or_else(|| format!("invalid value for {name}: {value}")) ``` ### What it does to a user 1. `xcl rpf pkg.rpf --extract` - the value is forgotten. `opt()` returns `None`, `extract` is `None`, so nothing is extracted; the record listing prints and the exit code is 0. The user concludes the record is not in the archive. 2. `xcl rpf pkg.rpf --fnid foo` - an unknown option is not looked at at all, so there is no filter: every record is listed, exit 0. 3. `xcl shaders pack --hash 0xZZ` (line 2026, `u64::from_str_radix(...).ok()`) - an unparseable hash becomes `None`, which means *no filter*: the whole corpus is translated instead of the one record. With the defaults the translator runs over the sample; with `--sample 0` that is a full sweep that takes hours and looks like it is working. 4. `--top abc`, `--threads abc`, `--timeout abc` - all silently take the default. Nothing here is a crash; the problem is that the tool answers a different question than the one asked, quietly. For `--hash` the wrong answer is expensive. ### Suggested fix One helper in `main.rs` (or a small `cli.rs`), used by both subcommands, modelled on `disasm::number_option`: ```rust fn number(args: &[String], name: &str) -> Result<Option<usize>, String> { let Some(index) = args.iter().position(|arg| arg == name) else { return Ok(None) }; let value = args.get(index + 1).ok_or_else(|| format!("{name} requires a value"))?; value.parse::<usize>().map(Some).ok_or_else(|| format!("invalid value for {name}: {value}")) } ``` returning `2` with the message on stderr, plus a pass over `args` that rejects an entry starting with `-` that no subcommand consumed ("unknown option: --fnid; see `xcl --help`"). The `Option`-returning local closures then become `number(args, "--top")?`. ### Not #2216 #2216 is the usage text not listing `--precompile` / `--pipelines`, which is the opposite direction: documented flags missing from `--help`, not flags that are accepted and ignored. **Not tested** - Nothing run: read from `1a0bbe3` without a checkout. Each case above is a direct reading of the closure and of the `opt()` call sites. **AI use** - Claude (Opus 5.5) read `crates/xcl/src/rpf.rs`, `crates/xcl/src/shaders.rs`, `crates/xcl/src/disasm.rs` and `crates/xcl/src/main.rs` on `1a0bbe3` and wrote this report; the human account checked the call sites before opening it.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:25:00 +00:00
Author
Owner

@⁠itsonewi on GitHub · 2026-11-25 12:01 UTC · original

I checked this against the open PRs that touch the same surface, so it does not duplicate one:

  • #2193 and #2075 (both crates/xcl/tests/rpf_e2e.rs) pin xcl rpf with no arguments, a missing archive, a non-RPF9 file and an empty TOC. None of them passes a known option with a missing or unparseable value, and none passes an unknown option - which is what happens here. Whoever fixes this should add those two cases to that file rather than a new one, so the rpf CLI tests stay in one place.
  • #919 pins the exit-code convention, and it matters for the fix: 2 for a usage or input error, 3 for a file that is not a PE, and an unknown command prints the usage to stdout with 2. xcl rpf and xcl shaders already print their own diagnostics to stderr with 2 (e.g. xcl rpf: --probe cannot be combined with --find), so the new messages should follow that and not copy the top-level usage block.
**@⁠itsonewi** on GitHub · 2026-11-25 12:01 UTC · [original](https://github.com/xcl-dev/xcl/issues/2266#issuecomment-7161810143) I checked this against the open PRs that touch the same surface, so it does not duplicate one: - **#2193** and **#2075** (both `crates/xcl/tests/rpf_e2e.rs`) pin `xcl rpf` with *no arguments*, a missing archive, a non-RPF9 file and an empty TOC. None of them passes a known option with a missing or unparseable value, and none passes an unknown option - which is what happens here. Whoever fixes this should add those two cases to that file rather than a new one, so the rpf CLI tests stay in one place. - **#919** pins the exit-code convention, and it matters for the fix: `2` for a usage or input error, `3` for a file that is not a PE, and an *unknown command* prints the usage to **stdout** with `2`. `xcl rpf` and `xcl shaders` already print their own diagnostics to **stderr** with `2` (e.g. `xcl rpf: --probe cannot be combined with --find`), so the new messages should follow that and not copy the top-level usage block.
Author
Owner

@⁠itsonewi on GitHub · 2026-11-25 15:51 UTC · original

Both #2285 and #2303 fix this, and they now conflict with each other: each creates crates/xcl/src/cli.rs from scratch with a different API (a Parser struct vs. free functions text / number / hex_u64 / unknown), so whichever merges second needs a rebase, and merging both would leave two parsers doing the same job.

Against what this issue asks for (stderr + exit 2, nothing silently defaulted), both are complete:

#2285 (GustavoEllwanger) #2303 (b3IKK)
usage errors eprintln! + return 2 eprintln! + return 2
missing value, or another --flag in the value slot rejected rejected
unknown option, stray positional rejected rejected
--hash 1-16 hex digits, 0x optional u64::from_str_radix, 0x optional
numeric options (--top, --threads, ...) parse::<usize> decimal or 0x
tests unit tests + tests/rpf_e2e.rs and tests/shaders_e2e.rs covering the cases reported here unit tests in cli.rs
changed lines / files ~599 / 6 ~342 / 4

Two factual differences, for whoever picks one; neither is a reason to reject either PR:

  • Shape of the fix. #2285 replaces the lookups with one single-pass Parser, so a value cannot be read twice and the option set is checked in one place. #2303 keeps the per-option args.iter().position(..) lookups and puts strictness around them - a much smaller diff, but the "read the same option twice" shape stays.
  • unknown() in #2303 scans values too. args.iter().find(|a| a.starts_with('-') && !known.contains(a)) walks every argument, values included, so a value that begins with a single dash is reported as an unknown option; the unit test above it comments "values are not looked at", which the implementation does not match. Values beginning with -- are caught earlier by text(), so this only bites on single-dash values. Minor, and it is the one place where the two behaviours really differ.

Worth merging one and closing the other so the second author's work is not lost to a rebase.


AI use: read both patches, the current main of main.rs / log.rs, and CONTRIBUTING.md through the GitHub API to compare the two fixes; no code was written by an agent here and no build was run.

**@⁠itsonewi** on GitHub · 2026-11-25 15:51 UTC · [original](https://github.com/xcl-dev/xcl/issues/2266#issuecomment-7167379654) Both #2285 and #2303 fix this, and they now conflict with each other: each creates `crates/xcl/src/cli.rs` from scratch with a different API (a `Parser` struct vs. free functions `text` / `number` / `hex_u64` / `unknown`), so whichever merges second needs a rebase, and merging both would leave two parsers doing the same job. Against what this issue asks for (stderr + exit 2, nothing silently defaulted), both are complete: | | #2285 (GustavoEllwanger) | #2303 (b3IKK) | |---|---|---| | usage errors | `eprintln!` + `return 2` | `eprintln!` + `return 2` | | missing value, or another `--flag` in the value slot | rejected | rejected | | unknown option, stray positional | rejected | rejected | | `--hash` | 1-16 hex digits, `0x` optional | `u64::from_str_radix`, `0x` optional | | numeric options (`--top`, `--threads`, ...) | `parse::<usize>` | decimal or `0x` | | tests | unit tests + `tests/rpf_e2e.rs` and `tests/shaders_e2e.rs` covering the cases reported here | unit tests in `cli.rs` | | changed lines / files | ~599 / 6 | ~342 / 4 | Two factual differences, for whoever picks one; neither is a reason to reject either PR: - **Shape of the fix.** #2285 replaces the lookups with one single-pass `Parser`, so a value cannot be read twice and the option set is checked in one place. #2303 keeps the per-option `args.iter().position(..)` lookups and puts strictness around them - a much smaller diff, but the "read the same option twice" shape stays. - **`unknown()` in #2303 scans values too.** `args.iter().find(|a| a.starts_with('-') && !known.contains(a))` walks every argument, values included, so a value that begins with a single dash is reported as an unknown option; the unit test above it comments "values are not looked at", which the implementation does not match. Values beginning with `--` are caught earlier by `text()`, so this only bites on single-dash values. Minor, and it is the one place where the two behaviours really differ. Worth merging one and closing the other so the second author's work is not lost to a rebase. --- **AI use:** read both patches, the current `main` of `main.rs` / `log.rs`, and `CONTRIBUTING.md` through the GitHub API to compare the two fixes; no code was written by an agent here and no build was run.
Author
Owner

@⁠itsonewi on GitHub · 2026-11-25 16:01 UTC · original

Correction to my note above, and the part I got wrong: I compared the two patches without reading their conversations, which was incomplete.

#2303 had already been through an independent retest by nnonno91 of eight CLI cases (--threads abc, --timeout abc, --only, --max-bytes x, --direct --threads abc, ... on an empty folder and on a nonexistent input), and b3IKK fixed four real holes it found at 8d1544f. #2303 is now closed, so those eight verified cases should be re-run against #2285's head before it merges rather than disappearing with the branch. GustavoEllwanger's own comparison on #2285 (posted at 15:49Z, before mine) also lists two behaviours my table did not: #2285 rejects stray positional arguments, which an unknown() that only looks at --prefixed arguments cannot catch, and validates --split as 0 or 1 instead of accepting any non-zero integer as true. Together that is a superset of what #2303 was checked against, so the closure looks right; the point is only that the eight cases should move with it.

The table above stands as far as the patches go. The missing input was the review history, not the code.


AI use: read both PR threads, the patches and the current main through the GitHub API; no code was written by an agent here and no build was run.

**@⁠itsonewi** on GitHub · 2026-11-25 16:01 UTC · [original](https://github.com/xcl-dev/xcl/issues/2266#issuecomment-7167530876) Correction to my note above, and the part I got wrong: I compared the two patches without reading their conversations, which was incomplete. #2303 had already been through an independent retest by nnonno91 of eight CLI cases (`--threads abc`, `--timeout abc`, `--only`, `--max-bytes x`, `--direct --threads abc`, ... on an empty folder and on a nonexistent input), and b3IKK fixed four real holes it found at `8d1544f`. #2303 is now closed, so those eight verified cases should be re-run against #2285's head before it merges rather than disappearing with the branch. GustavoEllwanger's own comparison on #2285 (posted at 15:49Z, before mine) also lists two behaviours my table did not: #2285 rejects stray positional arguments, which an `unknown()` that only looks at `-`-prefixed arguments cannot catch, and validates `--split` as `0` or `1` instead of accepting any non-zero integer as true. Together that is a superset of what #2303 was checked against, so the closure looks right; the point is only that the eight cases should move with it. The table above stands as far as the patches go. The missing input was the review history, not the code. --- **AI use:** read both PR threads, the patches and the current `main` through the GitHub API; no code was written by an agent here and no build was run.
This discussion has been locked. Commenting is limited to contributors.
No description provided.