summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Schmitz <tobiasschmitz2001@gmail.com>2025-05-12 10:06:18 +0200
committerGitHub <noreply@github.com>2025-05-12 08:06:18 +0000
commit54c5113a83d317ed9c37a129ad90165c100bd25d (patch)
treef350bfef161cb1e535076f210610ee549b7a81a6
parent9b09146a6b5e936966ed7ee73bce9dd2df3810ae (diff)
Catch indefinite loop in realization due to cycle between show and grouping rule (#6259)
-rw-r--r--crates/typst-realize/src/lib.rs11
-rw-r--r--tests/suite/styling/show.typ8
2 files changed, 19 insertions, 0 deletions
diff --git a/crates/typst-realize/src/lib.rs b/crates/typst-realize/src/lib.rs
index 151ae76b..7d2460a8 100644
--- a/crates/typst-realize/src/lib.rs
+++ b/crates/typst-realize/src/lib.rs
@@ -655,6 +655,7 @@ fn visit_grouping_rules<'a>(
let matching = s.rules.iter().find(|&rule| (rule.trigger)(content, &s.kind));
// Try to continue or finish an existing grouping.
+ let mut i = 0;
while let Some(active) = s.groupings.last() {
// Start a nested group if a rule with higher priority matches.
if matching.is_some_and(|rule| rule.priority > active.rule.priority) {
@@ -670,6 +671,16 @@ fn visit_grouping_rules<'a>(
}
finish_innermost_grouping(s)?;
+ i += 1;
+ if i > 512 {
+ // It seems like this case is only hit when there is a cycle between
+ // a show rule and a grouping rule. The show rule produces content
+ // that is matched by a grouping rule, which is then again processed
+ // by the show rule, and so on. The two must be at an equilibrium,
+ // otherwise either the "maximum show rule depth" or "maximum
+ // grouping depth" errors are triggered.
+ bail!(content.span(), "maximum grouping depth exceeded");
+ }
}
// Start a new grouping.
diff --git a/tests/suite/styling/show.typ b/tests/suite/styling/show.typ
index e8ddf553..f3d9efd5 100644
--- a/tests/suite/styling/show.typ
+++ b/tests/suite/styling/show.typ
@@ -258,3 +258,11 @@ I am *strong*, I am _emphasized_, and I am #[special<special>].
= Hello
*strong*
+
+--- issue-5690-oom-par-box ---
+// Error: 3:6-5:1 maximum grouping depth exceeded
+#show par: box
+
+Hello
+
+World