diff --git a/changelog.d/11560-config-path-list-linear-duplicates.md b/changelog.d/11560-config-path-list-linear-duplicates.md new file mode 100644 index 00000000000..eb755803df2 --- /dev/null +++ b/changelog.d/11560-config-path-list-linear-duplicates.md @@ -0,0 +1,5 @@ + +- A `ripr.toml` listing tens of thousands of `generated_file_patterns`, + `handwritten_files` or `profiles.bun_ub.test_roots` entries no longer stalls + the run: duplicate detection is linear instead of quadratic, with the same + error wording and ordering (#11560). diff --git a/crates/ripr/src/config.rs b/crates/ripr/src/config.rs index 04ec40a490c..dbf2190b23a 100644 --- a/crates/ripr/src/config.rs +++ b/crates/ripr/src/config.rs @@ -1041,6 +1041,9 @@ fn parse_languages_enabled(values: &[String]) -> Result, String> fn parse_generated_file_patterns(values: &[String]) -> Result, String> { let mut parsed = Vec::with_capacity(values.len()); + // Set membership keeps duplicate detection linear: a hostile `ripr.toml` + // may list tens of thousands of entries (#11560). + let mut seen = std::collections::HashSet::with_capacity(values.len()); for value in values { if value.chars().any(char::is_control) { return Err( @@ -1056,7 +1059,7 @@ fn parse_generated_file_patterns(values: &[String]) -> Result, Strin .to_string(), ); } - if parsed.iter().any(|existing| existing == trimmed) { + if !seen.insert(trimmed) { return Err(format!( "languages.rust.generated_file_patterns lists `{trimmed}` more than once; remove the duplicate" )); @@ -1073,6 +1076,7 @@ fn parse_handwritten_files( extension_ok: impl Fn(&str) -> bool, ) -> Result, String> { let mut parsed = Vec::with_capacity(values.len()); + let mut seen = std::collections::HashSet::with_capacity(values.len()); for value in values { if value.chars().any(char::is_control) { return Err(format!("{field} must not contain control characters")); @@ -1091,7 +1095,7 @@ fn parse_handwritten_files( return Err(format!("{field} must identify {extension_desc}")); } let normalized = path.to_string_lossy().replace('\\', "/"); - if parsed.contains(&normalized) { + if !seen.insert(normalized.clone()) { return Err(format!( "{field} lists `{normalized}` more than once; remove the duplicate" )); @@ -1116,10 +1120,11 @@ fn parse_bun_ub_profile(raw: RawBunUbProfileConfig) -> Result Result<(), Stri let _ = fs::remove_dir_all(&root); Ok(()) } + +/// #11560: a hostile `ripr.toml` may list tens of thousands of entries. The +/// duplicate check must stay linear, and still name the first duplicate. +#[test] +fn large_path_lists_parse_in_linear_time_and_still_reject_duplicates() -> Result<(), String> { + let render = |key: &str, table: &str, entries: &[String]| { + let items = entries + .iter() + .map(|entry| format!("\"{entry}\"")) + .collect::>() + .join(","); + format!("[{table}]\n{key} = [{items}]\n") + }; + let patterns: Vec = (0..60_000).map(|i| format!("gen{i}/**/*.rs")).collect(); + let files: Vec = (0..60_000).map(|i| format!("src/f{i}.rs")).collect(); + let roots: Vec = (0..60_000).map(|i| format!("tests/t{i}")).collect(); + let started = std::time::Instant::now(); + let config = parse_config(&render( + "generated_file_patterns", + "languages.rust", + &patterns, + ))?; + assert_eq!( + config.languages().rust.generated_file_patterns.len(), + 60_000 + ); + let config = parse_config(&render("handwritten_files", "languages.rust", &files))?; + assert_eq!(config.languages().rust.handwritten_files.len(), 60_000); + let bun = format!( + "{}bridge_hints = \"hints.json\"\n", + render("test_roots", "profiles.bun_ub", &roots) + ); + let config = parse_config(&bun)?; + assert_eq!( + config + .profiles() + .bun_ub + .as_ref() + .map(|p| p.test_roots.len()), + Some(60_000) + ); + assert!( + started.elapsed() < std::time::Duration::from_secs(10), + "60k-entry path lists must parse in linear time, took {:?}", + started.elapsed() + ); + + let mut duplicated = patterns.clone(); + duplicated.push("gen17/**/*.rs".to_string()); + let error = parse_config(&render( + "generated_file_patterns", + "languages.rust", + &duplicated, + )) + .err() + .ok_or("duplicate pattern must be rejected")?; + assert!(error.contains("`gen17/**/*.rs` more than once"), "{error}"); + Ok(()) +}