How to fix “stack overflow in regexp matcher” in emacs

烈酒焚心 提交于 2019-12-22 04:03:55

问题


I'm a big fan of Emacs, and use it a lot, especially while programming and debugging (using gud) (C/C++).

Recently I had to debug a program (rather simple but that compute on a lot of data (Graph Theory)), but I had a rather annoying problem. During the execution step by step of the program, I get the following error:

error in process filter: Stack overflow in regexp matcher

I made some research to find out what it was, and I found out this post: Debugging in emacs (with gud) often results in stack overflow error.

So as I understand it, there is a problem with the regexp matcher and the fact that some things in my program are simply too long? (I do have unusually long function name with a lot of parameters and I also use unusually big container.)

I'd really like to fix this, but I don't know anything about debugging Emacs Lisp, is there anyone to help me ?

Here is the output I get from Emacs internal debbuger : http://pastebin.com/5CKe74e6

I should also point out, that I use a personalized version of Emacs Prelude.


回答1:


The underlying problem is that a regular expression (regexp) contains too many alternatives, and when applied to a (typically long) text it fails to match whatever it tried to match.

In your case it's the regexp:

"\\([[:alnum:]-_]+\\)=\\({\\|\\[\\|\"\"\\|\"\\(?:[^\\\"]\\|\\\\.\\)*\"\\)"

Which is used by the function gdb-jsonify-buffer.

It looks like this regexp tries to match assignments. Basically, it matches a variable to the left of an = and (part of) an expression to the right. One of the things the regexp seems to match is a string, with containing escaped quotes -- this is always a warning sign as Emacs provides far better methods for parsing strings.

The problem can originate from the fact that this regexp is wrong (so that it matches a lot more than your string), that you have a malformed string, or that your program simply contains a really large string.

I would suggest that you file a bug report to the maintainer of that package. Make sure you include the text that caused the error to be triggered.

Alternatively, you could make an attempt at fixing this yourself. I would suggest that you replace the complex regexp with a simpler regexp that find the beginning of a string. You could then use, for example, (forward-sexp) to find the end of the string.




回答2:


I was also having this problem, so I used Lindydancer's suggestion of converting the regexp on the string literal to use (forward-sexp), and it's been working fine for me.

I've posted the patch:

http://lists.gnu.org/archive/html/bug-gnu-emacs/2017-12/msg00968.html

so hopefully it will get merged at some point. In the meantime, you can use this for gdb-jsonify-buffer:


(defun gdb-jsonify-buffer (&optional fix-key fix-list)
  "Prepare GDB/MI output in current buffer for parsing with `json-read'.

Field names are wrapped in double quotes and equal signs are
replaced with semicolons.

If FIX-KEY is non-nil, strip all \"FIX-KEY=\" occurrences from
partial output.  This is used to get rid of useless keys in lists
in MI messages, e.g.: [key=.., key=..].  -stack-list-frames and
-break-info are examples of MI commands which issue such
responses.

If FIX-LIST is non-nil, \"FIX-LIST={..}\" is replaced with
\"FIX-LIST=[..]\" prior to parsing. This is used to fix broken
-break-info output when it contains breakpoint script field
incompatible with GDB/MI output syntax.

If `default-directory' is remote, full file names are adapted accordingly."
  (save-excursion
    (let ((remote (file-remote-p default-directory)))
      (when remote
        (goto-char (point-min))
        (while (re-search-forward "[\\[,]fullname=\"\\(.+\\)\"" nil t)
          (replace-match (concat remote "\\1") nil nil nil 1))))
    (goto-char (point-min))
    (when fix-key
      (save-excursion
        (while (re-search-forward (concat "[\\[,]\\(" fix-key "=\\)") nil t)
          (replace-match "" nil nil nil 1))))
    (when fix-list
      (save-excursion
        ;; Find positions of braces which enclose broken list
        (while (re-search-forward (concat fix-list "={\"") nil t)
          (let ((p1 (goto-char (- (point) 2)))
                (p2 (progn (forward-sexp)
                           (1- (point)))))
            ;; Replace braces with brackets
            (save-excursion
              (goto-char p1)
              (delete-char 1)
              (insert "[")
              (goto-char p2)
              (delete-char 1)
              (insert "]"))))))
    (goto-char (point-min))
    (insert "{")
    (let ((re (concat "\\([[:alnum:]-_]+\\)=")))
      (while (re-search-forward re nil t)
        (replace-match "\"\\1\":" nil nil)
        (if (eq (char-after) ?\") (forward-sexp) (forward-char))))
    (goto-char (point-max))
    (insert "}")))


来源:https://stackoverflow.com/questions/31206568/how-to-fix-stack-overflow-in-regexp-matcher-in-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!