all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Noam Postavsky <npostavs@gmail.com>
Cc: Andreas Schwab <schwab@suse.de>,
	Cheng-An Yang <rhymer123@gmail.com>,
	22149@debbugs.gnu.org
Subject: bug#22149: 24.4; gdb stack overflow in regexp matcher
Date: Fri, 13 Mar 2020 19:58:23 +0100	[thread overview]
Message-ID: <C0103351-9BB4-4174-B3B9-A5314F2B651A@acm.org> (raw)
In-Reply-To: <CAKAd=p6_kJUsMmZ=5=SjOJzZDxfmXm8kiKZFzBzJbm0=3QLoig@mail.gmail.com>

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

This was a while ago, but the effect can still be observed in current master (28.0.50). The exact reproduction no longer works but it's probably just a quantitive change; perhaps the regexp stack has become bigger.

Simplified, and with some character renaming for clarity, the test case is essentially

(string-match (rx "t"
                  (* (or (not (any "bq"))
                         (: "b" nonl)))
                  "q")
              (concat "t" (make-string 160000 ?a)))

where the number 160000 can be lowered a bit to avoid the stack overflow.

One way of dodging the error regardless of string size is to swap the two 'or' operands, so that the (: "b" nonl) part, which usually fails, is attempted first. This is because the NFA matcher implements a kind of TCO: no backtrack point on the stack is needed for the last or-clause.

In fact, Noam's proposed workaround, equivalent to

(string-match (rx "t"
                  (* (or "bq" (not "b")))
                  "q")
              (concat "t" (make-string 160000 ?a)))

works precisely for this reason -- swapping the or-clauses here gives a stack overflow again.

What about this patch for Emacs 27?


[-- Attachment #2: 0001-Avoid-regexp-stack-overflow-in-GDB-string-matching-b.patch --]
[-- Type: application/octet-stream, Size: 1234 bytes --]

From 98db2fe313f869fcd146e4accc9e8cb2296468a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Fri, 13 Mar 2020 19:51:02 +0100
Subject: [PATCH] Avoid regexp stack overflow in GDB string matching
 (bug#22149)

* lisp/progmodes/gdb-mi.el (gdb--string-regexp):
Swap the or-clauses so that the rarely matching one comes first.
This avoids a build-up of backtrack points on the regexp stack.
---
 lisp/progmodes/gdb-mi.el | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/gdb-mi.el b/lisp/progmodes/gdb-mi.el
index e785acd284..5533aa000d 100644
--- a/lisp/progmodes/gdb-mi.el
+++ b/lisp/progmodes/gdb-mi.el
@@ -1035,7 +1035,10 @@ gdb-create-define-alist
 
 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
 
-(defconst gdb--string-regexp "\"\\(?:[^\\\"]\\|\\\\.\\)*\"")
+(defconst gdb--string-regexp (rx "\""
+                                 (* (or (seq "\\" nonl)
+                                        (not (any "\"\\"))))
+                                 "\""))
 
 (defun gdb-tooltip-print (expr)
   (with-current-buffer (gdb-get-buffer 'gdb-partial-output-buffer)
-- 
2.21.1 (Apple Git-122.3)


  parent reply	other threads:[~2020-03-13 18:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-12  6:16 bug#22149: 24.4; gdb stack overflow in regexp matcher Cheng-An Yang
2015-12-12 10:13 ` Andreas Schwab
2016-07-01  3:16   ` npostavs
2020-03-13 18:58 ` Mattias Engdegård [this message]
2020-03-13 19:39   ` Eli Zaretskii
2020-03-13 20:11     ` Mattias Engdegård
2020-03-14  7:58       ` Eli Zaretskii
2020-03-14  9:04         ` Mattias Engdegård
2020-03-14 10:14           ` Eli Zaretskii
2020-03-14 10:43             ` Mattias Engdegård

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=C0103351-9BB4-4174-B3B9-A5314F2B651A@acm.org \
    --to=mattiase@acm.org \
    --cc=22149@debbugs.gnu.org \
    --cc=npostavs@gmail.com \
    --cc=rhymer123@gmail.com \
    --cc=schwab@suse.de \
    /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.