From ae12889ecf2ece3315100837a920e026d2fe4a9b Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:36:20 -0500 Subject: [PATCH] TeX reader: don't reparse a script after an error inside it. scripted was wrapped in try, so an error inside a script argument made it backtrack, expr returned the base alone, and bareSubSup then parsed the same script again, at every level: with an error at the innermost level, nested scripts still took time exponential in the depth (16 levels took 1s, the 29 of the new test didn't finish in 10 minutes), although 0495533d made the valid case linear. Once a script has started, an error in it is now an error. --- src/Text/TeXMath/Readers/TeX.hs | 2 +- test/reader/tex/nested_scripts_error_performance.test | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 test/reader/tex/nested_scripts_error_performance.test diff --git a/src/Text/TeXMath/Readers/TeX.hs b/src/Text/TeXMath/Readers/TeX.hs index de091ee1..5c5ddb62 100644 --- a/src/Text/TeXMath/Readers/TeX.hs +++ b/src/Text/TeXMath/Readers/TeX.hs @@ -672,7 +672,7 @@ scriptArg = (fst <$> operatorname) -- other script, and then reparsing it would take time exponential in -- the nesting depth. scripted :: Maybe Bool -> Bool -> Exp -> TP Exp -scripted limits convertible a = try $ do +scripted limits convertible a = do let sub1 = symbol "_" >> scriptArg -- see #273 let sup1 = (symbol "^" >> scriptArg) <|> primes scripts <- (do m <- sub1 diff --git a/test/reader/tex/nested_scripts_error_performance.test b/test/reader/tex/nested_scripts_error_performance.test new file mode 100644 index 00000000..29d35524 --- /dev/null +++ b/test/reader/tex/nested_scripts_error_performance.test @@ -0,0 +1,7 @@ +<<< tex +x_{x_{x_{x_{x_{x_{x_{x_{x_{x_{x_{x_{x_{x_{x^{x^{x^{x^{x^{x^{x^{x^{x^{x^{x^{x^{x^{x^{x^{x\right)}}}}}}}}}}}}}}}}}}}}}}}}}}}}} +>>> native +x^{x^{x^{x^{x\right)}}}}}}}}}}}}}}}}}}}} + ^ +unexpected control sequence \right +expecting "%", "\\label", "\\tag", "\\nonumber", whitespace or "\\allowbreak"