Compare commits

..
Author SHA1 Message Date
moandClaude Fable 5.2 07b219b707 LISP textbox: a text too long to shape in bounded memory is an error
Shaping makes many points for each character, and the run's string budget
does not count them, so a string of the 8 MiB a routine may build would
take gigabytes to measure. Past 64 KiB of text textbox now stops with an
error that names it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-03 14:52:42 -07:00
3 changed files with 20 additions and 2 deletions
+10
View File
@@ -836,6 +836,9 @@ pub fn regapp(ctx: &mut Ctx<'_>, a: Vec<Value>) -> Result<Flow, LispError> {
Ok(Flow::Value(Value::str(name.trim())))
}
/// The most bytes of text `textbox` measures.
const MAX_TEXTBOX_TEXT: usize = 64 << 10;
/// `(textbox elist)`: the smallest box around a TEXT that `elist` describes (an `entget` list, or
/// a partial one with at least the string, group 1), as its lower-left and upper-right corners
/// relative to the insertion point, the text unrotated and left-justified. Groups the list
@@ -856,6 +859,13 @@ pub fn textbox(ctx: &mut Ctx<'_>, a: Vec<Value>) -> Result<Flow, LispError> {
if group(1).is_none() || group(0).is_some_and(|k| !matches!(&k, Value::Str(s) if s.eq_ignore_ascii_case("TEXT"))) {
return nil();
}
// Shaping makes many points for each character, which the run's string budget does not
// count: a text too long to measure in bounded memory is an error, not a box.
if let Some(Value::Str(s)) = group(1)
&& s.len() > MAX_TEXTBOX_TEXT
{
return Err(LispError::new(format!("textbox: text longer than {MAX_TEXTBOX_TEXT} bytes")));
}
let Ok(d) = ctx.s.doc() else { return nil() };
let style = match group(7) {
Some(Value::Str(s)) => s.to_string(),
+2 -2
View File
@@ -498,8 +498,8 @@ fn drawn_text(d: &Drawing, e: &Entity) -> Option<Vec<Vec<Vec2>>> {
let e = match d.anno_draw(e) {
cadcraft_doc::AnnoDraw::Plain => e,
cadcraft_doc::AnnoDraw::Hidden => return None,
cadcraft_doc::AnnoDraw::Scaled { factor, by } => {
scaled = cadcraft_doc::anno_placed(e, factor, by);
cadcraft_doc::AnnoDraw::Scaled(f) => {
scaled = cadcraft_doc::anno_scaled(e, f);
&scaled
}
};
+8
View File
@@ -57,6 +57,14 @@ fn textbox_measures_text_as_drawn() {
let blank = textbox(&mut s, "'((1 . \" \"))").expect("spaces");
assert!(blank[0] == 0.0 && blank[1] == 0.0 && blank[2] > 0.0 && blank[3] > 0.0, "{blank:?}");
// A text too long to shape in bounded memory is an error that names the function, and one
// well under the limit still measures.
let long_text = |n: usize| json!({ "code": format!("(textbox '((1 . \"{}\")))", "8".repeat(n)) });
let refused = s.execute("lisp", &long_text(1_000_000)).unwrap();
assert!(refused["error"].as_str().is_some_and(|e| e.contains("textbox")), "{refused}");
let measured = s.execute("lisp", &long_text(1_000)).unwrap();
assert!(measured["error"].is_null() && measured["value"] != "nil", "{measured}");
// Not a text: nil.
assert_eq!(textbox(&mut s, "'((0 . \"LINE\") (10 0.0 0.0 0.0) (11 5.0 5.0 0.0) (1 . \"x\"))"), None);
assert_eq!(textbox(&mut s, "'((0 . \"TEXT\") (40 . 1.0))"), None, "no string");