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

+29 -16
View File
@@ -32,6 +32,7 @@ mod pdf_links;
pub(super) mod pdf_match;
mod quantities;
mod restores;
mod scan_stack;
mod scanning;
mod scheduling;
mod specials;
@@ -768,11 +769,13 @@ impl Executor {
files: &mut impl SourceProvider,
definition_context: bool,
) -> Result<Option<InputToken>, Error> {
if self.expansion_depth >= self.limits.input_depth.min(32) {
if self.expansion_depth >= scan_stack::limit(self.limits.input_depth) {
return self.fail("native expansion nesting limit exceeded");
}
self.expansion_depth += 1;
let result = self.expand_with_pending_tokens(input, files, definition_context);
let result = scan_stack::enter(|| {
self.expand_with_pending_tokens(input, files, definition_context)
});
self.expansion_depth -= 1;
result
}
@@ -2368,13 +2371,7 @@ impl Executor {
}
}
fn expression(&mut self, files: &mut impl SourceProvider, depth: usize) -> Result<i32, Error> {
if self.expression_depth >= self.limits.input_depth.min(32) {
return self.fail("native expression nesting limit exceeded");
}
self.expression_depth += 1;
let value = self.expression_sum(files, depth)?;
self.expression_depth -= 1;
Ok(value)
self.with_expression_scan(|executor| executor.expression_sum(files, depth))
}
fn expression_sum(
&mut self,
@@ -2479,17 +2476,33 @@ impl Executor {
Ok(value)
}
fn integer(&mut self, files: &mut impl SourceProvider) -> Result<i32, Error> {
self.begin_value_scan()?;
let result = self.integer_inner(files);
self.scanner_depth -= 1;
result
self.with_value_scan(|executor| executor.integer_inner(files))
}
fn begin_value_scan(&mut self) -> Result<(), Error> {
if self.scanner_depth >= self.limits.input_depth.min(32) {
fn with_value_scan<T>(
&mut self,
scan: impl FnOnce(&mut Self) -> Result<T, Error>,
) -> Result<T, Error> {
if self.scanner_depth >= scan_stack::limit(self.limits.input_depth) {
return self.fail("native value scanner nesting limit exceeded");
}
self.scanner_depth += 1;
Ok(())
let result = scan_stack::enter(|| scan(self));
self.scanner_depth -= 1;
result
}
fn with_expression_scan<T>(
&mut self,
scan: impl FnOnce(&mut Self) -> Result<T, Error>,
) -> Result<T, Error> {
if self.expression_depth >= scan_stack::limit(self.limits.input_depth) {
return self.fail("native expression nesting limit exceeded");
}
self.expression_depth += 1;
let result = scan_stack::enter(|| scan(self));
self.expression_depth -= 1;
result
}
fn integer_inner(&mut self, files: &mut impl SourceProvider) -> Result<i32, Error> {
let mut sign = 1;
@@ -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()
}
}