Support deep TeX numeric scans with bounded stack growth

This commit is contained in:
gloamxun committed 2026-11-21 14:34:55 +08:00
1 parent 7070cc6b82
commit dba73f1f47
9 files changed
+206 -47

No files matched your search

@@ -14,13 +14,9 @@ impl Executor {
files: &mut impl SourceProvider,
parenthesized: bool,
) -> Result<RegisterValue, Error> {
if self.expression_depth >= self.limits.input_depth.min(32) {
return self.fail("native expression nesting limit exceeded");
}
self.expression_depth += 1;
let result = self.quantity_expression_sum(kind, files, parenthesized);
self.expression_depth -= 1;
result
self.with_expression_scan(|executor| {
executor.quantity_expression_sum(kind, files, parenthesized)
})
}
fn quantity_expression_sum(
&mut self,
@@ -68,18 +68,17 @@ impl Executor {
primitive: Primitive,
files: &mut impl SourceProvider,
) -> Result<i32, Error> {
self.begin_value_scan()?;
let value = self.quantity(RegisterKind::Skip, files);
self.scanner_depth -= 1;
let RegisterValue::Glue(glue) = value? else {
unreachable!()
};
Ok(match primitive {
Primitive::Gluestretch => glue.stretch.amount,
Primitive::Glueshrink => glue.shrink.amount,
Primitive::Gluestretchorder => i32::from(glue.stretch.order),
Primitive::Glueshrinkorder => i32::from(glue.shrink.order),
_ => unreachable!(),
self.with_value_scan(|executor| {
let RegisterValue::Glue(glue) = executor.quantity(RegisterKind::Skip, files)? else {
unreachable!()
};
Ok(match primitive {
Primitive::Gluestretch => glue.stretch.amount,
Primitive::Glueshrink => glue.shrink.amount,
Primitive::Gluestretchorder => i32::from(glue.stretch.order),
Primitive::Glueshrinkorder => i32::from(glue.shrink.order),
_ => unreachable!(),
})
})
}
pub(super) fn prepare_magnification(&mut self) -> Result<i32, Error> {
@@ -306,16 +305,11 @@ impl Executor {
infinite: bool,
files: &mut impl SourceProvider,
) -> Result<Flex, Error> {
self.begin_value_scan()?;
let result = self.dimension_inner(math, infinite, true, files);
self.scanner_depth -= 1;
result
self.with_value_scan(|executor| executor.dimension_inner(math, infinite, true, files))
}
pub(super) fn decimal_fixed(&mut self, files: &mut impl SourceProvider) -> Result<i32, Error> {
self.begin_value_scan()?;
let result = self.dimension_inner(false, false, false, files);
self.scanner_depth -= 1;
Ok(result?.amount)
self.with_value_scan(|executor| executor.dimension_inner(false, false, false, files))
.map(|value| value.amount)
}
fn dimension_inner(
@@ -0,0 +1,32 @@
//! Physical stack support for mutually recursive TeX scanners and expansion.
const SEGMENTED: bool = cfg!(any(
windows,
all(
any(target_os = "linux", target_os = "macos"),
any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "aarch64"
),
),
));
pub(super) fn limit(input_depth: usize) -> usize {
if SEGMENTED {
input_depth
} else {
input_depth.min(32)
}
}
#[inline]
pub(super) fn enter<T>(scan: impl FnOnce() -> T) -> T {
if SEGMENTED {
// Scanner and expansion callbacks stay on the caller's thread.
// Resource limits count TeX frames independently of the Rust stack.
stacker::maybe_grow(128 * 1024, 1024 * 1024, scan)
} else {
scan()
}
}