address review: empty-document range cannot overflow in set_attr

This commit is contained in:
Dylan M. Taylor
2026-11-03 22:01:48 +00:00
parent 2d20a577a4
commit 3f775ab20c
+8 -1
View File
@@ -977,7 +977,8 @@ impl TextDoc {
if r.start >= r.end && n > 0 {
return Ok(true);
}
let range = r.start..r.end.max(r.start + usize::from(n == 0));
// Empty text: apply_style ignores the bounds, so a fixed range avoids `start + 1` overflow.
let range = if n == 0 { 0..1 } else { r.start..r.end };
// Check every affected run first, so a value one run refuses changes none of them.
let mut err = None;
self.apply_style(range.clone(), |s| {
@@ -1377,6 +1378,12 @@ mod tests {
assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES);
}
#[test]
fn empty_document_accepts_an_extreme_range_without_overflow() {
let mut doc = TextDoc::plain("");
assert!(doc.set_attr("size", &J::from(20), Some(usize::MAX..usize::MAX)).unwrap());
}
#[test]
fn mixed_line_breaks_keep_original_byte_and_character_ranges() {
let text = "é\r\nβ\rx\n\n";