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;