Skip to content
Merged
Show file tree
Hide file tree
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
67 changes: 58 additions & 9 deletions pkgs/id/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize, String> {
match raw.parse::<usize>() {
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.
///
Expand Down Expand Up @@ -727,15 +739,17 @@ 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<usize>,
/// Return only the last N matches (default 1 if no number given).
#[arg(
long,
conflicts_with = "first",
num_args = 0..=1,
default_missing_value = "1"
default_missing_value = "1",
value_parser = parse_result_limit
)]
last: Option<usize>,
/// Exclude matches where name or hash contains this pattern (repeatable).
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -818,15 +832,17 @@ 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<usize>,
/// Return only the last N matches (default 1 if no number given).
#[arg(
long,
conflicts_with = "first",
num_args = 0..=1,
default_missing_value = "1"
default_missing_value = "1",
value_parser = parse_result_limit
)]
last: Option<usize>,
/// Exclude matches where name or hash contains this pattern (repeatable).
Expand Down Expand Up @@ -914,15 +930,17 @@ 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<usize>,
/// Return only the last N matches (default 1 if no number given).
#[arg(
long,
conflicts_with = "first",
num_args = 0..=1,
default_missing_value = "1"
default_missing_value = "1",
value_parser = parse_result_limit
)]
last: Option<usize>,
/// Print count of matches instead of the matches themselves.
Expand Down Expand Up @@ -991,15 +1009,17 @@ 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<usize>,
/// Return only the last N matches (default 1 if no number given).
#[arg(
long,
conflicts_with = "first",
num_args = 0..=1,
default_missing_value = "1"
default_missing_value = "1",
value_parser = parse_result_limit
)]
last: Option<usize>,
/// Print count of matches instead of the matches themselves.
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 41 additions & 1 deletion pkgs/id/src/repl/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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();
Expand Down