Bound malformed subtitle scans and escape mixed-case SubViewer breaks

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 17:08:39 -05:00
1 parent 29b64c868a
commit d795d23b31
3 files changed
+59 -15

No files matched your search

+25 -12
View File
@@ -67,21 +67,23 @@ fn split_controls(line: &str) -> (String, Vec<(char, String)>) {
while let Some(open) = rest.find('{') {
text.push_str(rest.get(..open).unwrap_or(""));
let after = rest.get(open + 1..).unwrap_or("");
let control = after.split_once('}').and_then(|(body, tail)| {
let (name, value) = body.split_once(':')?;
let mut chars = name.chars();
let c = chars.next().filter(|c| c.is_ascii_alphabetic())?;
chars.next().is_none().then(|| (c, value.to_string(), tail))
});
match control {
Some((c, value, tail)) => {
controls.push((c, value));
// Check the fixed-size control prefix before scanning its body. Invalid opening
// braces must not repeatedly scan the same suffix for a closing brace.
let name = after.as_bytes().first().copied().filter(u8::is_ascii_alphabetic);
if let Some(name) = name.filter(|_| after.as_bytes().get(1) == Some(&b':')) {
if let Some((body, tail)) = after.get(2..).and_then(|s| s.split_once('}')) {
controls.push((char::from(name), body.to_string()));
rest = tail;
}
None => {
} else {
// No later control can close either: preserve the entire remaining text.
text.push('{');
rest = after;
text.push_str(after);
rest = "";
break;
}
} else {
text.push('{');
rest = after;
}
}
text.push_str(rest);
@@ -207,6 +209,17 @@ mod tests {
assert_eq!(cue_text("{f:Arial}{s:20}a & b {x"), "a &amp; b {x");
}
#[test]
fn malformed_controls_inside_valid_cues_are_preserved() {
for raw in ["{".repeat(32_768), "{y:".repeat(16_384)] {
let doc = parse(&format!("{{0}}{{25}}{raw}")).unwrap();
assert_eq!(doc.cues.len(), 1);
assert_eq!(doc.cues[0].text, raw);
}
assert_eq!(cue_text("{{y:i}hello"), "<i>{hello</i>");
assert_eq!(cue_text("{xx:no}{y:b}hello"), "<b>{xx:no}hello</b>");
}
#[test]
fn rate_header() {
let d = parse("{1}{1}25.000\n{25}{50}x\n").unwrap();
+15 -2
View File
@@ -72,8 +72,12 @@ fn text_run(s: &str) -> String {
if let Some((_, len)) = crate::entity_at(rest) {
out.push_str(rest.get(..len).unwrap_or(""));
advance = len;
} else if let Some((body, _)) = rest.get(1..).and_then(|r| r.split_once(';')).filter(|(b, _)| b.len() <= 8)
&& let Some(ch) = numeric_reference(body)
} else if let Some(body) = rest.get(1..).and_then(|r| {
// Numeric entities have at most eight body bytes. Never scan the
// unbounded remainder again for each malformed ampersand.
let end = r.bytes().take(9).position(|b| b == b';')?;
r.get(..end)
}) && let Some(ch) = numeric_reference(body)
{
out.push_str(&escape_markup(&ch.to_string()));
advance = body.len() + 2;
@@ -241,6 +245,15 @@ mod tests {
assert_eq!(d.cues[1].end, last_cue_end(ms_to_tick(4000)));
}
#[test]
fn malformed_entities_inside_valid_cues_are_bounded() {
let raw = "&".repeat(32_768);
let doc = parse(&format!("<SAMI><SYNC Start=0><P>{raw}<SYNC Start=1000><P>&nbsp;")).unwrap();
assert_eq!(doc.cues.len(), 1);
assert_eq!(doc.cues[0].text, "&amp;".repeat(32_768));
assert_eq!(text_run("&#0000065; &#00000065; &#x41; &é;"), "A &amp;#00000065; A &amp;é;");
}
#[test]
fn only_the_first_class_is_read() {
let d = parse("<SAMI><BODY><SYNC Start=1000><P Class=ENCC>en<P Class=KRCC>kr<SYNC Start=2000><P Class=ENCC>&nbsp;</BODY></SAMI>").unwrap();
+19 -1
View File
@@ -91,7 +91,15 @@ pub fn write(doc: &Document) -> String {
}
let end = if clock(c.end) == clock(c.start) { Tick(c.end.0.saturating_add(crate::TICKS_PER_MS * 10)) } else { c.end };
out.push_str(&format!("{},{}\n", clock(c.start), clock(end)));
let lines: Vec<String> = lines.iter().map(|l| l.replace("[br]", "\u{ff3b}br\u{ff3d}").replace("[BR]", "\u{ff3b}BR\u{ff3d}")).collect();
let lines: Vec<String> = lines
.iter()
.map(|l| {
l.replace("[br]", "\u{ff3b}br\u{ff3d}")
.replace("[BR]", "\u{ff3b}BR\u{ff3d}")
.replace("[Br]", "\u{ff3b}Br\u{ff3d}")
.replace("[bR]", "\u{ff3b}bR\u{ff3d}")
})
.collect();
out.push_str(&lines.join("[br]"));
out.push_str("\n\n");
}
@@ -109,6 +117,16 @@ mod tests {
assert_eq!(d.cues[0].end, crate::text::ms_to_tick(3500));
}
#[test]
fn writer_escapes_every_case_of_literal_break_tokens() {
let doc = Document {
cues: vec![Cue { start: Tick(0), end: crate::text::ms_to_tick(1000), text: "a[br]b[BR]c[Br]d[bR]é".into(), ..Default::default() }],
..Default::default()
};
let decoded = parse(&write(&doc)).unwrap();
assert_eq!(decoded.cues[0].text, "a[br]b[BR]c[Br]d[bR]é");
}
#[test]
fn rejects_other_text() {
assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nx\n").is_err());