From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: npostavs@users.sourceforge.net Newsgroups: gmane.emacs.bugs 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 Message-ID: <87lgul167v.fsf@users.sourceforge.net> References: <87vatqc8j1.fsf@gmail.com> <87y3yl1tdp.fsf@users.sourceforge.net> <8760lp8nqv.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1483918161 17326 195.159.176.226 (8 Jan 2017 23:29:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 8 Jan 2017 23:29:21 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) Cc: 25391@debbugs.gnu.org To: Thierry Volpiatto Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Jan 09 00:29:16 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQMth-0003KU-Ke for geb-bug-gnu-emacs@m.gmane.org; Mon, 09 Jan 2017 00:29:09 +0100 Original-Received: from localhost ([::1]:36145 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cQMtm-0006jJ-1x for geb-bug-gnu-emacs@m.gmane.org; Sun, 08 Jan 2017 18:29:14 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42986) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cQMtf-0006hz-8y for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2017 18:29:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cQMta-0003O9-CA for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2017 18:29:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:60073) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cQMta-0003O2-81 for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2017 18:29:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cQMta-000871-26 for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2017 18:29:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: npostavs@users.sourceforge.net Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 08 Jan 2017 23:29:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 25391 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 25391-submit@debbugs.gnu.org id=B25391.148391809231110 (code B ref 25391); Sun, 08 Jan 2017 23:29:02 +0000 Original-Received: (at 25391) by debbugs.gnu.org; 8 Jan 2017 23:28:12 +0000 Original-Received: from localhost ([127.0.0.1]:47239 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQMsm-00085d-90 for submit@debbugs.gnu.org; Sun, 08 Jan 2017 18:28:12 -0500 Original-Received: from mail-io0-f193.google.com ([209.85.223.193]:33010) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQMsk-00085H-IL; Sun, 08 Jan 2017 18:28:10 -0500 Original-Received: by mail-io0-f193.google.com with SMTP id 101so1264881iom.0; Sun, 08 Jan 2017 15:28:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=xrhG7WdgJRV8cI2XE1NltsizOCFnUQ28sW0jGoKyz0k=; b=gM6MsDGNwL7qW6EIS1J3vbfPlimN9Tvvik6TADCdCyrHCHdB/nAD+nKoADQAeNH3FV 9S/349jFkSFxUv0rG2hXNrDcU1wpHCCUCaA9+vTv+QQUYSxOttgJ9ShMea4/f3ScXskH XmDngkX2YkH57usZxtX1BKIkVa5QUSXQ5XnI+YdOGuWlO8njW2J+fsIjbep3VzgGZpnV 5Es1YTDpSgC85oTlZ7f0XrBVS/eyJxfLisfyR/HNmt5ZxB7ENirrbOQ5rnjTwc9DlXXn 7rp/33P3ZJLJbD+PvfDSwMSBEPSzwdwFNFPPbLG47FE8FW1Z1LEJsyPRC+PRUNQtbmz+ I1WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:references:date :in-reply-to:message-id:user-agent:mime-version; bh=xrhG7WdgJRV8cI2XE1NltsizOCFnUQ28sW0jGoKyz0k=; b=uLnuIE0lbaNUvp8pUp1UlgLoGMkDJAEeIu/KUdoCyzFPPNLb7uMPX2TMycfZD960eg J+CeDphQhye5jX+hcFmrWxbqP8jyRxM6WCkcxXRrUloAJ/cncTMmC35mbhK5zdUUZip5 gQ+4XqehA+lvxrGPohtg6GcpVTk/WrrntCEYYcFjQjXL8f0aig0b8SbuMy+NfLrkUP/P wFdIUFu+xkgkOJqWBhR/CcQV/M0f4mdllkiqN8U1oBAZoZfUkwj82EhnvffQzA1VcEXn 6lBr3GeTZwz2/+eirO9SYUNQBvF0X5pJ84XmP9hgd9BUWdsAvhp/MxQ0xfliBg0LfPkD U+fg== X-Gm-Message-State: AIkVDXI8EU83MofMUO/Flc+jda8v4wv29QhZnOtjYSFGZzMUxKWObiiEYn1V05X8gUWaQg== X-Received: by 10.107.190.6 with SMTP id o6mr70388716iof.95.1483918084985; Sun, 08 Jan 2017 15:28:04 -0800 (PST) Original-Received: from zony ([45.2.7.65]) by smtp.googlemail.com with ESMTPSA id p187sm8096938iop.23.2017.01.08.15.28.03 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 08 Jan 2017 15:28:04 -0800 (PST) In-Reply-To: <8760lp8nqv.fsf@gmail.com> (Thierry Volpiatto's message of "Sun, 08 Jan 2017 18:28:40 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:127919 Archived-At: --=-=-= Content-Type: text/plain tags 25391 patch quit Thierry Volpiatto 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: --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=v1-0001-Improve-ffap-gopher-at-point-handling-of-long-lin.patch Content-Description: patch >From 589054b197ab4a9778454efd0192bdd45ce38518 Mon Sep 17 00:00:00 2001 From: Noam Postavsky 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 --=-=-=--