From e6fc43782306b996ce76d5e2340224966f9b25c2 Mon Sep 17 00:00:00 2001 From: "Peter A. Bigot" Date: Mon, 5 Sep 2022 12:53:37 -0700 Subject: [PATCH] Fix topic matches with single-level wildcard The check would incorrectly match when the topic had more levels than a pattern that didn't end in a wildcard. Signed-off-by: Peter A. Bigot --- src/topic.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/topic.rs b/src/topic.rs index 2ee586c..29d6c82 100644 --- a/src/topic.rs +++ b/src/topic.rs @@ -469,15 +469,17 @@ impl TopicFilter { false } else { + let mut saw_wc = false; for i in 0..n { if fields[i] == "#" { + saw_wc = true; break; } if fields[i] != "+" && fields[i] != top_fields[i] { return false; } } - true + saw_wc || n == top_fields.len() } } } @@ -526,8 +528,14 @@ mod tests { const FILTER2: &str = "some/+/thing"; let filter = TopicFilter::new(FILTER2).unwrap(); assert!(filter.is_match("some/topic/thing")); + assert!(!filter.is_match("some/topic/plus/thing")); let s = format!("{}", filter); assert_eq!(s, FILTER2); + + const FILTER3: &str = "some/+"; + let filter = TopicFilter::new(FILTER3).unwrap(); + assert!(filter.is_match("some/thing")); + assert!(!filter.is_match("some/thing/plus")); } }