Carlos Dagorret <[email protected]> writes:
Mykhailo Mishchenko <[email protected]> writes:

I'm quite new to this, but while testing it I noticed a small difference
with the original code:

-               (match-string 0)))))
+               (let ((from (match-end 0)))
+                 (when (re-search-forward "</math>" nil t)
+                   (buffer-substring from (match-beginning 0))))))))
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)))))

Yes, it was my mistake. I did not notice the original code did not strip the `<math>...</math>` wrapper. I took the liberty to format your changes as a patch in a patch set.

Mykhailo Mishchenko
From 6d871cbff0360b8b1d7b6e86a31d0d06c17d13bf Mon Sep 17 00:00:00 2001
From: Mykhailo Mishchenko <[email protected]>
Date: Wed, 2 Sep 2026 16:16:40 +0300
Subject: [PATCH 1/2] org-create-math-formula: fix regex matching stack
 overflow

---
 lisp/org.el | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 3803971ce..0d87c93b4 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16915,11 +16915,13 @@ inspection."
               (insert-file-contents tmp-out-file)
 	      (goto-char (point-min))
 	      (when (re-search-forward
-		     (format "<math[^>]*?%s[^>]*?>\\(.\\|\n\\)*</math>"
-			     (regexp-quote
-			      "xmlns=\"http://www.w3.org/1998/Math/MathML\"";))
-		     nil t)
-		(match-string 0)))))
+                     (format "<math[^>]*?%s[^>]*?>"
+	                     (regexp-quote
+	                      "xmlns=\"http://www.w3.org/1998/Math/MathML\"";))
+                     nil t)
+                (let ((from (match-end 0)))
+                  (when (re-search-forward "</math>" nil t)
+                    (buffer-substring from (match-beginning 0))))))))
     (cond
      (mathml
       (setq mathml
-- 
2.39.5

From 1b48bd30c83ef2f31d81eeae1b85a12e3723b9a4 Mon Sep 17 00:00:00 2001
From: Carlos Dagorret <[email protected]>
Date: Wed, 2 Sep 2026 14:31:46 -0300
Subject: [PATCH 2/2] org-create-math-formula: Keep `<math>...</math>` wrapper

---
 lisp/org.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 0d87c93b4..9cd2b9ea1 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16919,9 +16919,9 @@ inspection."
 	                     (regexp-quote
 	                      "xmlns=\"http://www.w3.org/1998/Math/MathML\"";))
                      nil t)
-                (let ((from (match-end 0)))
+                (let ((from (match-beginning 0)))
                   (when (re-search-forward "</math>" nil t)
-                    (buffer-substring from (match-beginning 0))))))))
+                    (buffer-substring from (match-end 0))))))))
     (cond
      (mathml
       (setq mathml
-- 
2.39.5

Reply via email to