Skip to content

Commit 3efa381

Browse files
committed
fix: guard against UTF-8 boundary panic in multi-word mention extractor
The two-pass extractor sliced after_at by byte offset (known.len()) which can land mid-character when content contains multi-byte UTF-8 (CJK, emoji). Replace panicking &after_at[..n] with after_at.get(..n) which returns None on invalid boundaries, gracefully skipping the candidate. Also fix clippy: sort_by → sort_by_key(Reverse) for the longest-first sort. Add three Unicode-specific tests to prevent regression.
1 parent 7615656 commit 3efa381

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

‎crates/sprout-sdk/src/mentions.rs‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn extract_at_mentions_with_known(content: &str, known_names: &[&str]) -> Ve
131131
.copied()
132132
.filter(|n| !n.trim().is_empty())
133133
.collect();
134-
sorted_known.sort_by(|a, b| b.len().cmp(&a.len()));
134+
sorted_known.sort_by_key(|k| std::cmp::Reverse(k.len()));
135135

136136
let mut names: Vec<String> = Vec::new();
137137
let mut seen = HashSet::new();
@@ -152,7 +152,14 @@ pub fn extract_at_mentions_with_known(content: &str, known_names: &[&str]) -> Ve
152152
if after_at.len() < known.len() {
153153
continue;
154154
}
155-
let candidate = &after_at[..known.len()];
155+
// Use get() to safely handle byte boundaries — known.len()
156+
// may land mid-character when content contains multi-byte
157+
// UTF-8 (e.g. CJK, emoji). If the slice isn't on a char
158+
// boundary, skip this candidate.
159+
let candidate = match after_at.get(..known.len()) {
160+
Some(s) => s,
161+
None => continue,
162+
};
156163
if !candidate.eq_ignore_ascii_case(known) {
157164
continue;
158165
}
@@ -426,6 +433,32 @@ mod tests {
426433
assert_eq!(result, vec!["alice"]);
427434
}
428435

436+
#[test]
437+
fn unicode_content_does_not_panic() {
438+
// Known name byte-length may land mid-character in multi-byte content.
439+
// e.g. known "ab" (2 bytes) vs content starting with 日 (3 bytes) —
440+
// byte offset 2 is not a char boundary. Must not panic; gracefully
441+
// skips the candidate via get() returning None.
442+
let result = extract_at_mentions_with_known("@日本語 hello", &["ab"]);
443+
// "ab" doesn't match — falls through to single-word fallback which
444+
// stops at non-ASCII, so no match. The key assertion: no panic.
445+
assert!(result.is_empty());
446+
}
447+
448+
#[test]
449+
fn unicode_known_name_matches_with_boundary() {
450+
// Multi-byte known name followed by a space (valid boundary).
451+
let result = extract_at_mentions_with_known("@日本 hello", &["日本"]);
452+
assert_eq!(result, vec!["日本"]);
453+
}
454+
455+
#[test]
456+
fn unicode_known_name_with_ascii_content_no_panic() {
457+
// Reverse case: multi-byte known name against ASCII content.
458+
let result = extract_at_mentions_with_known("@alice hello", &["日本語"]);
459+
assert_eq!(result, vec!["alice"]);
460+
}
461+
429462
// ── match_names_to_profiles ─────────────────────────────────────────
430463

431464
fn profile<'a>(pk: &'a str, json: &'a str) -> MentionProfile<'a> {

0 commit comments

Comments
 (0)