Carlos Dagorret <[email protected]> writes:

> I ran into this exact issue when exporting a document with a complex 
> MathJax formula, and applying your fix solved the stack overflow for me.
> ...
> It worked fine in my specific test, but I think we need to keep the 
> full `<math>...</math>` wrapper because OpenDocument / LibreOffice needs 
> those tags to know where the MathML object actually starts and ends. 
> Without them, it might fail to render as a proper formula in other cases.
>
> To keep the tags (just like the original code did), we could use 
> `(match-beginning 0)` like this:
>
> (when (re-search-forward
>        (format "<math[^>]*?%s[^>]*?>"
>                (regexp-quote "xmlns=\"http://www.w3.org/1998/Math/MathML\"";))
>        nil t)
>   (let ((from (match-beginning 0)))
>     (when (re-search-forward "</math>" nil t)
>       (buffer-substring from (match-end 0)))))

What about simply using non-greedy version of the original regexp?
I cannot test it myself since I do not have a reproducer.

>From 8fedad0965fddf79b1288a989dc4e56630bdf3fe Mon Sep 17 00:00:00 2001
Message-ID: <8fedad0965fddf79b1288a989dc4e56630bdf3fe.1788618959.git.yanta...@posteo.net>
From: Ihor Radchenko <[email protected]>
Date: Sat, 5 Sep 2026 16:34:44 +0200
Subject: [PATCH] org-create-math-formula: Fix stack overflow in regexp

* lisp/org.el (org-create-math-formula): Use non-greedy regexp
to match <math...>...</math> text.

Reported-by: Mykhailo Mishchenko <[email protected]>
Link: https://orgmode.org/list/75ZIj4KgLDc5DlcK8LET1LZ9Bcx9U_Fc9BaWygoePseLUWzWLPabKSlETnsVMhT9GflfDzEPSqFdqHT_shUCqZ7BXjBKA-IiCqV6qfXYOfY=@protonmail.com
---
 lisp/org.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/org.el b/lisp/org.el
index e07cfb337..c8bc28ea6 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16915,7 +16915,7 @@ (defun org-create-math-formula (latex-frag &optional mathml-file)
               (insert-file-contents tmp-out-file)
 	      (goto-char (point-min))
 	      (when (re-search-forward
-		     (format "<math[^>]*?%s[^>]*?>\\(.\\|\n\\)*</math>"
+		     (format "<math[^>]*?%s[^>]*?>\\(.\\|\n\\)*?</math>"
 			     (regexp-quote
 			      "xmlns=\"http://www.w3.org/1998/Math/MathML\"";))
 		     nil t)
-- 
2.54.0

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to