Subject: [PATCH] cperl-mode: don't freeze over a cool regexp

From: Harald Jörg <[email-masked]>
Date: Wed, 2 Sep 2020 22:06:40 +0200

That bug exists since 14 years, but only surfaces if the regexp has a
closing brace in the wrong place (two characters before an unfinished
group starts).  The reason is that `cperl-forward-group-in-re' failed
to report unbalanced parentheses: In the function, the error was too
narrowly localized and gets lost before returning.

Patch attached, including two test cases.
-- 
Cheers,
haj
From 3e513396340736dd182e5afa562abca5641ef300 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Harald=20J=C3=B6rg?= <[email-masked]>
Date: Wed, 2 Sep 2020 21:47:10 +0200
Subject: [PATCH] =?UTF-8?q?2020-09-02=20=20Harald=20J=C3=B6rg=20=20<haj@po?=
 =?UTF-8?q?steo.de>?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 * lisp/progmodes/cperl-mode.el (cperl-forward-group-in-re): Make
 sure that an error is reported back to the caller (Bug#16368).

 * test/lisp/progmodes/cperl-mode-tests.el (cperl-mode-test-bug-16368):
 Tests for balanced (no error) and unbalanced (caught exception)
 cases of `cperl-forward-group-in-re'.
---
 lisp/progmodes/cperl-mode.el            |  9 +++++----
 test/lisp/progmodes/cperl-mode-tests.el | 21 +++++++++++++++++++++
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/cperl-mode.el b/lisp/progmodes/cperl-mode.el
index 44579cfd38..ac22218318 100644
--- a/lisp/progmodes/cperl-mode.el
+++ b/lisp/progmodes/cperl-mode.el
@@ -3241,8 +3241,8 @@ cperl-forward-group-in-re
 Works before syntax recognition is done."
   ;; Works *before* syntax recognition is done
   (or st-l (setq st-l (list nil)))	; Avoid overwriting '()
-  (let (st b reset-st)
-    (condition-case b
+  (let (st result reset-st)
+    (condition-case err
 	(progn
 	  (setq st (cperl-cached-syntax-table st-l))
 	  (modify-syntax-entry ?\( "()" st)
@@ -3251,7 +3251,8 @@ cperl-forward-group-in-re
 	  (set-syntax-table st)
 	  (forward-sexp 1))
       (error (message
-	      "cperl-forward-group-in-re: error %s" b)))
+	      "cperl-forward-group-in-re: error %s" err)
+             (setq result err)))
     ;; now restore the initial state
     (if st
 	(progn
@@ -3259,7 +3260,7 @@ cperl-forward-group-in-re
 	  (modify-syntax-entry ?\) "." st)))
     (if reset-st
 	(set-syntax-table reset-st))
-    b))
+    result))

 (defvar font-lock-string-face)
diff --git a/test/lisp/progmodes/cperl-mode-tests.el b/test/lisp/progmodes/cperl-mode-tests.el
index be8b42d99a..3a49b91ec2 100644
--- a/test/lisp/progmodes/cperl-mode-tests.el
+++ b/test/lisp/progmodes/cperl-mode-tests.el
@@ -48,4 +48,25 @@ cperl-mode-test-bug-42168
   (let ((code "{ $a- / $b } # /"))
     (should (equal (nth 8 (cperl-test-ppss code "/")) 7))))
 
+(ert-deftest cperl-mode-test-bug-16368 ()
+  "Verify that `cperl-forward-group-in-re' doesn't hide errors."
+  (require 'cperl-mode)
+  (let ((code "/(\\d{4})(?{2}/;")     ; the regex from the bug report
+        (result))
+    (with-temp-buffer
+      (insert code)
+      (goto-char 9)
+      (setq result (cperl-forward-group-in-re))
+      (should (equal (car result) 'scan-error))
+      (should (equal (nth 1 result) "Unbalanced parentheses"))
+      (should (= (point) 9))))        ; point remains unchanged on error
+  (let ((code "/(\\d{4})(?{2})/;")    ; here all parens are balanced
+        (result))
+    (with-temp-buffer
+      (insert code)
+      (goto-char 9)
+      (setq result (cperl-forward-group-in-re))
+      (should (equal result nil))
+      (should (= (point) 15)))))      ; point has skipped the group
+
 ;;; cperl-mode-tests.el ends here
-- 
2.20.1