diff --git a/pkgs/id/src/cli.rs b/pkgs/id/src/cli.rs index 59bbcf5e6a..c897da74e5 100644 --- a/pkgs/id/src/cli.rs +++ b/pkgs/id/src/cli.rs @@ -197,6 +197,18 @@ pub struct Cli { /// Commands are organized by their primary function: storage, retrieval, /// search, or system operations. #[derive(Subcommand, Debug)] +/// Parses a result limit (`--first` / `--last` / peek `--lines`): zero is +/// rejected with a clear error instead of silently producing an empty result +/// or a header-only preview (#3059). Clap names the flag in its error prefix, +/// so the message stays flag-specific at the boundary. +fn parse_result_limit(raw: &str) -> Result { + match raw.parse::() { + Ok(0) => Err("zero is not a valid limit: use at least 1".to_owned()), + Ok(n) => Ok(n), + Err(err) => Err(err.to_string()), + } +} + pub enum Command { /// Start a server that accepts put/get requests from peers. /// @@ -727,7 +739,8 @@ pub enum Command { long, conflicts_with = "last", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] first: Option, /// Return only the last N matches (default 1 if no number given). @@ -735,7 +748,8 @@ pub enum Command { long, conflicts_with = "first", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] last: Option, /// Exclude matches where name or hash contains this pattern (repeatable). @@ -790,7 +804,7 @@ pub enum Command { #[arg(long)] name: bool, /// Number of lines to show from head and tail (default: 5). - #[arg(short = 'n', long, default_value = "5")] + #[arg(short = 'n', long, default_value = "5", value_parser = parse_result_limit)] lines: usize, /// Show only head lines (no tail). #[arg(long, conflicts_with = "tail_only")] @@ -818,7 +832,8 @@ pub enum Command { long, conflicts_with = "last", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] first: Option, /// Return only the last N matches (default 1 if no number given). @@ -826,7 +841,8 @@ pub enum Command { long, conflicts_with = "first", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] last: Option, /// Exclude matches where name or hash contains this pattern (repeatable). @@ -914,7 +930,8 @@ pub enum Command { long, conflicts_with = "last", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] first: Option, /// Return only the last N matches (default 1 if no number given). @@ -922,7 +939,8 @@ pub enum Command { long, conflicts_with = "first", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] last: Option, /// Print count of matches instead of the matches themselves. @@ -991,7 +1009,8 @@ pub enum Command { long, conflicts_with = "last", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] first: Option, /// Return only the last N matches (default 1 if no number given). @@ -999,7 +1018,8 @@ pub enum Command { long, conflicts_with = "first", num_args = 0..=1, - default_missing_value = "1" + default_missing_value = "1", + value_parser = parse_result_limit )] last: Option, /// Print count of matches instead of the matches themselves. @@ -2923,6 +2943,35 @@ mod tests { } #[test] + #[test] + fn test_cli_rejects_zero_result_limits() { + std::thread::Builder::new() + .stack_size(8 * 1024 * 1024) + .spawn(|| { + // #3059: an explicit zero limit is an error at the CLI boundary, never a + // silent empty result (find/search/show/peek share the flag pair). + for cmd in ["find", "search", "show", "peek"] { + assert!( + Cli::try_parse_from(["id", cmd, "--first", "0", "query"]).is_err(), + "{cmd} --first 0 must be rejected" + ); + assert!( + Cli::try_parse_from(["id", cmd, "--last", "0", "query"]).is_err(), + "{cmd} --last 0 must be rejected" + ); + } + assert!(Cli::try_parse_from(["id", "peek", "--lines", "0", "query"]).is_err()); + assert!(Cli::try_parse_from(["id", "peek", "-n", "0", "query"]).is_err()); + // Bare flags keep their defaults (1 / 1 / 5 lines). + assert!(Cli::try_parse_from(["id", "find", "query", "--first"]).is_ok()); + assert!(Cli::try_parse_from(["id", "find", "query", "--last"]).is_ok()); + assert!(Cli::try_parse_from(["id", "peek", "query"]).is_ok()); + }) + .unwrap() + .join() + .unwrap(); + } + fn test_cli_rejects_first_and_last_together() { std::thread::Builder::new() .stack_size(8 * 1024 * 1024) diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index 07387c2d1e..7efc7a0297 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -602,11 +602,20 @@ struct FindArgs<'a> { } impl FindArgs<'_> { - /// Reject mutually exclusive result limits before any search work starts. + /// Reject mutually exclusive and zero result limits before any search work starts. fn validate_result_limits(&self) -> Result<()> { if self.first.is_some() && self.last.is_some() { anyhow::bail!("--first and --last cannot be used together"); } + // #3059: an explicit zero limit is an error, never a silent empty result. (A + // nonnumeric value keeps the #2009 behaviour: it falls back to the default of 1 + // in the parsers, so only a parsed 0 reaches this check.) + if self.first == Some(0) { + anyhow::bail!("--first must be at least 1 (got 0)"); + } + if self.last == Some(0) { + anyhow::bail!("--last must be at least 1 (got 0)"); + } Ok(()) } @@ -1296,6 +1305,10 @@ fn parse_peek_args<'a>(rest: &[&'a str]) -> Result<(FindArgs<'a>, PeekOptions)> } find_args.validate_result_limits()?; + // #3059: `--lines 0` / `-n 0` would render a header/elision-only preview. + if peek_opts.lines == 0 { + anyhow::bail!("--lines must be at least 1 (got 0)"); + } Ok((find_args, peek_opts)) } @@ -1549,6 +1562,33 @@ mod tests { } #[test] + #[test] + fn test_parse_rejects_zero_result_limits() { + // #3059: explicit zero limits are errors at the REPL boundary. + assert!(parse_find_args(&["q", "--first", "0"], "tag").is_err()); + assert!(parse_find_args(&["q", "--last", "0"], "tag").is_err()); + assert!(parse_peek_args(&["q", "--lines", "0"]).is_err()); + assert!(parse_peek_args(&["q", "-n", "0"]).is_err()); + assert!(parse_peek_args(&["q", "--first", "0"]).is_err()); + // Bare flags keep the default of 1; a nonnumeric value keeps the #2009 fallback. + assert_eq!( + parse_find_args(&["q", "--first"], "tag").unwrap().first, + Some(1) + ); + assert_eq!( + parse_find_args(&["q", "--last"], "tag").unwrap().last, + Some(1) + ); + assert_eq!( + parse_find_args(&["q", "--first", "abc"], "tag") + .unwrap() + .first, + Some(1) + ); + // Peek without --lines keeps the default of 5. + assert_eq!(parse_peek_args(&["q"]).unwrap().1.lines, 5); + } + fn test_parse_find_args_basic() { let rest = vec!["query1", "query2"]; let args = parse_find_args(&rest, "union").unwrap();