all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: npostavs@users.sourceforge.net
To: Thierry Volpiatto <thierry.volpiatto@gmail.com>
Cc: 25391@debbugs.gnu.org
Subject: bug#25391: 24.5; ffap-guesser "stack overflow in regexp matcher" error may crash emacs.
Date: Sun, 08 Jan 2017 18:29:08 -0500	[thread overview]
Message-ID: <87lgul167v.fsf@users.sourceforge.net> (raw)
In-Reply-To: <8760lp8nqv.fsf@gmail.com> (Thierry Volpiatto's message of "Sun,  08 Jan 2017 18:28:40 +0100")

[-- Attachment #1: Type: text/plain, Size: 652 bytes --]

tags 25391 patch
quit

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
> However, this handle the problem with "Stack overflow in regexp matcher"
> generally, but perhaps regexps such as `ffap-gopher-regexp` could be
> avoided when possible in emacs source code.

Ah, so by "crash" you meant throw an error.

>
> IIUC regexps like "^.*\\(this\\|that etc...\\)" create an incredible
> number of travels between bol and eol (which may sometimes be eob) and
> lead to a stack overflow inevitably on long lines (perhaps not so long
> in this case).

Right, and ffap-gopher-at-point is also using `set' on local variables,
icky.  Here's a patch:


[-- Attachment #2: patch --]
[-- Type: text/plain, Size: 4854 bytes --]

From 589054b197ab4a9778454efd0192bdd45ce38518 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs@gmail.com>
Date: Sun, 8 Jan 2017 18:19:32 -0500
Subject: [PATCH v1] Improve ffap-gopher-at-point handling of long lines

* lisp/ffap.el (ffap-gopher-regexp): Remove.
(ffap--gopher-var-on-line): New function.
(ffap-gopher-at-point): Use it instead of ffap-gopher-regexp which could
overflow the regexp stack on long lines (Bug#25391).  Use `let-alist'
instead of calling `set' on local variables.
* test/lisp/ffap-tests.el (ffap-gopher-at-point): New test.
---
 lisp/ffap.el            | 61 ++++++++++++++++++++++++-------------------------
 test/lisp/ffap-tests.el | 15 ++++++++++++
 2 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/lisp/ffap.el b/lisp/ffap.el
index 8144d41..1ef2b10 100644
--- a/lisp/ffap.el
+++ b/lisp/ffap.el
@@ -1193,10 +1193,17 @@ ffap-url-at-point
                       (list (car bounds) (cdr bounds)))))
           val))))
 
-(defvar ffap-gopher-regexp
-  "^.*\\<\\(Type\\|Name\\|Path\\|Host\\|Port\\) *= *\\(.*\\) *$"
-  "Regexp matching a line in a gopher bookmark (maybe indented).
-The two subexpressions are the KEY and VALUE.")
+(defun ffap--gopher-var-on-line ()
+  "Return (KEY . VALUE) of gopher bookmark on current line."
+  (save-excursion
+    (let ((bol (progn (beginning-of-line) (point)))
+          (eol (progn (end-of-line) (skip-chars-backward " ") (point))))
+     (goto-char bol)
+     (when (re-search-forward
+            "\\<\\(Type\\|Name\\|Path\\|Host\\|Port\\) *= *" eol t)
+       (let ((key (match-string 1))
+             (val (buffer-substring-no-properties (match-end 0) eol)))
+         (cons (intern (downcase key)) val))))))
 
 (defun ffap-gopher-at-point ()
   "If point is inside a gopher bookmark block, return its URL.
@@ -1204,33 +1211,25 @@ ffap-gopher-at-point
 Sets the variable `ffap-string-at-point-region' to the bounds of URL, if any."
   ;; `gopher-parse-bookmark' from gopher.el is not so robust
   (save-excursion
-    (beginning-of-line)
-    (if (looking-at ffap-gopher-regexp)
-	(progn
-	  (while (and (looking-at ffap-gopher-regexp) (not (bobp)))
-	    (forward-line -1))
-	  (or (looking-at ffap-gopher-regexp) (forward-line 1))
-          (setq ffap-string-at-point-region (list (point) (point)))
-	  (let ((type "1") path host (port "70"))
-	    (while (looking-at ffap-gopher-regexp)
-	      (let ((var (intern
-			  (downcase
-			   (buffer-substring (match-beginning 1)
-					     (match-end 1)))))
-		    (val (buffer-substring (match-beginning 2)
-					   (match-end 2))))
-		(set var val)
-		(forward-line 1)))
-            (setcdr ffap-string-at-point-region (list (point)))
-	    (if (and path (string-match "^ftp:.*@" path))
-		(concat "ftp://"
-			(substring path 4 (1- (match-end 0)))
-			(substring path (match-end 0)))
-	      (and (= (length type) 1)
-		   host;; (ffap-machine-p host)
-		   (concat "gopher://" host
-			   (if (equal port "70") "" (concat ":" port))
-			   "/" type path))))))))
+    (let* ((beg (progn (beginning-of-line)
+                       (while (and (not (bobp)) (ffap--gopher-var-on-line))
+                         (forward-line -1))
+                       (point)))
+           (bookmark (cl-loop for keyval = (ffap--gopher-var-on-line)
+                              while keyval collect keyval
+                              do (forward-line 1))))
+      (when bookmark
+        (setq ffap-string-at-point-region (list beg (point)))
+        (let-alist (nconc bookmark '((type . "1") (port . "70")))
+          (if (and .path (string-match "\\`ftp:.*@" .path))
+              (concat "ftp://"
+                      (substring .path 4 (1- (match-end 0)))
+                      (substring .path (match-end 0)))
+            (and (= (length .type) 1)
+                 .host ;; (ffap-machine-p host)
+                 (concat "gopher://" .host
+                         (if (equal .port "70") "" (concat ":" .port))
+                         "/" .type .path))))))))
 
 (defvar ffap-ftp-sans-slash-regexp
   (and
diff --git a/test/lisp/ffap-tests.el b/test/lisp/ffap-tests.el
index 1ba5f86..f3414ac 100644
--- a/test/lisp/ffap-tests.el
+++ b/test/lisp/ffap-tests.el
@@ -49,6 +49,21 @@
               (should (equal '(1 1) ffap-string-at-point-region)))))
       (and (file-exists-p file) (delete-file file)))))
 
+(ert-deftest ffap-gopher-at-point ()
+  (with-temp-buffer
+    (insert "\
+Type = 1
+Name = foo
+Path = /the/path
+Port = 7070
+Host = example.com\n")
+    (should-not (ffap-gopher-at-point))
+    (goto-char (point-min))
+    (should (equal (ffap-gopher-at-point)
+                   "gopher://example.com:7070/1/the/path"))
+    (should (equal ffap-string-at-point-region
+                   (list (point-min) (point-max))))))
+
 (provide 'ffap-tests)
 
 ;;; ffap-tests.el ends here
-- 
2.9.3


  reply	other threads:[~2017-01-08 23:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-08  7:31 bug#25391: 24.5; ffap-guesser "stack overflow in regexp matcher" error may crash emacs Thierry Volpiatto
2017-01-08 15:08 ` npostavs
2017-01-08 17:28   ` Thierry Volpiatto
2017-01-08 23:29     ` npostavs [this message]
2017-01-09  7:00       ` Thierry Volpiatto
2017-01-10  4:37         ` npostavs
2017-01-10  7:17           ` Thierry Volpiatto
2017-01-10 13:37             ` npostavs
2017-01-10 14:42               ` Thierry Volpiatto
2017-01-14  2:25                 ` npostavs
2017-01-14  6:57                   ` npostavs
     [not found]                   ` <874m12t57f.fsf@gmail.com>
2017-01-14 14:43                     ` Noam Postavsky
     [not found]                       ` <8737gltvw8.fsf@gmail.com>
2017-01-15 19:08                         ` Noam Postavsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lgul167v.fsf@users.sourceforge.net \
    --to=npostavs@users.sourceforge.net \
    --cc=25391@debbugs.gnu.org \
    --cc=thierry.volpiatto@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.