Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

fix(repo): bound wildcard filtering work

Rudy Celekli55d79bd00ce5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d864085..1b099a49 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,10 @@ All notable changes to Open Ontologies are documented here.
## [Unreleased]
+### Fixed
+
+- Bound wildcard repository filtering to pattern/filename pairs so repeated stars cannot stall `onto_repo_list`.
+
### Added
- **A named profile hands a client one job's worth of the tool surface** (#263).
diff --git a/src/repo.rs b/src/repo.rs
index 9c39b0ab..2fc344d9 100644
--- a/src/repo.rs
+++ b/src/repo.rs
@@ -49,25 +49,26 @@ pub struct RepoEntry {
/// (no path separators). This is intentionally tiny so we don't pull in a
/// glob crate just for filtering tool output.
pub fn glob_match(pattern: &str, name: &str) -> bool {
- fn helper(p: &[u8], n: &[u8]) -> bool {
- match (p.first(), n.first()) {
- (None, None) => true,
- (Some(b'*'), _) => {
- // Try consuming zero or more characters from `n`.
- if helper(&p[1..], n) {
- return true;
- }
- if !n.is_empty() && helper(p, &n[1..]) {
- return true;
- }
- false
+ // Keep the existing byte and ASCII-case matching contract, but visit each
+ // pattern/name pair once. Recursive `*` expansion revisits the same suffixes
+ // exponentially and can stall onto_repo_list on a single ordinary filename.
+ let name = name.as_bytes();
+ let mut matched = vec![false; name.len() + 1];
+ matched[0] = true;
+ for pc in pattern.as_bytes() {
+ if *pc == b'*' {
+ for i in 1..matched.len() {
+ matched[i] = matched[i] || matched[i - 1];
+ }
+ } else {
+ for i in (1..matched.len()).rev() {
+ matched[i] =
+ matched[i - 1] && (*pc == b'?' || pc.eq_ignore_ascii_case(&name[i - 1]));
}
- (Some(b'?'), Some(_)) => helper(&p[1..], &n[1..]),
- (Some(pc), Some(nc)) if pc.eq_ignore_ascii_case(nc) => helper(&p[1..], &n[1..]),
- _ => false,
+ matched[0] = false;
}
}
- helper(pattern.as_bytes(), name.as_bytes())
+ matched[name.len()]
}
/// Normalize `dir` to an absolute path that lies inside one of the configured
diff --git a/tests/repo_glob_bound_test.rs b/tests/repo_glob_bound_test.rs
new file mode 100644
index 00000000..a3ac827e
--- /dev/null
+++ b/tests/repo_glob_bound_test.rs
@@ -0,0 +1,67 @@
+//! Supported repeated wildcards must not stall repository listing.
+use open_ontologies::repo::glob_match;
+
+fn reference(pattern: &[u8], name: &[u8]) -> bool {
+ match (pattern.first(), name.first()) {
+ (None, None) => true,
+ (Some(b'*'), _) => {
+ reference(&pattern[1..], name) || (!name.is_empty() && reference(pattern, &name[1..]))
+ }
+ (Some(b'?'), Some(_)) => reference(&pattern[1..], &name[1..]),
+ (Some(pc), Some(nc)) if pc.eq_ignore_ascii_case(nc) => reference(&pattern[1..], &name[1..]),
+ _ => false,
+ }
+}
+
+fn words(alphabet: &[u8], maximum: usize) -> Vec<String> {
+ let mut all = vec![String::new()];
+ let mut level = vec![String::new()];
+ for _ in 0..maximum {
+ level = level
+ .into_iter()
+ .flat_map(|prefix| {
+ alphabet.iter().map(move |c| {
+ let mut word = prefix.clone();
+ word.push(char::from(*c));
+ word
+ })
+ })
+ .collect();
+ all.extend(level.iter().cloned());
+ }
+ all
+}
+
+#[test]
+fn bounded_matcher_keeps_existing_wildcard_and_case_behavior() {
+ for pattern in words(b"ab*?", 5) {
+ for name in words(b"aB", 4) {
+ assert_eq!(
+ glob_match(&pattern, &name),
+ reference(pattern.as_bytes(), name.as_bytes()),
+ "pattern {pattern:?}, name {name:?}"
+ );
+ }
+ }
+ assert!(glob_match("*.TTL", "ontology.ttl"));
+ assert!(glob_match("schema?.ttl", "schema1.ttl"));
+ assert!(!glob_match("schema?.ttl", "schema12.ttl"));
+ // Preserve the existing byte contract rather than adding Unicode semantics.
+ assert!(!glob_match("?.ttl", "é.ttl"));
+ assert!(glob_match("??.ttl", "é.ttl"));
+}
+
+#[test]
+fn a_short_repeated_star_pattern_finishes_on_a_short_filename() {
+ let pattern = format!("{}b.ttl", "*a".repeat(24));
+ let name = format!("{}.ttl", "a".repeat(32));
+ let (sender, receiver) = std::sync::mpsc::channel();
+ let worker = std::thread::spawn(move || sender.send(glob_match(&pattern, &name)).unwrap());
+ let result = receiver
+ .recv_timeout(std::time::Duration::from_secs(2))
+ .expect(
+ "repository filtering exceeded two seconds for a 53-byte pattern and 36-byte filename",
+ );
+ assert!(!result);
+ worker.join().unwrap();
+}