From 73f6d28fec736594886e9176444133ba3d31c3a9 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 17:29:20 -0600 Subject: [PATCH 1/2] fix(id/repl): reject nonnumeric result limits --- pkgs/id/src/repl/runner.rs | 93 +++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index 957cf6625c..b3413174c0 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -614,9 +614,8 @@ impl FindArgs<'_> { 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.) + // #3059: an explicit zero limit is an error, never a silent empty result. + // Nonnumeric option values are rejected by the argument parsers. if self.first == Some(0) { anyhow::bail!("--first must be at least 1 (got 0)"); } @@ -709,24 +708,24 @@ fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> Result() { - args.first = Some(n); - i += 1; - } else { - args.first = Some(1); // default to 1 - } + let value = rest[i + 1]; + args.first = + Some(value.parse::().map_err(|_| { + anyhow::anyhow!("--first requires an integer (got {value:?})") + })?); + i += 1; } else { args.first = Some(1); // default to 1 } } else if arg == "--last" { // --last with optional number argument if i + 1 < rest.len() && !rest[i + 1].starts_with('-') { - if let Ok(n) = rest[i + 1].parse::() { - args.last = Some(n); - i += 1; - } else { - args.last = Some(1); // default to 1 - } + let value = rest[i + 1]; + args.last = + Some(value.parse::().map_err(|_| { + anyhow::anyhow!("--last requires an integer (got {value:?})") + })?); + i += 1; } else { args.last = Some(1); // default to 1 } @@ -1263,23 +1262,23 @@ fn parse_peek_args<'a>(rest: &[&'a str]) -> Result<(FindArgs<'a>, PeekOptions)> } } else if arg == "--first" { if i + 1 < rest.len() && !rest[i + 1].starts_with('-') { - if let Ok(n) = rest[i + 1].parse::() { - find_args.first = Some(n); - i += 1; - } else { - find_args.first = Some(1); - } + let value = rest[i + 1]; + find_args.first = + Some(value.parse::().map_err(|_| { + anyhow::anyhow!("--first requires an integer (got {value:?})") + })?); + i += 1; } else { find_args.first = Some(1); } } else if arg == "--last" { if i + 1 < rest.len() && !rest[i + 1].starts_with('-') { - if let Ok(n) = rest[i + 1].parse::() { - find_args.last = Some(n); - i += 1; - } else { - find_args.last = Some(1); - } + let value = rest[i + 1]; + find_args.last = + Some(value.parse::().map_err(|_| { + anyhow::anyhow!("--last requires an integer (got {value:?})") + })?); + i += 1; } else { find_args.last = Some(1); } @@ -1289,9 +1288,11 @@ fn parse_peek_args<'a>(rest: &[&'a str]) -> Result<(FindArgs<'a>, PeekOptions)> i += 1; } } else if arg == "--lines" || arg == "-n" { - if i + 1 < rest.len() - && let Ok(n) = rest[i + 1].parse::() - { + if i + 1 < rest.len() && !rest[i + 1].starts_with('-') { + let value = rest[i + 1]; + let n = value + .parse::() + .map_err(|_| anyhow::anyhow!("--lines requires an integer (got {value:?})"))?; peek_opts.lines = n; i += 1; } @@ -1611,16 +1612,36 @@ mod tests { parse_find_args(&["q", "--last"], "tag").unwrap().last, Some(1) ); - assert_eq!( - parse_find_args(&["q", "--first", "abc"], "tag") - .unwrap() - .first, - Some(1) - ); + assert_eq!(parse_peek_args(&["q", "--first"]).unwrap().0.first, Some(1)); + assert_eq!(parse_peek_args(&["q", "--last"]).unwrap().0.last, Some(1)); // Peek without --lines keeps the default of 5. assert_eq!(parse_peek_args(&["q"]).unwrap().1.lines, 5); } + #[test] + fn test_parse_rejects_nonnumeric_result_limit_values() { + for (flag, value) in [("--first", "two"), ("--last", "abc")] { + let find_error = parse_find_args(&["q", flag, value], "tag") + .err() + .expect("find parser should reject a nonnumeric limit"); + assert!(find_error.to_string().contains(flag)); + assert!(find_error.to_string().contains(value)); + + let peek_error = parse_peek_args(&["q", flag, value]) + .err() + .expect("peek parser should reject a nonnumeric limit"); + assert!(peek_error.to_string().contains(flag)); + assert!(peek_error.to_string().contains(value)); + } + + for flag in ["--lines", "-n"] { + let error = parse_peek_args(&["q", flag, "abc"]) + .err() + .expect("peek parser should reject a nonnumeric line count"); + assert!(error.to_string().contains("abc")); + } + } + fn test_parse_find_args_basic() { let rest = vec!["query1", "query2"]; let args = parse_find_args(&rest, "union").unwrap(); From 5095af5fd2c934fd0f32d1af03af57f96e4cd7fd Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 20:27:04 -0600 Subject: [PATCH 2/2] fix(id/repl): keep output redirection after bare --first/--last (#2009) A >file token following a bare --first or --last was consumed as the optional numeric operand and failed integer parsing, breaking find/search q --first >out.txt (Codex P2 on PR #4072). Redirection tokens are now excluded from numeric-operand detection in parse_find_args; the existing output branch handles them and the limit keeps its default of 1. Malformed numeric values still error. Regression tests cover both flags. --- pkgs/id/src/repl/runner.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index b3413174c0..5ccd48deae 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -706,8 +706,14 @@ fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> Resultfile` + // redirection token is not the number (it is handled by + // the output branch below), so `--first >out.txt` keeps + // the default limit of 1. + if i + 1 < rest.len() + && !rest[i + 1].starts_with('-') + && !rest[i + 1].starts_with('>') + { let value = rest[i + 1]; args.first = Some(value.parse::().map_err(|_| { @@ -718,8 +724,12 @@ fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> Result') + { let value = rest[i + 1]; args.last = Some(value.parse::().map_err(|_| { @@ -1752,6 +1762,24 @@ mod tests { assert!(parse_find_args(&["query", "--last", "3"], "union").is_ok()); } + #[test] + fn test_parse_find_args_first_before_redirection_keeps_default() { + // `--first >out.txt`: the redirection token is the output + // option, not the optional number (Codex P2 on PR #4072). + let args = parse_find_args(&["query", "--first", ">out.txt"], "union").unwrap(); + assert_eq!(args.first, Some(1)); + assert_eq!(args.output_file, Some("out.txt")); + assert!(args.to_file); + } + + #[test] + fn test_parse_find_args_last_before_redirection_keeps_default() { + let args = parse_find_args(&["query", "--last", ">out.txt"], "union").unwrap(); + assert_eq!(args.last, Some(1)); + assert_eq!(args.output_file, Some("out.txt")); + assert!(args.to_file); + } + #[test] fn test_parse_find_args_count() { let rest = vec!["query", "--count"];